【问题标题】:How to Execute a selected CheckBox in GUI using Java Netbeans如何使用 Java Netbeans 在 GUI 中执行选定的 CheckBox
【发布时间】:2016-02-22 16:51:58
【问题描述】:

我想使用 Java NetBeans 在调色板 GUI 中执行选定的项目。我打算先选择一个或多个项目,然后执行按钮。代码如下:

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();
}

这是界面:

非常感谢您的任何评论!

【问题讨论】:

  • 执行选中项是什么意思??
  • 我的意思是执行一项或多项检查项目
  • 这里的问题是关于执行 当你点击Run 时如果你同时选择JapanIndonesia 会发生什么??
  • 是的,这就是我的意思。抱歉解释不清楚..
  • 当您点击Run 时,如果您同时选择JapanIndonesia 会发生什么??

标签: java user-interface netbeans checkbox execute


【解决方案1】:

如果您使用的是设计器。

  1. 选择运行按钮。
  2. 右键单击弹出菜单并选择事件 -> 动作 -> 动作执行。 (或者直接双击按钮)。

如果你没有使用 Designer 把它放在你的 constructor() 中

 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
    }
}  

如果两个复选框都被选中,它会做两件事,如果只有一个,它只会做一件事等等。

【讨论】:

  • 它有效。是的,这就是我想要的。非常感谢!
  • 您好 WillShackleford,如果您愿意帮助我,我在 stackoverflow.com/questions/70844564/… 的帖子中有另一个问题。这是与这篇文章相关的问题。
猜你喜欢
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
相关资源
最近更新 更多