编程语言
首页 > 编程语言> > Java-Work11-图形界面设计1(1)

Java-Work11-图形界面设计1(1)

作者:互联网

题目要求

简单界面设计

  1. 创建图形界面
  2. 在图形界面上添加组件
  3. 为控件添加触发事件
  4. 不同界面间连接

程序难点

  1. 设置背景颜色
  2. 给控件添加监听
  3. 重写监听事件

题目代码

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);
	}
}

标签:Work11,Java,图形界面,swing,new,add,butOk,panel1,import
来源: https://www.cnblogs.com/Conan-jine/p/15853984.html