JAVA的按纽代码的一点问题(BottonTest)如下
发布网友
发布时间:2022-05-29 13:05
我来回答
共2个回答
热心网友
时间:2023-10-19 05:14
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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("ButtonTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ButtonPanel panel=new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=200;
}
class ButtonPanel extends JPanel{
public ButtonPanel(){
JButton yellowButton=new JButton("Yellow");
JButton blueButton=new JButton("Blue");
JButton redButton=new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction=new ColorAction(Color.YELLOW,this);
ColorAction blueAction=new ColorAction(Color.BLUE,this);
ColorAction redAction=new ColorAction(Color.RED,this);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
}
class ColorAction implements ActionListener{
Color c;
ButtonPanel panel;
public ColorAction(Color c,ButtonPanel panel){
this.c=c;
this.panel=panel;
}
public void actionPerformed(ActionEvent event){
panel.setBackground(c);
}
}
热心网友
时间:2023-10-19 05:15
你先说说你的程序是实现什么功能的?