如何检查JButton是否被按下?我知道有一个方法,它的名字是"isEnabled"
所以我尝试编写一个代码进行测试.
这里的代码:
final JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
panel.add(btnAdd);
JButton btnConfirm = new JButton("Check Out");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (btnAdd.isEnabled()) {
System.out.println("Add Button is pressed");
}
if (!btnAdd.isEnabled()) {
System.out.println("Add Button is not pressed");
}
}
});
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,代码只提供"按下添加按钮",虽然我没有按"添加"按钮.为什么会这样发生?
我写了一些java代码,当用户按下按钮时,将数据插入到SQL Server 2012的数据库中.当我运行代码时,我收到此错误:
com.microsoft.sqlserver.jdbc.SQLServerException:','附近的语法不正确.
它说这sqlstatement.executeUpdate();条线导致了错误.我知道这条线不是问题.问题是我的SQL查询,但我找不到我的查询是如何错误的.你能帮帮我吗?
这里的代码
count++;
for(int count = 0; count < table_1.getRowCount(); count++){
try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection dbconbt8 = DriverManager.getConnection("" +"jdbc:sqlserver://localhost;databaseName=Store;user=sa;password=XXXXXX");
String sqlQ = "INSERT INTO [dbo].[Transaction]([TransactionID],[ProductID]"+
",[TotalPrice]) VALUES ("+count+"','"+table_1.getValueAt(count, 0).toString()+"','"+sumprice+ "') ";
PreparedStatement sqlstatement = dbconbt8.prepareStatement(sqlQ);
sqlstatement.executeUpdate();
sqlstatement.close();
dbconbt8.close();
} catch (SQLException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)