java GUI问题
来源:百度知道 编辑:UC知道 时间:2024/07/03 03:15:22
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonSwing {
public static void main(String[] args)
{
ButtonFrame bf=new ButtonFrame();
}
}
class ButtonFrame extends JFrame implements ActionListener{
JButton b1=new JButton("红色");
JButton b2=new JButton("黄色");
JButton b3=new JButton("蓝色");
b1.addActionListener(this);
b2.addActionListener(this);
b2.addActionListener(this);
ButtonFrame()
{
super("test");
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
pack();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b2)
setBackground(Color.yellow);
if(e.getSource()==b3)
setBackground(Color.blue);
if(e.getSource()==b1)
set
具体修改见其中注释。还有不懂的话,可以加我,我详细给你讲!^_^
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonSwing {
public static void main(String[] args)
{
ButtonFrame bf=new ButtonFrame();
//==================
// 新建JFrame之后,要将其设置为“可见的”才会在屏幕上显示出来
bf.setVisible(true);
//===================
}
}
class ButtonFrame extends JFrame implements ActionListener{
JButton b1=new JButton("红色");
JButton b2=new JButton("黄色");
JButton b3=new JButton("蓝色");
//b1.addActionListener(this);
//b2.addActionListener(this);
// b2.addActionListener(this);
ButtonFrame()
{
super("test");
setLayout(new FlowLayout());
//===================
//这个要移到函数里面
b1.addActionListener(this);
b2.addActionListener(this);
b2.addActionListener(this);
//=====================
add(b1);
add