当我点击位于jList中的项目时,背景颜色不会改变[重复](Background color doesn't change when I click on the item located

编程入门 行业动态 更新时间:2024-10-25 21:32:46
当我点击位于jList中的项目时,背景颜色不会改变[重复](Background color doesn't change when I click on the item located in the jList [duplicate])

这个问题在这里已有答案:

如何比较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 answers

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

更多推荐

本文发布于:2023-08-07 22:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   颜色   背景   项目   jList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!