如何使用 Java Netbeans 在 GUI 中执行选定的复选框
How to Execute a selected CheckBox in GUI using Java Netbeans
我想使用 Java NetBeans 在调色板 GUI 中执行一个 selected 项目。我打算先 select 一个或多个项目,然后执行按钮。这是代码:
User user = status.getUser();
Date dated = status.getCreatedAt();
PreparedStatement stmt = null;
Connection conne = null;
try {
Class.forName("com.mysql.jdbc.Driver");
jTextArea1.append("Connecting to database... " + "\n");
conne = DriverManager.getConnection("jdbc:mysql://localhostuseUnicode=true&characterEncoding=UTF-8", "root", "");
jTextArea1.append(status + "\n");
jTextArea1.append("Inserting records into the table..." + "\n");
stmt = conne.prepareStatement("set names 'utf8'");
stmt.execute();
stmt = conne.prepareStatement("set character set utf8");
stmt.execute();
stmt = conne.prepareStatement("INSERT INTO japantweet(ID,date,name,statusLocation,text,source) VALUES (?,?,?,?,?,?)");
stmt.setInt(1, (int) status.getId());
stmt.setString(2, getTimeStamp());
stmt.setString(3, status.getUser().getScreenName());
stmt.setString(4, user.getLocation());
stmt.setString(5, status.getText());
stmt.setString(6, status.getSource());
stmt.executeUpdate();
jTextArea1.append("this record inserted!" + "\n");
jTextArea1.append("=======================" + "\n");
} catch (SQLException se) {
se.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
}
这是界面:
非常感谢任何评论!
如果您使用的是设计器。
- Select 运行 按钮。
- 右键单击弹出菜单并选择“事件”->“动作”->“动作执行”。 (或只需双击按钮)。
如果您不使用设计器,请将其放入您的构造函数()
this.jButton1.addActionListener(this::jButton1ActionPerformed);
...
无论哪种方式,您的操作执行功能都将如下所示:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(this.jCheckBox1.isSelected()) {
// do the thing associated with checkbox1
}
if(this.jCheckBox2.isSelected()) {
// do the thing associated with checkbox2
}
}
如果两个复选框都被选中,它会做两件事,如果只有一个,它只会做一件事等等。
我想使用 Java NetBeans 在调色板 GUI 中执行一个 selected 项目。我打算先 select 一个或多个项目,然后执行按钮。这是代码:
User user = status.getUser();
Date dated = status.getCreatedAt();
PreparedStatement stmt = null;
Connection conne = null;
try {
Class.forName("com.mysql.jdbc.Driver");
jTextArea1.append("Connecting to database... " + "\n");
conne = DriverManager.getConnection("jdbc:mysql://localhostuseUnicode=true&characterEncoding=UTF-8", "root", "");
jTextArea1.append(status + "\n");
jTextArea1.append("Inserting records into the table..." + "\n");
stmt = conne.prepareStatement("set names 'utf8'");
stmt.execute();
stmt = conne.prepareStatement("set character set utf8");
stmt.execute();
stmt = conne.prepareStatement("INSERT INTO japantweet(ID,date,name,statusLocation,text,source) VALUES (?,?,?,?,?,?)");
stmt.setInt(1, (int) status.getId());
stmt.setString(2, getTimeStamp());
stmt.setString(3, status.getUser().getScreenName());
stmt.setString(4, user.getLocation());
stmt.setString(5, status.getText());
stmt.setString(6, status.getSource());
stmt.executeUpdate();
jTextArea1.append("this record inserted!" + "\n");
jTextArea1.append("=======================" + "\n");
} catch (SQLException se) {
se.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
}
这是界面:
非常感谢任何评论!
如果您使用的是设计器。
- Select 运行 按钮。
- 右键单击弹出菜单并选择“事件”->“动作”->“动作执行”。 (或只需双击按钮)。
如果您不使用设计器,请将其放入您的构造函数()
this.jButton1.addActionListener(this::jButton1ActionPerformed);
...
无论哪种方式,您的操作执行功能都将如下所示:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(this.jCheckBox1.isSelected()) {
// do the thing associated with checkbox1
}
if(this.jCheckBox2.isSelected()) {
// do the thing associated with checkbox2
}
}
如果两个复选框都被选中,它会做两件事,如果只有一个,它只会做一件事等等。