列表中的选定项在JAVA中不起作用

列表中的选定项在JAVA中不起作用,java,Java,如果我点击按钮,就会出现一个错误。这怎么可能? 我的结果是,如果我点击按钮,标签“text1”应该得到组合框的文本 import javax.swing.*; import java.awt.*; public class Naveed extends JFrame { public static void main(String[] args) { JFrame frame = new Naveed(); frame.setSize(400, 400

如果我点击按钮,就会出现一个错误。这怎么可能? 我的结果是,如果我点击按钮,标签“text1”应该得到组合框的文本

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

public class Naveed extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new Naveed();
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setTitle("Rechthoek");
        Paneel paneel = new Paneel();
        frame.setContentPane(paneel);
        frame.setVisible(true);
    }
}
//


您没有正确分配abc和text1。如下更改代码:

        String[] items = {"Item1", "Item2", "Item3"};
        abc = new JComboBox(items);
        JButton button1 = new JButton("Click");
        button1.addActionListener(new knopHandler());
        text1 = new JLabel("Result");

告诉我们出现了什么错误会很有用。通过在构造函数中重新声明text1变量来查找“变量阴影”。不要这样做。是的,在将来,请提出一个更完整的问题,其中包含错误消息并指示抛出错误的行
text1=新的JLabel(“结果”)
因此您没有重新声明变量,也没有将构造函数的text1设置为局部变量。
        String[] items = {"Item1", "Item2", "Item3"};
        abc = new JComboBox(items);
        JButton button1 = new JButton("Click");
        button1.addActionListener(new knopHandler());
        text1 = new JLabel("Result");