每次JComboBox更改时如何更新JLabel?

时间:2013-06-08 02:06:46

标签: java swing jlabel jcombobox itemlistener

我有一个包含12种不同选择的JComboBox,根据选择的内容,我希望问题(JLabel)更改匹配选择。我已经尝试了if语句来查看所选内容以及它是否与应该选择的内容相匹配,然后问题会相应地更改,但JLabel在某种情况下从未真正发生过变化。

代码

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Window extends JFrame{
    private static final long serialVersionUID = 1L;
    public Window(){
        super("Area Finder v1.0");
        BufferedImage image = null;

        try {
            image = ImageIO.read(getClass().getClassLoader().getResource("images/areafinder.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.setIconImage(image);
        setSize(400, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        JLabel Instr = new JLabel("What would kind of area would you like to find?");
        String[] areaChoices = {"Circle", "Square", "Rectangle", "Triangle", "Trapezoid", "Parallelogram", "Hexagon", "Rhombus", "Pentagon", "Polygon", "Ellipse", "Sector"};
        final JComboBox<?> choiceBox = new JComboBox(areaChoices);
        final Object isSelected = choiceBox.getSelectedItem();
        choiceBox.setToolTipText("Select which one you want to find the area of!");
        choiceBox.setSelectedIndex(0);
        final JLabel q = new JLabel("");
        final JTextField inputFromUser = new JTextField("");
        JButton findArea = new JButton("Find Area");
        /* Question Changer*/

        /*End of Question Changer*/
        findArea.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0){
                if(isSelected == "Circle"){
                    double radius = Double.parseDouble(inputFromUser.getText());
                    double area = 3.14 * (radius * radius);
                    JOptionPane.showMessageDialog(null, "Your Area is " + area);
                }else if(isSelected == "Square"){

                }

            }
        });
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(15,15,15,15);
        panel.add(Instr);
        panel.add(choiceBox);
        panel.add(findArea);
        panel.add(q);
        panel.add(inputFromUser);
        add(panel);
    }
}
编辑:所以我用System.out.println()进行了一些测试;我发现它同时调用了所有项目,但是首先调用了所选项目。 例如:

choiceBox.addItemListener(new ItemListener(){
    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getItem() == "Circle"){
        System.out.println("Circle selected");
        }else if(e.getItem() == "Square"){
            System.out.println("Square selected");
        }

    }

});

如果选择圆形,则打印出“选择圆形所选方形”,如果选择“方形”,则选择“选定方形圆形”。

3 个答案:

答案 0 :(得分:5)

ItemListener添加JComboBox以便在选择更改时做出反应。

此外,当你这样做时:

Object isSelected = choiceBox.getSelectedItem();

...你刚刚获得所选的值;只要组合框更新,你就不会神奇地绑定isSelected变量来更新。如果您想要新值,则需要再次致电getSelectedItem()

答案 1 :(得分:4)

尝试向JComboBox添加动作侦听器

choiceBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//put the code here
}
});

答案 2 :(得分:0)

更改JLabel的文本

label.setText("Changed text");

告诉容器重新布局。

frame.invalidate();
frame.repaint();

这可确保重绘框架以显示更改的标签。要知道组合框何时更改了它的选择,请像这样添加ActionListener

combo.addActionListener (new ActionListener ()
{
    public void actionPerformed(ActionEvent e)
    {
        label.setText(combo.getText());
        frame.invalidate();
    }
});