import javax.swing.*;import java.awt.*;import java.util.Random;public class TicketSeller { private JFrame mainFrame; private JPanel panel; private JProgressBar progressBar1; private JProgressBar progressBar2; private JLabel label1; private JLabel label2; private JButton startButton; private int totalTickets = 108; private int ticketSold1 = 0; private int ticketSold2 = 0; public TicketSeller() { prepareGUI(); addComponents(); } private void prepareGUI() { mainFrame = new JFrame("Ticket Seller"); mainFrame.setSize(400, 200); mainFrame.setLayout(new GridLayout(2, 1)); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void addComponents() { panel = new JPanel(new GridLayout(2, 2)); label1 = new JLabel("LYH_1_108_1"); progressBar1 = new JProgressBar(0, totalTickets); progressBar1.setValue(0); panel.add(label1); panel.add(progressBar1); label2 = new JLabel("LYH_1_108_2"); progressBar2 = new JProgressBar(0, totalTickets); progressBar2.setValue(0); panel.add(label2); panel.add(progressBar2); mainFrame.add(panel); startButton = new JButton("Start"); startButton.addActionListener(e -> { Thread thread1 = new Thread(() -> sellTickets(1)); Thread thread2 = new Thread(() -> sellTickets(2)); thread1.start(); thread2.start(); }); mainFrame.add(startButton); mainFrame.setVisible(true); } private void sellTickets(int windowNum) { while (ticketSold1 + ticketSold2 < totalTickets) { int ticketSold = new Random().nextInt(5) + 1; if (windowNum == 1) { if (ticketSold1 + ticketSold <= totalTickets) { ticketSold1 += ticketSold; progressBar1.setValue(ticketSold1); } } else { if (ticketSold2 + ticketSold <= totalTickets) { ticketSold2 += ticketSold; progressBar2.setValue(ticketSold2); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { new TicketSeller(); }}
时间: 2024-01-15 13:03:15 浏览: 194
这段代码是一个简单的模拟售票程序,它创建了一个窗口,包含两个进度条和一个“开始”按钮。总共有 108 张票,两个窗口分别出售随机数量的票,直到所有票都售出为止。
要显示已售票的份数,可以在程序中添加一个标签或文本框,每次销售后更新它的文本。例如,在 `sellTickets()` 方法中,可以添加以下代码:
```java
if (windowNum == 1) {
if (ticketSold1 + ticketSold <= totalTickets) {
ticketSold1 += ticketSold;
progressBar1.setValue(ticketSold1);
label1.setText("LYH_1_108_1: " + ticketSold1 + " sold");
}
} else {
if (ticketSold2 + ticketSold <= totalTickets) {
ticketSold2 += ticketSold;
progressBar2.setValue(ticketSold2);
label2.setText("LYH_1_108_2: " + ticketSold2 + " sold");
}
}
```
这将在售票时更新标签的文本,以显示已售票的份数。
相关问题
在util包中写DbUtil类:定义方法,实现数据库的连接和关闭。 5.在model包中创建用户实体类User 。 包含id、userName、userPwd成员变量;构造方法; getXxx()、setXxx()方法 6.在view包中添加登录窗体类LoginFrame(包含mian方法),MainFrame窗体类。 7.在dao包中: 创建UserDao类,定义添加数据的方法,public int userAdd(Connection con,User user)(在main方法中调用该方法实现向数据库中插入数据); 创建LoginDao类,通过用户名和密码查询用户的方法,public static User login(Connection con,User user) 。 8. 为LoginFrame登录窗体的“登录”按钮和“注册”按钮添加监听器并处理单击事件。登录时,输入正确的用户名和密码,弹出MainFrame窗体;输入错误,弹出提示框。9.为Register注册窗体的“确定”和“取消”按钮添加监听器并处理单击事件。注册成功弹出消息提示框,两次密码不一致,弹出提示框。点击“取消”按钮,textField内容清空,可重新输入。给出java代码
抱歉,我是一名自然语言处理模型,无法提供完整的Java代码。但是,我可以给你提供一些思路和代码片段。
首先,我们需要在util包中创建一个DbUtil类,用于连接和关闭数据库:
```java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbUtil {
// 数据库连接信息
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root";
private static final String PASSWORD = "123456";
// 获取数据库连接
public static Connection getConnection() throws SQLException {
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
return conn;
}
// 关闭数据库连接
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
接着,在model包中创建一个User类,用于表示用户实体:
```java
package model;
public class User {
private int id;
private String userName;
private String userPwd;
public User() {}
public User(int id, String userName, String userPwd) {
this.id = id;
this.userName = userName;
this.userPwd = userPwd;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
}
```
然后,在dao包中创建一个UserDao类,用于实现用户数据的添加:
```java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.User;
import util.DbUtil;
public class UserDao {
// 添加用户数据
public static int userAdd(Connection con, User user) throws SQLException {
String sql = "insert into user (id,userName,userPwd) values (?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, user.getId());
pstmt.setString(2, user.getUserName());
pstmt.setString(3, user.getUserPwd());
int result = pstmt.executeUpdate();
pstmt.close();
return result;
}
}
```
接着,在dao包中创建一个LoginDao类,用于实现用户的登录:
```java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.User;
import util.DbUtil;
public class LoginDao {
// 登录验证
public static User login(Connection con, User user) throws SQLException {
User resultUser = null;
String sql = "select * from user where userName=? and userPwd=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getUserPwd());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
resultUser = new User();
resultUser.setId(rs.getInt("id"));
resultUser.setUserName(rs.getString("userName"));
resultUser.setUserPwd(rs.getString("userPwd"));
}
rs.close();
pstmt.close();
return resultUser;
}
}
```
接着,在view包中创建一个LoginFrame类,用于实现登录窗体:
```java
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import dao.LoginDao;
import model.User;
import util.DbUtil;
public class LoginFrame extends JFrame {
private JPanel contentPane;
private JTextField userNameField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrame frame = new LoginFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("用户名:");
lblNewLabel.setBounds(81, 78, 54, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("密码:");
lblNewLabel_1.setBounds(81, 123, 54, 15);
contentPane.add(lblNewLabel_1);
userNameField = new JTextField();
userNameField.setBounds(145, 75, 156, 21);
contentPane.add(userNameField);
userNameField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(145, 120, 156, 21);
contentPane.add(passwordField);
JButton loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName = userNameField.getText();
String userPwd = new String(passwordField.getPassword());
User user = new User();
user.setUserName(userName);
user.setUserPwd(userPwd);
Connection conn = null;
try {
conn = DbUtil.getConnection();
User resultUser = LoginDao.login(conn, user);
if (resultUser != null) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
dispose();
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
DbUtil.closeConnection(conn);
}
}
});
loginButton.setBounds(81, 185, 93, 23);
contentPane.add(loginButton);
JButton registerButton = new JButton("注册");
registerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
RegisterFrame frame = new RegisterFrame();
frame.setVisible(true);
dispose();
}
});
registerButton.setBounds(208, 185, 93, 23);
contentPane.add(registerButton);
}
}
```
接着,在view包中创建一个MainFrame类,用于实现主窗体:
```java
package view;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class MainFrame extends JFrame {
private JPanel contentPane;
/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
}
}
```
最后,在view包中创建一个RegisterFrame类,用于实现注册窗体:
```java
package view;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import dao.UserDao;
import model.User;
import util.DbUtil;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class RegisterFrame extends JFrame {
private JPanel contentPane;
private JTextField userNameField;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RegisterFrame frame = new RegisterFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RegisterFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("用户名:");
lblNewLabel.setBounds(81, 56, 54, 15);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("密码:");
lblNewLabel_1.setBounds(81, 101, 54, 15);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("确认密码:");
lblNewLabel_2.setBounds(81, 146, 68, 15);
contentPane.add(lblNewLabel_2);
userNameField = new JTextField();
userNameField.setBounds(145, 53, 156, 21);
contentPane.add(userNameField);
userNameField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(145, 98, 156, 21);
contentPane.add(passwordField);
confirmPasswordField = new JPasswordField();
confirmPasswordField.setBounds(145, 143, 156, 21);
contentPane.add(confirmPasswordField);
JButton okButton = new JButton("确定");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName = userNameField.getText();
String userPwd = new String(passwordField.getPassword());
String confirmPassword = new String(confirmPasswordField.getPassword());
if (userPwd.equals(confirmPassword)) {
User user = new User();
user.setId(1);
user.setUserName(userName);
user.setUserPwd(userPwd);
Connection conn = null;
try {
conn = DbUtil.getConnection();
int result = UserDao.userAdd(conn, user);
if (result > 0) {
JOptionPane.showMessageDialog(null, "注册成功");
dispose();
LoginFrame frame = new LoginFrame();
frame.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "注册失败");
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
DbUtil.closeConnection(conn);
}
} else {
JOptionPane.showMessageDialog(null, "两次密码不一致");
}
}
});
okButton.setBounds(81, 191, 93, 23);
contentPane.add(okButton);
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
userNameField.setText("");
passwordField.setText("");
confirmPasswordField.setText("");
}
});
cancelButton.setBounds(208, 191, 93, 23);
contentPane.add(cancelButton);
}
}
```
以上是一个简单的基于Java Swing和MySQL的用户登录和注册系统的实现。
给我一个使用了Java swing的扫雷游戏Java源码
好的,下面是一个使用Java Swing实现的扫雷游戏的源码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class MineSweeper implements ActionListener {
private JFrame mainFrame;
private JPanel mainPanel, buttonPanel;
private JButton[][] buttonArray;
private int[][] mineArray;
private boolean[][] clickedArray;
private int size, numMines;
private Random random;
public MineSweeper(int size, int numMines) {
this.size = size;
this.numMines = numMines;
this.random = new Random();
this.mineArray = new int[size][size];
this.clickedArray = new boolean[size][size];
mainFrame = new JFrame("Mine Sweeper");
mainPanel = new JPanel();
buttonPanel = new JPanel();
buttonArray = new JButton[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
buttonArray[i][j] = new JButton();
buttonArray[i][j].setPreferredSize(new Dimension(25, 25));
buttonArray[i][j].addActionListener(this);
buttonPanel.add(buttonArray[i][j]);
}
}
mainPanel.add(buttonPanel);
mainFrame.add(mainPanel);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setVisible(true);
generateMines(numMines);
}
private void generateMines(int numMines) {
int count = 0;
while (count < numMines) {
int x = random.nextInt(size);
int y = random.nextInt(size);
if (mineArray[x][y] == 0) {
mineArray[x][y] = 1;
count++;
}
}
}
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (event.getSource() == buttonArray[i][j]) {
if (mineArray[i][j] == 1) {
buttonArray[i][j].setText("M");
JOptionPane.showMessageDialog(mainFrame, "You lose!");
System.exit(0);
} else {
int count = countMines(i, j);
buttonArray[i][j].setText("" + count);
clickedArray[i][j] = true;
if (count == 0) {
expand(i, j);
}
if (checkWin()) {
JOptionPane.showMessageDialog(mainFrame, "You win!");
System.exit(0);
}
}
}
}
}
}
private int countMines(int x, int y) {
int count = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < size && j >= 0 && j < size) {
if (mineArray[i][j] == 1) {
count++;
}
}
}
}
return count;
}
private void expand(int x, int y) {
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 0 && i < size && j >= 0 && j < size) {
if (!clickedArray[i][j]) {
int count = countMines(i, j);
buttonArray[i][j].setText("" + count);
clickedArray[i][j] = true;
if (count == 0) {
expand(i, j);
}
}
}
}
}
}
private boolean checkWin() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (!clickedArray[i][j]) {
if (mineArray[i][j] != 1) {
return false;
}
}
}
}
return true;
}
public static void main(String[] args) {
new MineSweeper(10, 10);
}
}
```
这个扫雷游戏的源码使用了Java Swing库来实现图形界面,使用了JButton来表示每个格子,使用了JOptionPane来弹出游戏胜利或失败的提示框。游戏开始时,程序会生成一个size * size的二维数组,其中1表示雷,0表示非雷。玩家需要点击每个格子来揭示其内容,如果揭示到了雷则游戏失败,如果揭示到的格子周围有雷,则格子上会显示雷的数量。当玩家已经揭示了所有非雷格子时,游戏胜利。
阅读全文