java 实现 ItemListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12449792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:56:14  来源:igfitidea点击:

Implementing ItemListener

javaswingjframejpanelitemlistener

提问by Pickle

i've been having problem in the itemStateChanged section. when I compiled the program i get the "cannot find variable" error and I can't seem to find out where I did wrong. Any help is much appreciated. Thank You.

我在 itemStateChanged 部分遇到了问题。当我编译程序时,我收到“找不到变量”错误,而且我似乎无法找出我做错的地方。任何帮助深表感谢。谢谢。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Postcode extends JFrame implements ItemListener {

    public static void main(String[] arg) {
        JFrame fr = new JFrame("Melaka Postcode");
        fr.setSize(240, 125);
        fr.setVisible(true);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public Postcode() {
        String[] code = {"75450", "77000", "78000"};
        JLabel lb1 = new JLabel("Postcode");
        JLabel lb2 = new JLabel("District?");
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JComboBox cb = new JComboBox(code);
        cb.addItemListener(this);
        Font f = new Font("Verdana", Font.BOLD, 14);
        lb2.setFont(f);
        p1.add(lb1);
        p1.add(cb);
        p2.add(lb2);
    }

    public void itemStateChanged(ItemEvent e) {
        if (code.getSelectedItem().equals("75450")) {
            lb2.setText = "Bukit Beruang";
        }
        if (code.getSelectedItem().equals("77000")) {
            lb2.setText = "Jasin";
        }
        if (code.getSelectedItem().equals("75450")) {
            lb2.setText = "Alor Gajah";
        }
    }
}

回答by Dan D.

Your code had a lot of problems, so I have to post here the solution entirely. The fixes:

你的代码有很多问题,所以我必须在这里完整地发布解决方案。修复:

Declared the variables as member variables

将变量声明为成员变量

Called getSelectedItem()on the combobox, not on the array of String

getSelectedItem()在 上调用combobox,而不是在 String 数组上调用

Use the JLabel.setText()correctly

JLabel.setText()正确使用

import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Postcode extends JFrame implements ItemListener {
    String[] code = { "75450", "77000", "78000" };

    JLabel lb1 = new JLabel("Postcode");

    JLabel lb2 = new JLabel("District?");

    JPanel p1 = new JPanel();

    JPanel p2 = new JPanel();

    JComboBox cb = new JComboBox(code);

    public static void main(String[] arg) {
        JFrame fr = new JFrame("Melaka Postcode");
        fr.setSize(240, 125);
        fr.setVisible(true);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public Postcode() {
        cb.addItemListener(this);

        Font f = new Font("Verdana", Font.BOLD, 14);

        lb2.setFont(f);

        p1.add(lb1);
        p1.add(cb);

        p2.add(lb2);

    }

    public void itemStateChanged(ItemEvent e) {
        if (cb.getSelectedItem().equals("75450"))
            lb2.setText("Bukit Beruang");
        if (cb.getSelectedItem().equals("77000"))
            lb2.setText("Jasin");
        if (cb.getSelectedItem().equals("75450"))
            lb2.setText("Alor Gajah");
    }

}

回答by dbf

In itemStateChanged, you are calling .getSelectedItem()on a variable that is not known to the scope of itemStateChanged(), secondly I don't think you want to call this method on a String array (code), I think you use e.getItem()or e.getItemSelectableinstead.

在 itemStateChanged 中,您正在调用.getSelectedItem()范围未知的变量itemStateChanged(),其次我认为您不想在 String 数组 ( code)上调用此方法,我认为您使用e.getItem()e.getItemSelectable代替。

See the documentation on ItemEvent

请参阅有关ItemEvent的文档