题目要求
简单界面设计
- 创建图形界面
- 在图形界面上添加组件
- 为控件添加触发事件
- 不同界面间连接
程序难点
- 设置背景颜色
- 给控件添加监听
- 重写监听事件
题目代码
package work11;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Work11_1_3 extends JFrame implements ActionListener
{
JButton butOk, butQuit;
JPanel panel1, panel2;
JLabel label;
public Work11_1_3()
{
setTitle("窗体");
setLocation(250, 250);
setSize(300, 300);
panel1 = new JPanel();
panel1.setBackground(Color.yellow);
butOk = new JButton("Ok");
butQuit = new JButton("Quit");
butOk.addActionListener(this);
butQuit.addActionListener(this);
panel1.add(butOk);
panel1.add(butQuit);
panel2 = new JPanel();
panel2.setBackground(Color.green);
label = new JLabel("文本框");
panel2.add(label);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == butOk)
JOptionPane.showMessageDialog(null, "test!");
else if(e.getSource() == butQuit)
System.exit(0);
}
public static void main(String[] args)
{
Work11_1_3 frame = new Work11_1_3();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}