?? jpaneltest.java
字號(hào):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JPanelTest extends JApplet {
Panel panel1 = new Panel();
BorderLayout borderLayout1 = new BorderLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField1 = new JTextField();
Font ft1,ft2;
private Component glassPane = new MyGlassPane();
//初始化
public void init() {
try {
myInit();
}
catch(Exception e) { //異常處理
e.printStackTrace();
}
}
//*************初始設(shè)定*******************
private void myInit() throws Exception {
this.setGlassPane(glassPane);//將一個(gè)JPanel當(dāng)作一個(gè)glassPane
this.getContentPane().setLayout(borderLayout1);//設(shè)定ContentPane的Layout
//#設(shè)定jButton1的外觀
jButton1.setBackground(Color.black);
jButton1.setForeground(Color.orange);
ft1=new Font("宋體",Font.BOLD,18);
jButton1.setFont(ft1);
jButton1.setText("按鈕一");
//#設(shè)定jButton2的外觀
jButton2.setBackground(Color.blue);
jButton2.setForeground(Color.red);
ft2=new Font("宋體",Font.PLAIN,18);
jButton2.setFont(ft2);
jButton2.setText("按鈕二");
jTextField1.setText("請(qǐng)壓一下按鈕");
//*****加入組件*****
this.getContentPane().add(panel1, BorderLayout.SOUTH);
//加panel1到ContentPane的南邊
panel1.add(jButton1, null);//Panel加入JButton1
panel1.add(jButton2, null);//Panel加入JButton2
this.getContentPane().add(jTextField1, BorderLayout.NORTH);
//加TextField1到ContentPane的北邊?
//#Button1的事件傾聽(tīng)#
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
//#Button2的事件傾聽(tīng)#
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
}
//#jButton1的按鈕事件#
void jButton1_actionPerformed(ActionEvent e) {
glassPane.setVisible(true);
}
//#jButton2的按鈕事件#
void jButton2_actionPerformed(ActionEvent e) {
glassPane.setVisible(false);
}
//*************初始設(shè)定*******************
}
//此類(lèi)繼承JPanel當(dāng)作glassPane
class MyGlassPane extends JPanel {
private JButton button;
private Point point= new Point(100,100),//繪圖起始點(diǎn)
previous,now;//
private String demoString ="JPanel作GlassPane,可設(shè)定DoubleBuffered()";
public MyGlassPane() {
this.setDoubleBuffered(true);
this.setOpaque(false);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
previous= e.getPoint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
now= e.getPoint();
point.x += now.x - previous.x;
point.y += now.y - previous.y;
repaint();
previous.x = now.x;
previous.y = now.y;
}
});
}
public void paintComponent(Graphics g) {
FontMetrics fm = g.getFontMetrics();
int dsw = fm.stringWidth(demoString);//取得字符串長(zhǎng)度
int dsh = fm.getHeight( );
int fmAscent = fm.getAscent();
g.drawRect(point.x, point.y, dsw + 15, dsh + 15);
g.drawString(demoString,point.x + 5, point.y + fmAscent + 5);
for(int i=50;i<=150;i+=10){
g.fillOval(point.x+i, point.y+i,30,30);}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -