每次JComboBox更改时,如何更新JLabel? [英] How do I update a JLabel everytime a JComboBox changes?

查看:80
本文介绍了每次JComboBox更改时,如何更新JLabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有12个不同选择的JComboBox,并且根据选择的内容,我希望问题(JLabel)更改以匹配该选择.我尝试了一个if语句来查看选择的内容,如果它与应选择的内容相匹配,则问题会相应更改,但是JLabel在某种情况下永远不会真正更改.

I have a JComboBox with 12 different selections, and depending on what is selected I want the question (JLabel) to change matching the selection. I've tried an if statement to see what is selected and if it matches what should be selected, then the question changes accordingly, but the JLabel never really changes under an circumstance.

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()做了一些测试;我发现它可以一次调用所有项目,但是被选中的项目将被首先调用. 示例:

So I did a few tests with System.out.println(); and I figured out it's calling all the items at once, but the one that's selected is being called first. Example:

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");
        }
            
    }
    
});

打印出选定的圆形选定的正方形";如果您选择了圆形,但选择了选择正方形".如果选择正方形.

Prints out "Circle Selected Square Selected" if you select circle, but "Square Selected Circle Selected" if you select Square.

推荐答案

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

Add an ItemListener to the JComboBox to react when the selection changes.

此外,当您执行此操作时:

Also, when you do this:

Object isSelected = choiceBox.getSelectedItem();

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

... you are just getting the selected value at that time; you aren't magically binding the isSelected variable to update whenever the combobox updates. You need to call getSelectedItem() again if you want the new value.

这篇关于每次JComboBox更改时,如何更新JLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆