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,而是在可用推文计数的构造函数中调用 JOptionPane:
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。当第一次创建对象时以及调用构造函数之前时,会调用此函数,这会让您感到困惑。我自己,我想知道所有这些是否可以包含在一个静态方法中。
关于java - 如何将 ResultSet 响应传递给 JOptionPane showMessageDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32486845/