?? buttondrawtest.java
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonDrawTest
{
public static void main(String[] args)
{
DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class DrawFrame extends JFrame
{
public DrawFrame()
{
Toolkit kit=Toolkit.getDefaultToolkit();
Image img = kit.getImage("icon.gif");
setIconImage(img);
setTitle("ButtonDraw");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
DrawPanel panel=new DrawPanel();
Container content=getContentPane();
content.add(panel);
}
public static final int DEFAULT_WIDTH=400;
public static final int DEFAULT_HEIGHT=400;
}
class DrawPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("press the button ",MESSAGE_X,MESSAGE_Y);
}
public DrawPanel()
{
JButton not=new JButton("not");
JButton hello=new JButton("hello");
JButton world=new JButton("world");
add(not);
add(hello);
add(world);
DrawAction notAction=new DrawAction("not",70,100);
DrawAction helloAction=new DrawAction("hello",170,100);
DrawAction worldAction=new DrawAction("world",250,100);
hello.addActionListener(helloAction);
world.addActionListener(worldAction);
not.addActionListener(notAction);
}
private class DrawAction implements ActionListener
{
public DrawAction(String s ,int leftX,int topY)
{
x=leftX;
y=topY;
string=s;
}
public void actionPerformed(ActionEvent event)
{
Graphics g=getGraphics();
g.drawString(string,x,y);
}
private int x;
private int y;
private String string;
}
private int MESSAGE_X=200;
private int MESSAGE_Y=200;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -