?? buttontest.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{ setTitle("Button Test");
setSize(DefualtX,DefualtY);
ButtonPanel bluepanel = new ButtonPanel(Color.BLUE,"BLUE");
ButtonPanel yellowpanel = new ButtonPanel(Color.YELLOW,"Yellow");
ButtonPanel greenpanel = new ButtonPanel(Color.GREEN,"Green");
ButtonPanel redpanel = new ButtonPanel(Color.RED,"Red");
ButtonPanel exitpanel = new ButtonPanel("Exit");
add(bluepanel, BorderLayout.NORTH);
add(yellowpanel, BorderLayout.WEST);
add(greenpanel, BorderLayout.EAST);
add(redpanel, BorderLayout.SOUTH);
add(exitpanel, BorderLayout.CENTER);
}
public int DefualtX = 300;
public int DefualtY = 150;
}
class ButtonPanel extends JPanel
{
public ButtonPanel(Color color, String strs)
{
JButton colorButton = new JButton(strs);
add(colorButton);
colorButton.addActionListener(new ColorAction(color));
}
public ButtonPanel(String strs)
{ JButton exitButton = new JButton(strs);
add(exitButton);
exitButton.addActionListener(new ExitAction());
}
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{ backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{ setBackground(backgroundColor);
JButton source = (JButton)event.getSource();
source.setText("Exit");
source.addActionListener(new ExitAction());
}
private Color backgroundColor;
}
private class ExitAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{ System.exit(0);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -