java创建一个面板窗口_Java 创建一个窗口,使其启动时位于屏幕中间

import java.awt.Toolkit;

import javax.swing.JFrame;

public class WindowInTheMiddle extends JFrame {

public static void main(String[] args) {

WindowInTheMiddle window = new WindowInTheMiddle();

window.launchFrame();

}

public void launchFrame() {

// The width & Height of the window

final int formWidth = 500;

final int formHeight = 500;

// The width & Height of the screen

int screenWidth = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();

int screenHeight = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();

// Set title, size, location & visible

this.setTitle("Hello World! I'm coming!");

this.setSize(formWidth, formHeight);

this.setLocation((screenWidth-formWidth)/2, (screenHeight-formHeight)/2);

this.setVi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java Swing创建一个简单的登录窗口的示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LoginWindow extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; public LoginWindow() { setTitle("登录"); setSize(300, 150); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); // 创建面板 JPanel panel = new JPanel(new GridLayout(3, 2)); // 添加组件 JLabel usernameLabel = new JLabel("用户名:"); panel.add(usernameLabel); usernameField = new JTextField(); panel.add(usernameField); JLabel passwordLabel = new JLabel("密码:"); panel.add(passwordLabel); passwordField = new JPasswordField(); panel.add(passwordField); JButton loginButton = new JButton("登录"); loginButton.addActionListener(this); panel.add(loginButton); JButton cancelButton = new JButton("取消"); cancelButton.addActionListener(this); panel.add(cancelButton); // 添加面板窗口 add(panel); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("登录")) { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); // TODO: 检查用户名和密码是否正确 } else if (e.getActionCommand().equals("取消")) { dispose(); } } public static void main(String[] args) { new LoginWindow(); } } ``` 这个窗口包含一个文本框和一个密码框,以及两个按钮:一个用于登录,一个用于取消。当用户单击登录按钮时,程序将获取文本框和密码框中的值,并执行某些操作(例如检查用户名和密码是否正确)。如果用户单击取消按钮,程序将关闭窗口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值