再在上面的窗口里面,放一个登录注册面板,用代码呈现
时间: 2024-03-13 17:47:07 浏览: 162
java代码-使用java制作登录与注册的窗口的源代码
好的,下面是在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中,以便在窗口中显示。
阅读全文
相关推荐
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?php
session_start();
//注销登录
if(@$_GET['action'] == "logout"){
unset($_SESSION['userid']);
unset($_SESSION['username']);
echo '注销登录成功!点击此处 登录';
exit;
}
if(!isset($_POST['submit'])){
exit('非法访问!');
}
$username = htmlspecialchars($_POST['username']);
$password = MD5($_POST['password']);
//包含数据库连接文件
include('conn.php');
//检测用户名及密码是否正确
$check_query = mysql_query("select uid from user where username='$username' and password='$password' limit 1");
if($result = mysql_fetch_array($check_query)){
//登录成功
$_SESSION['username'] = $username;
$_SESSION['userid'] = $result['uid'];
echo $username,' 欢迎你!进入 用户中心
'; echo '点击此处 注销 登录!
'; exit; } else { exit('登录失败!点击此处 返回 重试'); } ?> </body> </html>
'; echo '点击此处 注销 登录!
'; exit; } else { exit('登录失败!点击此处 返回 重试'); } ?> </body> </html>