如何基于JComboBox选择更改JTextField

时间:2022-09-11 18:56:13

This is just a silly program I am working on for school but I am having some trouble. I have a JComboBox and based on what the user selects I want to change the text field. I'm having some trouble with that however, as it stands now the program compiles and runs but the text field doesn't change. I've found plenty of examples of people doing far more complicated things but I just need a simple solution. Here is the code. Thanks!

这只是我在学校工作的一个愚蠢的计划,但我遇到了一些麻烦。我有一个JComboBox,根据用户选择我想要更改文本字段。然而,我遇到了一些麻烦,因为现在程序编译并运行,但文本字段不会改变。我发现很多人做了更复杂的事情,但我只需要一个简单的解决方案。这是代码。谢谢!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class emocon extends JFrame implements ItemListener {
JPanel row1 = new JPanel();
JComboBox choose = new JComboBox();
JPanel row2 = new JPanel();
JTextField text = new JTextField(10);
//image will be displayed here
JPanel row3 = new JPanel();
JLabel pic = new JLabel();

//Images
ImageIcon happy = new ImageIcon("images/happy.gif");
ImageIcon lol = new ImageIcon("images/lol.gif");
ImageIcon winky = new ImageIcon("images/winky.gif");
ImageIcon sad = new ImageIcon("images/sad.gif");
ImageIcon worried = new ImageIcon("images/worried.gif");
ImageIcon angry = new ImageIcon("images/angry.gif");
ImageIcon shock = new ImageIcon("images/shock.gif");
ImageIcon uninpressed = new ImageIcon("images/uninpressed.gif");
ImageIcon yawn = new ImageIcon("images/yawn.gif");
ImageIcon evil = new ImageIcon("images/evil.gif");

public emocon(){
    setTitle("Emoticon Converter");
    setSize(350,350);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

GridLayout two = new GridLayout(3,1);
setLayout(two);
 choose.addItem("Happy");
 choose.addItem("LOL");
 choose.addItem("Winky");
 choose.addItem("Sad");
 choose.addItem("Worried");
 choose.addItem("Angry");
 choose.addItem("Shock");
 choose.addItem("Uninpressed");
 choose.addItem("Yawn");
 choose.addItem("Evil");
choose.addItemListener(this);
row1.add(choose);
row2.add(text);
row3.add(pic); 
add(row1);
add(row2);
add(row3);

}
@Override
public void itemStateChanged(ItemEvent item) {
Object source = item.getSource();
String emo = source.toString();
if (emo == "Sad"){
    text.setText("hjgjhg");
}
}   
public static void main(String[] args) {
    emocon emo = new emocon();
}
}

2 个解决方案

#1


3  

You've a problem here:

你有一个问题:

if (emo == "Sad"){
    text.setText("hjgjhg");
}

Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

不要使用==比较字符串。请改用equals(...)或equalsIgnoreCase(...)方法。理解==检查两个对象是否相同而不是你感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是重要的。而不是

if (fu == "bar") {
  // do something
}

do,

做,

if ("bar".equals(fu)) {
  // do something
}

or,

要么,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}

EDIT

编辑

Your other problem is that you're just getting the source from the ItemEvent. The source is the JComboBox, not what you want. You need to get the selected item!

你的另一个问题是你只是从ItemEvent获取源代码。源代码是JComboBox,而不是你想要的。你需要获得所选项目!

Try using a different method available in your ItemEvent object, not getSource().

尝试使用ItemEvent对象中可用的其他方法,而不是getSource()。

#2


1  

Get the selected item like this:

获取所选项目,如下所示:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}

#1


3  

You've a problem here:

你有一个问题:

if (emo == "Sad"){
    text.setText("hjgjhg");
}

Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

不要使用==比较字符串。请改用equals(...)或equalsIgnoreCase(...)方法。理解==检查两个对象是否相同而不是你感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是重要的。而不是

if (fu == "bar") {
  // do something
}

do,

做,

if ("bar".equals(fu)) {
  // do something
}

or,

要么,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}

EDIT

编辑

Your other problem is that you're just getting the source from the ItemEvent. The source is the JComboBox, not what you want. You need to get the selected item!

你的另一个问题是你只是从ItemEvent获取源代码。源代码是JComboBox,而不是你想要的。你需要获得所选项目!

Try using a different method available in your ItemEvent object, not getSource().

尝试使用ItemEvent对象中可用的其他方法,而不是getSource()。

#2


1  

Get the selected item like this:

获取所选项目,如下所示:

String item = (String)jComboBox.getSelectedItem();
if(item.equals("Sad")){
    text.setText("hjgjhg");
}