62,614
社区成员
发帖
与我相关
我的任务
分享
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{
private JPanel buttonPanel;
private JButton yellowButton;
private JButton redButton;
private JButton blueButton;
public ButtonFrame(){
setTitle("ButtonTest");
setSize(300,200);
yellowButton = new JButton("Yellow");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
buttonPanel = new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
yellowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
buttonPanel.setBackground(Color.yellow);
}
});
redButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
buttonPanel.setBackground(Color.red);
}
});
blueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
buttonPanel.setBackground(Color.blue);
}
});
}
}
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 implements ActionListener{
private JPanel buttonPanel;
private JButton yellowButton;
private JButton redButton;
private JButton blueButton;
public ButtonFrame(){
setTitle("ButtonTest");
setSize(300,200);
yellowButton = new JButton("Yellow");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
buttonPanel = new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
yellowButton.addActionListener(this);
redButton.addActionListener(this);
blueButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if(eventSource == yellowButton){
buttonPanel.setBackground(Color.yellow);
}else if(eventSource == redButton){
buttonPanel.setBackground(Color.red);
}else if(eventSource == blueButton){
buttonPanel.setBackground(Color.blue);
}
}
}
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{
private JPanel buttonPanel;
private JButton yellowButton;
private JButton redButton;
private JButton blueButton;
public ButtonFrame(){
setTitle("ButtonTest");
setSize(300,200);
yellowButton = new JButton("Yellow");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
buttonPanel = new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
yellowButton.addActionListener(this);
redButton.addActionListener(this);
blueButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if(eventSource == yellowButton){
buttonPanel.setBackground(Color.yellow);
}else if(eventSource == redButton){
buttonPanel.setBackground(Color.red);
}else if(eventSource == blueButton){
buttonPanel.setBackground(Color.blue);
}
}
}