再在上面的窗口里面,放一个登录注册面板,用代码呈现
时间: 2024-03-13 16:47:07 浏览: 28
好的,下面是在GameWin类中添加一个登录注册面板的代码:
```
package xjdz;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameWin extends JFrame {
private static final int WIDTH = 999;
private static final int HEIGHT = 666;
private static final String TITLE = "oqcw星际大战仿飞机大战小游戏";
private static final String BG_IMAGE_PATH = "src/bg.jpg";
private JTextField usernameField;
private JPasswordField passwordField;
public GameWin() {
initWindow();
initLoginPanel();
}
private void initWindow() {
// 设置窗口大小、位置、标题、背景等属性
this.setSize(WIDTH, HEIGHT);
this.setLocationRelativeTo(null);
this.setTitle(TITLE);
this.setResizable(false);
this.setContentPane(createBackgroundPanel());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
dispose();
}
});
}
private JPanel createBackgroundPanel() {
JPanel bgPanel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon = new ImageIcon(BG_IMAGE_PATH);
Image img = icon.getImage();
g.drawImage(img, 0, 0, getWidth(), getHeight(), icon.getImageObserver());
}
};
bgPanel.setLayout(null);
bgPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
return bgPanel;
}
private void initLoginPanel() {
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.Y_AXIS));
loginPanel.setOpaque(false);
JLabel title = new JLabel("请登录");
title.setAlignmentX(Component.CENTER_ALIGNMENT);
title.setFont(new Font("宋体", Font.BOLD, 24));
title.setBorder(BorderFactory.createEmptyBorder(30, 0, 30, 0));
loginPanel.add(title);
JPanel usernamePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JLabel usernameLabel = new JLabel("用户名:");
usernameField = new JTextField(20);
usernamePanel.add(usernameLabel);
usernamePanel.add(usernameField);
loginPanel.add(usernamePanel);
JPanel passwordPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JLabel passwordLabel = new JLabel("密 码:");
passwordField = new JPasswordField(20);
passwordPanel.add(passwordLabel);
passwordPanel.add(passwordField);
loginPanel.add(passwordPanel);
JButton loginButton = new JButton("登录");
loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 处理登录逻辑
}
});
loginPanel.add(loginButton);
JButton registerButton = new JButton("注册");
registerButton.setAlignmentX(Component.CENTER_ALIGNMENT);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 处理注册逻辑
}
});
loginPanel.add(registerButton);
JPanel wrapper = new JPanel(new GridBagLayout());
wrapper.setOpaque(false);
wrapper.add(loginPanel);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
getContentPane().add(wrapper, gbc);
}
public void start() {
this.setVisible(true);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.start();
}
}
```
在这段代码中,我们添加了一个initLoginPanel方法,用于初始化登录注册面板。该面板使用了BoxLayout和GridBagLayout来进行布局,并且使用了JLabel、JTextField、JPasswordField、JButton等Swing组件来呈现。同时,我们还为登录、注册按钮添加了ActionListener,以处理具体的登录、注册逻辑。最后,我们将登录注册面板添加到了窗口的content pane中,以便在窗口中显示。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)