Oracle“如何制作对话框” tutorial并没有帮助我,也没有看到任何其他帖子,因为它们不能解决将值传递给showMessageDialog的问题。
我有一个Java Swing应用程序,如下所示:
用户在三个字段中的每个字段中输入信息,当用户按下按钮“获取推文”时,这些信息将合并到MySQL查询中。
按下按钮后,我想将行计数返回到showMessageDialog,为用户提供是否继续的选项。
问题:我不知道如何将ResultSet值传递给showMessageDialog。我是否需要在JOptionPane上设置一个动作监听器?如果是这样,您如何管理时间?
我的showMessageDialog如下所示。如您所见,“ 0”的值不正确。
我的JButton和关联的动作监听器的代码片段:
JButton btnFetch = new JButton("Fetch Tweets!");
btnFetch.setBackground(new Color(255, 153, 51));
btnFetch.setForeground(new Color(0, 0, 128));
btnFetch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
app.Query_Variable = queryText.getText();
app.Start_Date = startText.getText();
app.End_Date = endText.getText();
try {
app.getTweetCount();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
response = new ResponseOption(app.Tweet_Count);
/*
try {
app.runApplication();
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | ParseException
| SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
JOptionPane.showMessageDialog(frame, "Find your file at " + app.Printed_Filename);
}
});
我为showMessageDialog创建了一个名为“ ResponseOption”的类,认为这是初始化对话框所必需的。但是,对话框显示时不包含ResultSet值...
public class ResponseOption {
int RowCount;
Object[] options = {"Yes, Please.",
"No, Thanks."};
String question = "There are " + RowCount + " Tweets. Do you want to print them?";
int n = JOptionPane.showOptionDialog(frame,
"There are " + X + " Tweets. Do you want to print them?",
"Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, // do not use custom icon
options, // titles of buttons
options[0]); // default button title
public ResponseOption(int Rowcount) {
this.RowCount = RowCount;
}
}
getTweetCount()代码:---此代码很好。
public int getTweetCount() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
// create a mysql database connection
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/?";
Class.forName(myDriver).newInstance();
Connection conn = DriverManager.getConnection(myUrl, User, Password);
PreparedStatement setNames = conn.prepareStatement("SET NAMES 'utf8mb4'");
setNames.execute();
// fetch tweet count
String CountSQL = "SELECT count(*) "
+ "FROM test.tweet "
+ "WHERE text LIKE '%" + Query_Variable + "%' "
+ "AND created_at BETWEEN '" + Start_Date + "' AND '" + End_Date + "';";
PreparedStatement CountPS = conn.prepareStatement(CountSQL);
ResultSet CountRS = CountPS.executeQuery();
if (CountRS.next()) {
Tweet_Count = CountRS.getInt(1);
}
System.out.println("Tweet count = " + Tweet_Count);
return Tweet_Count;
}
最佳答案
一种可能的改进是更改ResponseOption,以便在创建对象时不调用JOptionPane,而是在可使用tweet计数编号的构造函数中调用:
public ResponseOption(int rowCount, JFrame frame) {
this.rowCount = rowCount;
String question = "There are " + rowCount + " Tweets. Do you want to print them?";
n = JOptionPane.showOptionDialog(frame,
question,
"Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
// do something with n here?
}
请注意,以前您在变量声明部分中调用了JOptionPane。第一次创建对象时以及构造函数被调用之前,就会被调用,这使您感到困惑。我自己,我想知道是否所有这些都可以用一个静态方法包装起来。