import java.awt.*;
import java.awt.event.*; import javax.swing.*;
publicclass Jframtest2 implements ActionListener{ staticfinalintWIDTH = 300; staticfinalintHEIGHT = 200; public Jframtest2() {
JFrame jf = new JFrame(\信息提示\); jf.setLayout(new GridLayout(3, 1)); jf.add(new Panel()); JPanel p = newJPanel(); JPanel p2 = newJPanel();
Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); // 获取屏幕的高度和宽度
int width = screenSize.width; int height = screenSize.height; int x = (width - WIDTH) / 2; int y = (height - HEIGHT) / 2; jf.setLocation(x, y);// 窗口居中显
JLabel jl = new JLabel(\你点击了登录按钮! \); JButton b1 = new JButton(\确定\);
b1.addActionListener(this);//监听按钮b1
p.setLocation(3, 1); p2.setLocation(3, 1);
p.add(jl,BorderLayout.CENTER);//将jl标签设置为中间显示 p2.add(b1);
jf.add(p); jf.add(p2);
jf.setResizable(false); jf.setVisible(true); jf.setSize(300, 200); jf.setSize(300, 200); }
publicvoid actionPerformed(ActionEvent e) { String comm = e.getActionCommand();
}
}
if( comm.equals(\确定\)) {
System.exit(0); }
当点击“取消”按钮时提示如下: import java.awt.*;
import java.awt.event.*; import javax.swing.*;
publicclass Jframtest3 implements ActionListener{ public Jframtest3(){ } }
JFrame jf = new JFrame(\信息提示\); jf.setLayout(new GridLayout(3, 1)); jf.add(new JPanel());
JLabel jl1 = new JLabel(\你点击了取消按钮!\); JButton b = new JButton(\确定\); b.addActionListener(this);
JPanel jp1 = newJPanel(); JPanel jp2 = newJPanel(); jp1.setLocation(3, 1);
jp2.setLocation(3, 1);//设置面板的位置
jp1.add(jl1,BorderLayout.CENTER);//面板jp1添加字符jl1,位置为中央 jp2.add(b);//面板jp2添加b按钮 jf.add(jp1);
jf.add(jp2);//将面板jp1,jp2添加到顶层容器jf
jf.setResizable(false); jf.setVisible(true); jf.setSize(300, 200);
jf.setLocation(300, 200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
publicvoid actionPerformed(ActionEvent e) { String comm = e.getActionCommand(); }
if( comm.equals(\确定\)) {
System.exit(0); }