问题部分是我声明和分配的地方:
String emotionText = (String) emotions.getSelectedItem();
我稍后尝试使用它来与 JcomboBox 中的数组项之一进行比较。我说的地方
if (emotionText.equals("Happy") {
JLabel newText = new JLabel(verse);
panel2.add(newText);
}
即。如果结果是这样..那么就这样做,但它不能正常工作。任何见解将不胜感激。预先感谢。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JComboBox {
public static void main(String[] args) {
JFrame frame = new JFrame("Mood Food");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 800);
frame.setLocation(430, 100);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // added code
frame.add(panel);
var name = javax.swing.JOptionPane.showInputDialog("What is your name?");
JLabel lblName = new JLabel(name + " ,welcome to the Mood Food Program");
lblName.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lblName);
JLabel lbl = new JLabel("How are you feeling today?");
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lbl);
String[] choices = {"Select..", "Happy", "Content", "Grateful", "Excited", "Sad",
"Angry", "Lonely", "Heartbroken", "Worried"};
JComboBox<String> emotions = new JComboBox<String>(choices);
emotions.setMaximumSize(panel.getPreferredSize());
emotions.setAlignmentX(Component.CENTER_ALIGNMENT);
emotions.setAlignmentY(Component.TOP_ALIGNMENT);
String emotionText = (String) emotions.getSelectedItem();
panel.add(emotions);
String verse = Happy.Verses();
JPanel panel2 = new JPanel();
panel2.setVisible(false);
frame.add(panel2);
if (emotionText.equals("Happy") {
JLabel newText = new JLabel(verse);
panel2.add(newText);
}
JButton btn = new JButton("OK");
btn.setAlignmentX(Component.CENTER_ALIGNMENT); // added code
panel.add(btn);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
boolean visible = panel.isVisible();
panel.setVisible(!visible);
panel2.setVisible(visible);
}});
frame.setVisible(true); // added code
}
}
Swing 是一个事件驱动的环境,也就是说,发生某些事情并且您对其做出响应。这也意味着您的代码不会以线性或顺序流运行。
例如,你不能这样做...
JComboBox<String> emotions = new JComboBox<String>(choices);
emotions.setMaximumSize(panel.getPreferredSize());
emotions.setAlignmentX(Component.CENTER_ALIGNMENT);
emotions.setAlignmentY(Component.TOP_ALIGNMENT);
String emotionText = (String) emotions.getSelectedItem();
panel.add(emotions);
String verse = Happy.Verses();
if (emotionText.equals("Happy") {
JLabel newText = new JLabel(verse);
panel2.add(newText);
}
当您调用
getSelectedItem
时,用户实际上没有机会选择任何内容(没有向他们呈现任何内容)。
相反,您需要将
ActionListener
添加到 JComboBox
并从其中更新 emotionText
。
由于此工作流程的延迟性质,您还应该更新按钮上的
ActionListener
,以根据 emotionText
的值更新标签。
更像是...
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JComboBox {
public static void main(String[] args) {
new Main();
}
private String emotionText = "Empty inside";
private CardLayout cardLayout;
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Mood Food");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 800);
frame.setLocation(430, 100);
cardLayout = new CardLayout();
frame.setLayout(cardLayout);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
frame.add(panel, "first");
var name = javax.swing.JOptionPane.showInputDialog("What is your name?");
JLabel lblName = new JLabel(name + " ,welcome to the Mood Food Program");
lblName.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lblName, gbc);
JLabel lbl = new JLabel("How are you feeling today?");
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(lbl, gbc);
String[] choices = {"Select..", "Happy", "Content", "Grateful", "Excited", "Sad",
"Angry", "Lonely", "Heartbroken", "Worried"};
JComboBox<String> emotions = new JComboBox<String>(choices);
emotions.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
emotionText = (String)emotions.getSelectedItem();
}
});
panel.add(emotions, gbc);
JPanel panel2 = new JPanel();
JLabel newText = new JLabel(emotionText);
panel2.add(newText);
frame.add(panel2, "second");
JButton btn = new JButton("OK");
btn.setAlignmentX(Component.CENTER_ALIGNMENT); // added code
panel.add(btn, gbc);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newText.setText(emotionText);
cardLayout.show(frame.getContentPane(), "second");
}
});
frame.setVisible(true); // added code
}
});
}
}