需要一段用java写的游戏登陆界面要与数据库连接的代码使用JDBC进行数据库的增删改查操作1.下载Microsoft SQL Server 2005 JDBC 驱动包jar文件 将jar文件引入工程中2.封装数据库链接的获取和关闭操作import java.sql.*;public class BaseDao {
/**
* 数据库驱动类的字符串,完整的包名加类名 在工程中查看添加的jar文件 能看到这个类
*/
private static final String DRIVE = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; /**
* 数据库连接地址
*
* DataBaseName=数据库名称 其它固定
*/
private static final String URL = "jdbc:sqlserver://localhost:1433;DataBaseName=bbs"; /**
* 连接数据库的用户名
*/
private static final String USER = "sa"; /**
* 用户密码
*/
private static final String PASSWORD = ""; /**
* 获取连接 异常直接抛出 或者捕获后自定义异常信息再抛出
*/
public static Connection getConnection() throws Exception {
Class.forName(DRIVE);
return DriverManager.getConnection(URL, USER, PASSWORD);
} /**
* 关闭与数据库的连接 释放资源
*/
public static void closeAll(ResultSet resultSet, PreparedStatement pst,
Connection connection) throws Exception {
if (resultSet != null)
resultSet.close();
if (pst != null)
pst.close();
if (connection != null)
connection.close();
}}3.创建图书的实体类public class Book {
/**
* 数据库主键
*/
private Long id; /**
* 作者
*/
private String author; /**
* 书名
*/
private String name;
/**
* 默认构造
*
*/
public Book() {
}
/**
* 全字段构造
* @param id
* @param author
* @param name
*/
public Book(Long id, String author, String name) {
this.id = id;
this.author = author;
this.name = name;
}
/**
* 以下为读写属性的方法
* @return
*/
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.创建与图书表交互的工具类import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;public class BookDao {
/**
* 添加新书
*
* @param book 要添加入数据库的图书 作者 书名 必须给定
*/
public void addBook(Book book) throws Exception {
// 连接
Connection connection = null;
// 执行语句
PreparedStatement pst = null;
try {
connection = BaseDao.getConnection();
// 构造执行语句
String sql = "insert into book values("book.getAuthor()","
book.getName()")";
pst = connection.prepareStatement(sql);
pst.executeUpdate(); } catch (Exception e) {
// 抛出异常
throw e;
} finally {
// 无论是否异常 均关闭数据库
BaseDao.closeAll(null, pst, connection);
}
} /**
* 查询所有书籍列表
*/
public ListBook getBooks() throws Exception {
// 用于存放查寻结果的集合
ListBook books = new ArrayListBook();
// 连接
Connection connection = null;
// 执行语句
PreparedStatement pst = null;
// 查询结果
ResultSet resultSet = null;
try {
connection = BaseDao.getConnection();
// 构造查询语句
String sql = "select * from book";
pst = connection.prepareStatement(sql);
resultSet = pst.executeQuery(); // 循环读取查询结果行
while (resultSet.next()) {
// getXXX的参数为数据表列名
Book book = new Book(resultSet.getLong("id"), resultSet
.getString("author"), resultSet.getString("name"));
// 将封装好的图书对象存入集合
books.add(book);
}
} catch (Exception e) {
// 抛出异常
throw e;
} finally {
// 无论是否异常 均关闭数据库
BaseDao.closeAll(resultSet, pst, connection);
}
// 返回查询结果
return books;
}/***其它方法类似上面 只是语句不同*/
}当然 以上只是简单的封装 初学者可以在理解以上代码的基础上 进行更高级的封装
5.使用BookDao添加书籍和获取所有书籍列表import java.util.List;/**
* 测试类
* @author Administrator
*
*/
public class Test { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//创建工具类对象
BookDao dao = new BookDao();
//创建一本图书
Book book = new Book(null,"QQ:495691293","编程菜鸟");
//添加书籍到数据库
dao.addBook(book);
//获取所有图书列表
ListBook books = dao.getBooks();
//输出结果
for (Book b : books) {
System.out.println(b.getId() "\t" b.getAuthor() "\t" b.getName());
}
}}
求java编写的登陆界面代码import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class LoginFrame {
private JFrame jFrame = null;//@jve:decl-index=0:visual-constraint="293,25"
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JButton jButton = null;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new Dimension(330, 222));
jFrame.setTitle("登陆");
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJPanel(), null);
}
return jContentPane;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(45, 76, 54, 22));
jLabel1.setText("密码:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(47, 39, 53, 25));
jLabel.setText("用户名:");
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(2, 7, 305, 167));
jPanel.add(jLabel, null);
jPanel.add(jLabel1, null);
jPanel.add(getJTextField(), null);
jPanel.add(getJTextField1(), null);
jPanel.add(getJButton(), null);
}
return jPanel;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(106, 40, 154, 24));
}
return jTextField;
}
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(105, 76, 154, 23));
}
return jTextField1;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(187, 103, 66, 25));
jButton.setText("登陆");
}
return jButton;
}
}
几个Java小游戏代码package reduce;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Vector;
public class Frame extends JFrame implements Runnable {
JPanel contentPane;
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JSlider jSlider1 = new JSlider();
JLabel jLabel1 = new JLabel();
JButton jButton2 = new JButton();
JLabel jLabel2 = new JLabel();
int count = 1, rapidity = 80; // count 当前进行的个数, rapidity 游标的位置
int zhengque = 0, cuowu = 0;
int rush[] = { 10 ,20 ,30 };//游戏每关的个数 可以自由添加.列 { 10 ,20 ,30 ,40,50}
int rush_count = 0;//记录关数
char list[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' };//随机出现的数字 可以自由添加
Vector number = new Vector();
String paiduan = "true";
AudioClip Musci_anjian, Music_shibai, Music_chenggong;
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//-----------------声音文件---------------------
Musci_anjian = Applet.newAudioClip(new File("sounds//anjian.wav")
.toURL());
Music_shibai = Applet.newAudioClip(new File("sounds//shibai.wav")
.toURL());
Music_chenggong = Applet.newAudioClip(new File(
"sounds//chenggong.wav").toURL());
//---------------------------------------
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(588, 530));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel1.setBounds(new Rectangle(4, 4, 573, 419));
jPanel1.setLayout(null);
jButton1.setBounds(new Rectangle(277, 442, 89, 31));
jButton1.setText("开始");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jSlider1.setBounds(new Rectangle(83, 448, 164, 21));
jSlider1.setMaximum(100);
jSlider1.setMinimum(1);
jSlider1.setValue(50);
jLabel1.setText("速度");
jLabel1.setBounds(new Rectangle(35, 451, 39, 18));
jButton2.setBounds(new Rectangle(408, 442, 89, 31));
jButton2.setText("结束");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jLabel2.setText("第一关:100个");
jLabel2.setBounds(new Rectangle(414, 473, 171, 21));
contentPane.add(jPanel1);
contentPane.add(jButton2);
contentPane.add(jButton1);
contentPane.add(jSlider1);
contentPane.add(jLabel1);
contentPane.add(jLabel2);
this.addKeyListener(new MyListener());
jButton1.addKeyListener(new MyListener());
jSlider1.addKeyListener(new MyListener());
jSlider1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
rapidity = jSlider1.getValue();
}
});
}
public void run() {
number.clear();
zhengque = 0;
cuowu = 0;
paiduan = "true";
while (count = rush[rush_count]) {
try {
Thread t = new Thread(new Tthread());
t.start();
count= 1;
Thread.sleep(1000(int) (Math.random() * 2000)); // 生产下组停顿时间
// 最快1快.最慢2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (true) { // 等待最后一个字符消失
if (number.size() == 0) {
break;
}
}
if (zhengque == 0) { // 为java游戏登录代码大全了以后相除..如果全部正确或者错误就会出现错误. 所以..
zhengque = 1;
}
if (cuowu == 0) {
cuowu = 1;
}
if (paiduan.equals("true")) { // 判断是否是自然结束
if (zhengque / cuowu = 2) {
JOptionPane.showMessageDialog(null, "恭喜你过关java游戏登录代码大全了");
rush_count= 1; // 自动加1关
if (rush_countrush.length) {
if (rapidity10) { // 当速度大于10的时候在-5提加速度.怕速度太快
rapidity -= 5; // 速度自动减10毫秒
jSlider1.setValue(rapidity); // 选择位置
}
Thread t = new Thread(this);
t.start();
} else {
JOptionPane.showMessageDialog(null, "牛B...你通关了..");
rush_count = 0;
count = 0;
}
} else {
JOptionPane.showMessageDialog(null, "请再接再励");
rush_count = 0;
count = 0;
}
} else {
rush_count = 0;
count = 0;
}
}
public void jButton1_actionPerformed(ActionEvent e) {
Thread t = new Thread(this);
t.start();
}
public void jButton2_actionPerformed(ActionEvent e) {
count = rush[rush_count]1;
paiduan = "flase";
}
class Tthread implements Runnable {
public void run() {
boolean fo = true;
int Y = 0, X = 0;
JLabel show = new JLabel();
show.setFont(new java.awt.Font("宋体", Font.PLAIN, 33));
jPanel1.add(show);
X = 10(int) (Math.random() * 400);
String parameter = list[(int) (Math.random() * list.length)]"";
Bean bean = new Bean();
bean.setParameter(parameter);
bean.setShow(show);
number.add(bean);
show.setText(parameter);
while (fo) {
// ---------------------数字下移--------------------
show.setBounds(new Rectangle(X, Y= 2, 33, 33));
try {
Thread.sleep(rapidity);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Y = 419) {
fo = false;
for (int i = number.size() - 1; i = 0; i--) {
Bean bn = ((Bean) number.get(i));
if (parameter.equalsIgnoreCase(bn.getParameter())) {
cuowu= 1;
jLabel2.setText("正确:"zhengque"个,错误:"cuowu
"个");
number.removeElementAt(i);
Music_shibai.play();
break;
}
}
}
}
}
}
class MyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
String uu = e.getKeyChar()"";
for (int i = 0; inumber.size(); i) {
Bean bean = ((Bean) number.get(i));
if (uu.equalsIgnoreCase(bean.getParameter())) {
zhengque= 1;
number.removeElementAt(i);
bean.getShow().setVisible(false);
jLabel2.setText("正确:"zhengque"个,错误:"cuowu"个");
Music_chenggong.play();
break;
}
}
Musci_anjian.play();
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
Frame frame = new Frame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.heightscreenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.widthscreenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
}
class Frame1_jButton2_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton2_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame adaptee;
Frame1_jButton1_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Bean {
String parameter = null;
JLabel show = null;
public JLabel getShow() {
return show;
}
public void setShow(JLabel show) {
this.show = show;
}
public String getParameter() {
return parameter;
}
public void setParameter(String parameter) {
this.parameter = parameter;
}
}
我只有一个打字母小游戏
用java写一个登录界面的代码,哪位大神会?。?谢谢 。import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Test26 {
public static void main(String[] args) {
final String userName = "abc";
final String passwrod = "123";
JFrame jFrame = new JFrame("登陆界面");
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth() - 200) / 2, ((int)dimension.getHeight() - 300) / 2, 200, 150);
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label1 = new JLabel("姓名");
label1.setBounds(10, 10, 100, 30);
jFrame.add(label1);
JLabel label2 = new JLabel("密码");
label2.setBounds(10, 40, 100, 30);
jFrame.add(label2);
final JTextField text1 = new JTextField();
text1.setBounds(50, 15, 130, 20);
jFrame.add(text1);
final JPasswordField text2 = new JPasswordField();
text2.setBounds(50, 45, 130, 20);
jFrame.add(text2);
JButton button = new JButton("Login");
button.setBounds(10, 75, 170, 40);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(userName.equals(text1.getText())passwrod.equals(text2.getText())) {
JOptionPane.showMessageDialog(null, "登陆成功误", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
}
});
jFrame.add(button);
jFrame.setVisible(true);
}
}
java游戏登录代码大全我有一个微信公众号java游戏登录代码大全,经常会分享一些Java技术相关的干货,还有一些学习资源 。
如果java游戏登录代码大全你喜欢我的分享,可以用微信搜索“Java团长”或者“javatuanzhang”关注 。
求JAVA实现用户登录界面代码?java游戏登录代码大全你要先学会截图哦,java游戏登录代码大全你发java游戏登录代码大全的看不清楚,重新写java游戏登录代码大全了一个你参考参考!
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Day30A extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel labelName,labelId,labelPass,labelMoney,labelSelect,labelCar;
private JComboBoxString jcb;
private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
private ButtonGroup btg;
private JRadioButton jr1,jr2;
Day30A(){
this.setTitle("注册账户");
this.setLayout(new GridLayout(7,1));
this.setSize(300,280);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
this.setVisible(true);
}
private void init() {
String str="卡片类型1,卡片类型2,卡片类型3,卡片类型4,卡片类型5";
jcb=new JComboBox(str.split(","));
labelId=new JLabel("账号: ");
labelName=new JLabel("姓名: ");
labelPass=new JLabel("密码: ");
labelMoney=new JLabel("开户金额:");
labelSelect=new JLabel("存款类型:");
labelCar=new JLabel("卡片类型:");
addFun1();
addFun2();
}
private void addFun2() {
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
}
private void addFun1() {
jp1=new JPanel();
jp1.add(labelId);
jp1.add(new JTextField(15));
jp2=new JPanel();
jp2.add(labelName);
jp2.add(new JTextField(15));
jp3=new JPanel();
jp3.add(labelPass);
jp3.add(new JTextField(15));
jp4=new JPanel();
jp4.add(labelMoney);
jp4.add(new JTextField(13));
jp5=new JPanel();
jp5.add(labelSelect);
btg=new ButtonGroup();
jr1=new JRadioButton("定期");
jr2=new JRadioButton("活期",true);
btg.add(jr1);
btg.add(jr2);
jp5.add(jr1);
jp5.add(jr2);
jp6=new JPanel();
jp6.add(labelCar);
jp6.add(jcb);
jp7=new JPanel();
jp7.add(new JButton("确定"));
jp7.add(new JButton("取消"));
}
public static void main(String[] args) {
new Day30A();
}
}
【java游戏登录代码大全 java登录界面代码简单】关于java游戏登录代码大全和java登录界面代码简单的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- redis跨IP,redis启动
- steam为什么占cpu,Steam为什么占用40gg
- 计算机工程毕业设计推荐,计算机科学系毕业设计
- 永康sap产品成本结构,sap生产成本核算基本逻辑
- 打游戏时老弹出网络,玩游戏怎么老是出现网络问题
- 真实火车模拟游戏,真实火车模拟手机版
- php文件中追加数据库 php添加数据库
- gis母线偏移,gis母线检修
- 酷狗音乐电脑怎么打开mv,电脑酷狗怎么打开播放界面