很好,小二,再在上面窗口中间,放入一个登录注册面板,请用代码呈现
时间: 2024-03-13 18:47:40 浏览: 68
好的,为了在窗口中间放入登录注册面板,可以使用JPanel作为容器,然后将登录和注册的表单放在这个容器中。以下是示例代码:
```java
package xjdz;
import javax.swing.*;
import java.awt.*;
public class GameWin extends JFrame {
private static final int DEFAULT_WIDTH = 999;
private static final int DEFAULT_HEIGHT = 666;
private static final String DEFAULT_TITLE = "oqcw星际大战仿飞机大战小游戏";
private static final boolean DEFAULT_RESIZABLE = false;
private JLabel backgroundImageLabel;
public GameWin(String imagePath) {
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocationRelativeTo(null);
setTitle(DEFAULT_TITLE);
setResizable(DEFAULT_RESIZABLE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addBackgroundImage(imagePath);
addLoginPanel();
showWindow();
}
private void addBackgroundImage(String imagePath) {
backgroundImageLabel = new JLabel(new ImageIcon(imagePath));
backgroundImageLabel.setBounds(0, 0, getWidth(), getHeight());
getLayeredPane().add(backgroundImageLabel, new Integer(0));
}
private void addLoginPanel() {
JPanel loginPanel = new JPanel(new GridLayout(2, 1)); // 创建一个网格布局的面板,用于容纳登录和注册表单
JTextField usernameField = new JTextField(20);
JPasswordField passwordField = new JPasswordField(20);
JButton loginButton = new JButton("登录");
JButton registerButton = new JButton("注册");
loginPanel.add(usernameField);
loginPanel.add(passwordField);
loginPanel.add(loginButton);
loginPanel.add(registerButton);
// 将登录注册面板添加到窗口中央
add(loginPanel, BorderLayout.CENTER);
}
private void showWindow() {
setVisible(true);
}
public static void main(String[] args) {
GameWin gameWin = new GameWin("src/bg.jpg");
}
}
```
这段代码中,我们创建了一个JPanel容器loginPanel,并将其布局设置为2行1列的网格布局。然后,我们创建了一个JTextField用于输入用户名,一个JPasswordField用于输入密码,以及两个JButton分别用于登录和注册。将这些组件添加到loginPanel中,然后将loginPanel添加到窗口中央,就可以在窗口中央放入登录注册面板了。
阅读全文
相关推荐
<!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>