项目任务:显示Swing一个窗口
1.项目实施
package p07;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame {
private JButton btnLogin;
public LoginFrame(String title){
super(title);
initGUI();
eventHandling();
}
private void eventHandling() {
btnLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "哈哈,今天立夏,欢迎登录~");
}
});
}
private void initGUI(){
btnLogin = new JButton("登录");
btnLogin.setFont(new Font("楷体",Font.PLAIN,20));
JPanel panel = (JPanel) this.getContentPane();
panel.setLayout(new FlowLayout());
panel.add(btnLogin);
this.setVisible(true);
this.setBounds(200,200,400,300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String [] args){
new LoginFrame("用户登录");
}
}
2.运行编程结果
- 结束窗口运行则结果显示