s
o
u
l
s
j
i
e

javaGUI 最基础的界面 标签、输入框、按钮、事件处理 模板

//GUI界面
package CallName;
import java.awt.Toolkit;
import java.awt.event.*;

import javax.swing.*;

public class CallNamePage extends JFrame {
	JTextField txt11 = new JTextField();
	JButton btn = new JButton("按钮");

	public CallNamePage() {
		super("标题");
		JLabel lab = new JLabel("标签:");
		lab.setBounds(50, 40, 40, 40);
		txt11.setBounds(90, 40, 40, 40);
		btn.setBounds(130, 40, 80, 40);

		//事件
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ee) {
				JOptionPane.showMessageDialog(null, "事件");
			}
		});
		// 将控件添加到容器
		JPanel p = new JPanel();
		p.setLayout(null);
		// 布局标题
		p.add(lab);
		p.add(txt11);
		p.add(btn);
		getContentPane().add(p);
		setSize(400, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

	}

	// 程序入口
	public static void main(String[] args) {
		CallNamePage s = new CallNamePage();
		s.CenterPanel();
	}

	// 将界面开始位置显示到屏幕中间
	public void CenterPanel() {
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		this.setLocation(width / 2, height / 4);
	}

}

  

posted @ 2022-06-06 09:04  soulsjie  阅读(282)  评论(0编辑  收藏  举报
你累吗?累就对了,当你觉得累时证明你在走上坡路!-----NotFoundObject - 2016-12-14 08:43