这只是我正在为学校工作的一个愚蠢的计划,但我遇到了一些麻烦。我有一个 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();
}
}