这个问题在这里已有答案:
如何比较Java中的字符串? 23个答案我的项目中有一个jList和按钮。 我的目的是当我点击jList上的项目时,需要更改按钮的背景颜色。 在我的情况下,当我点击该项目时我返回的值是“OK”或“NO”。 因此,当项目在jList中单击时,我返回对应的“OK”或“NO”没有任何问题。 但是,背景颜色不会改变。 这是我使用的代码:
private void listBoxProjectsValueChanged(javax.swing.event.ListSelectionEvent evt) { String value = (String)listBoxProjects.getSelectedValue(); String sql = "SELECT * FROM PBL_Projects where ProjectName=?"; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://webserver:1111; databaseName=sinfo;integratedSecurity=true;"; Connection conn = DriverManager.getConnection(url); PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, value); ResultSet rs = pst.executeQuery(); if(rs.next()){ String studentName = rs.getString("StudentName"); String projectName = rs.getString("ProjectName"); String projectDetails = rs.getString("ProjectDetails"); String t1s1 = rs.getString("T1S1"); txtDisplayProjectName.setText(projectName); txtDisplayStudentName.setText(studentName); txtAreaDisplayProjectDetails.setText(projectDetails); lblResult.setText(t1s1); if(t1s1=="NO"){ btnT1S1.setBackground(Color.RED); }else if(t1s1 == "OK"){ btnT1S1.setBackground(Color.GREEN); }else{ btnT1S1.setBackground(Color.BLUE); } } } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } }在这里,我可以看到其他属性,如ProjectName,StudentName等。 我使用非常简单的if语句来改变按钮的背景颜色。 但它不会让我。
This question already has an answer here:
How do I compare strings in Java? 23 answersI have a jList and buttons in my project. My purpose is when I click on the item on the jList, the background color of the buttons needs to be changed. In my case, the value that I am returning when I click on the item is either "OK" or "NO". So when the item clicked in the jList, I am returning corresponded "OK" or "NO" without any problem. However, the background color doesn't change. Here is the code that I use:
private void listBoxProjectsValueChanged(javax.swing.event.ListSelectionEvent evt) { String value = (String)listBoxProjects.getSelectedValue(); String sql = "SELECT * FROM PBL_Projects where ProjectName=?"; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://webserver:1111; databaseName=sinfo;integratedSecurity=true;"; Connection conn = DriverManager.getConnection(url); PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, value); ResultSet rs = pst.executeQuery(); if(rs.next()){ String studentName = rs.getString("StudentName"); String projectName = rs.getString("ProjectName"); String projectDetails = rs.getString("ProjectDetails"); String t1s1 = rs.getString("T1S1"); txtDisplayProjectName.setText(projectName); txtDisplayStudentName.setText(studentName); txtAreaDisplayProjectDetails.setText(projectDetails); lblResult.setText(t1s1); if(t1s1=="NO"){ btnT1S1.setBackground(Color.RED); }else if(t1s1 == "OK"){ btnT1S1.setBackground(Color.GREEN); }else{ btnT1S1.setBackground(Color.BLUE); } } } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } }In here, I can see the other properties like ProjectName, StudentName, and others. I use very simple if statement to change the background color of the buttons. But it won't let me.
最满意答案
我正在使用double equals运算符比较String。 它应该是.equals方法。
if(t1s1.equals("NO")){ comboT1S1.setBackground(Color.red); btnT1S1.setBackground(Color.RED); btnT1S1.setText("NO"); }else if(t1s1.equals("OK")){ btnT1S1.setBackground(Color.GREEN); btnT1S1.setText("OK"); }else if (t1s1.equals("")){ btnT1S1.setBackground(Color.BLUE); }I was comparing String using double equals operator. It should be .equals method.
if(t1s1.equals("NO")){ comboT1S1.setBackground(Color.red); btnT1S1.setBackground(Color.RED); btnT1S1.setText("NO"); }else if(t1s1.equals("OK")){ btnT1S1.setBackground(Color.GREEN); btnT1S1.setText("OK"); }else if (t1s1.equals("")){ btnT1S1.setBackground(Color.BLUE); }更多推荐
发布评论