package project; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import javax.swing.*; public class AddBookDialog extends JDialog{ final int WIDTH=400; final int HEIGHT=300; public AddBookDialog(JFrame jf,String title,boolean isModel) { super(jf,title,isModel); this.setBounds(HEIGHT, HEIGHT, WIDTH, HEIGHT); Box vBox=Box.createVerticalBox(); Box numBox=Box.createHorizontalBox(); JLabel numLabel=new JLabel("学号:"); JTextField numField=new JTextField(15); numBox.add(numLabel); numBox.add(Box.createHorizontalStrut(20)); numBox.add(numField); Box nameBox=Box.createHorizontalBox(); JLabel nameLabel=new JLabel("姓名:"); JTextField nameField=new JTextField(15); nameBox.add(nameLabel); nameBox.add(Box.createHorizontalStrut(20)); nameBox.add(nameField); Box sexBox=Box.createHorizontalBox(); JLabel sexLabel=new JLabel("性别:"); JTextField sexField=new JTextField(15); sexBox.add(sexLabel); sexBox.add(Box.createHorizontalStrut(20)); sexBox.add(sexField); Box ageBox=Box.createHorizontalBox(); JLabel ageLabel=new JLabel("年龄:"); JTextField ageField=new JTextField(15); ageBox.add(ageLabel); ageBox.add(Box.createHorizontalStrut(20)); ageBox.add(ageField); Box teBox=Box.createHorizontalBox(); JLabel teLabel=new JLabel("电话:"); JTextField teField=new JTextField(15); teBox.add(teLabel); teBox.add(Box.createHorizontalStrut(20)); teBox.add(teField); Box idBox=Box.createHorizontalBox(); JLabel idLabel=new JLabel("房间号:"); JTextField idField=new JTextField(15); idBox.add(idLabel); idBox.add(Box.createHorizontalStrut(20)); idBox.add(idField); Box btnBox=的问题
时间: 2024-04-26 12:21:08 浏览: 110
这段代码是用来创建一个添加图书信息的对话框。它包含了学号、姓名、性别、年龄、电话和房间号等信息的输入框,以及一个“确定”按钮和一个“取消”按钮。当用户点击“确定”按钮时,程序将会把输入的信息保存到数据库中。这个对话框是基于 Java Swing 实现的,它继承了 JDialog 类,并在构造函数中设置了对话框的大小和布局。在对话框中,使用了多个 Box 来实现信息输入框的布局,以及一个 JPanel 来包含“确定”和“取消”按钮。当用户点击“确定”按钮时,程序会调用相应的方法将信息保存到数据库中。
相关问题
优化这段代码package com.daiinfo.catering.pane; import com.daiinfo.catering.util.JDBConnection; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.EventListener; public class DishManagePane2 extends JFrame { private JTable table; private JButton addButton,updateButton,deleteButton; public DishManagePane2(){ setTitle("菜品管理"); setSize(800,600); String [] columnNames = {"菜品名","价格","菜品类别"}; Object[][] data = {{"糖醋排骨",29.0,"西餐"},{"宫保鸡丁",28.0,"中餐"}}; JTable table = new JTable(data,columnNames); addButton = new JButton("增加"); updateButton = new JButton("修改"); deleteButton = new JButton("删除"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); JPanel panel = new JPanel(new BorderLayout()); panel.add(new JScrollPane(table),BorderLayout.CENTER); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(addButton); buttonPanel.add(updateButton); buttonPanel.add(deleteButton); panel.add(buttonPanel,BorderLayout.SOUTH); add(panel); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JDBConnection(); new DishManagePane2(); } }
以下是对代码的优化:
1. 将 JTable、JButton 声明为类成员变量,方便后面的方法访问。
2. 将 JTable 中的数据从构造函数中移除,改为从数据库中读取,使用 JDBConnection 类进行数据库连接和查询。
3. 将按钮的 ActionListener 单独提取出来,作为类内部的一个私有方法。
4. 将 JFrame 窗口的设置移到单独的方法中,方便对窗口属性的修改和扩展。
优化后的代码如下:
```java
package com.daiinfo.catering.pane;
import com.daiinfo.catering.util.JDBConnection;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class DishManagePane2 extends JFrame {
private JTable table;
private JButton addButton, updateButton, deleteButton;
public DishManagePane2() {
setTitle("菜品管理");
setSize(800, 600);
setWindow();
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(table), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
add(panel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setWindow() {
addButton = new JButton("增加");
updateButton = new JButton("修改");
deleteButton = new JButton("删除");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 添加菜品的逻辑
}
});
// 从数据库中读取数据并绑定到 JTable 上
try {
Connection conn = JDBConnection.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from dishes";
ResultSet rs = stmt.executeQuery(sql);
Vector<Vector<Object>> data = new Vector<>();
while (rs.next()) {
Vector<Object> row = new Vector<>();
row.add(rs.getString("name"));
row.add(rs.getDouble("price"));
row.add(rs.getString("category"));
data.add(row);
}
Vector<String> columnNames = new Vector<>();
columnNames.add("菜品名");
columnNames.add("价格");
columnNames.add("菜品类别");
table = new JTable(data, columnNames);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new JDBConnection();
new DishManagePane2();
}
}
```
这样,代码就更加清晰、易于维护和扩展了。
package package06; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.; import javax.swing.*; public class Java6 extends JFrame { JLabel wenzi; JTextField shuru; JButton chaxun; JTextArea fanhui; public Java6(){ super("中英查询软件"); // 窗口添加pNorth 在北面区域 add(getNorthPanel(),BorderLayout.NORTH); } private JPanel getNorthPanel() { JPanel panel = new JPanel(); wenzi = new JLabel("请输入要查询的单词:"); shuru=new JTextField(8); chaxun=new JButton("查询"); fanhui =new JTextArea(6,25); panel.add(wenzi,new FlowLayout()); panel.add(shuru,new FlowLayout()); panel.add(chaxun,new FlowLayout()); panel.add(fanhui,new FlowLayout()); return panel; } public static void main(String[] args) throws Exception{ Java6 zy=new Java6(); zy.setBounds(100, 100, 340, 170); zy.setVisible(true); zy.setDefaultCloseOperation(zy.EXIT_ON_CLOSE); Class.forName("com.mysql.cj.jdbc.Driver"); //加载MySQL的驱动类 String ur1="jdbc:mysql://localhost:3306/java实训"; String user="root"; String password="root"; try { Connection Con =DriverManager.getConnection(ur1,user,password); System.out.println("eeeeee"); } catch (Exception e) { // TODO: handle exception System.out.println("ww"); } } }这些代码连接不了数据库,是哪里错误了吗?
代码中没有执行具体的 SQL 操作,只是打印了一些信息,因此连接不成功并不会报错。如果要执行 SQL 操作,需要使用 Statement 或 PreparedStatement 对象。此外,建议将连接数据库的代码放在查询按钮的 ActionListener 中执行,而不是在 main 方法中执行。以下是一个参考代码:
```java
package package06;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class Java6 extends JFrame {
JLabel wenzi;
JTextField shuru;
JButton chaxun;
JTextArea fanhui;
public Java6() {
super("中英查询软件");
add(getNorthPanel(), BorderLayout.NORTH);
}
private JPanel getNorthPanel() {
JPanel panel = new JPanel();
wenzi = new JLabel("请输入要查询的单词:");
shuru = new JTextField(8);
chaxun = new JButton("查询");
fanhui = new JTextArea(6, 25);
panel.add(wenzi, new FlowLayout());
panel.add(shuru, new FlowLayout());
panel.add(chaxun, new FlowLayout());
panel.add(fanhui, new FlowLayout());
chaxun.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String word = shuru.getText().trim();
if (word.isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入单词");
return;
}
String url = "jdbc:mysql://localhost:3306/java实训";
String user = "root";
String password = "root";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
String sql = "SELECT * FROM words WHERE word = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, word);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String meaning = rs.getString("meaning");
fanhui.setText(meaning);
} else {
fanhui.setText("未找到该单词");
}
}
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "数据库连接错误");
}
}
});
return panel;
}
public static void main(String[] args) throws Exception {
Java6 zy = new Java6();
zy.setBounds(100, 100, 340, 170);
zy.setVisible(true);
zy.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
```
注意:这只是一个参考代码,具体实现可能需要根据实际需求进行修改。另外,建议在使用完 Connection、Statement 或 ResultSet 等对象后及时关闭,可以使用 try-with-resources 语句来自动关闭。
阅读全文