我在 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";
}
}
}