繁体   English   中英

每当JComboBox更改时,如何更新JLabel?

[英]How do I update a JLabel everytime a JComboBox changes?

我有一个带有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");
        }

    }

});

如果选择圆,则输出“选择圆选定的圆”,如果选择“平方”,则打印出“选定圆的选定圆”。

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

另外,当您这样做时:

Object isSelected = choiceBox.getSelectedItem();

...您当时只是获得选定的值; 您不会神奇地绑定isSelected变量以在组合框更新时进行更新。 如果需要新值,则需要再次调用getSelectedItem()

尝试将动作侦听器添加到JComboBox

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

更改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();
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM