繁体   English   中英

错误:“无法编译的源代码”

[英]Error: “Uncompilable source code”

我处于初步阶段,无论我尝试什么,似乎无法让程序运行。 我认为这个问题与我如何调用关键字“this”有关,但我不能确定。

现在我让程序简单地打印出JComboBox中突出显示的内容,以便保持简单,因为比较方面可以稍后使用“if”语句完成。 任何帮助表示赞赏。 这是我的代码:

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

public class Main extends JPanel implements ActionListener {
    JFrame frame;
    JPanel panel;
    JLabel label1;
    JLabel label2;

    public Main() {
        super(new BorderLayout());

        String[] colorStrings = { "", "Black", "White", "Red", "Blue", "Yellow",
        "Green", "Orange", "Purple"};
        JComboBox color1 = new JComboBox(colorStrings);
        color1.setSelectedIndex(7);
        color1.addActionListener(this);
        JComboBox color2 = new JComboBox(colorStrings);
        color2.setSelectedIndex(7);
        color2.addActionListener(this);

        add(label1, BorderLayout.PAGE_START);
        add(color1, BorderLayout.PAGE_START);
        add(label2, BorderLayout.PAGE_END);
        add(color2, BorderLayout.PAGE_END);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }

    public void actionPerformed(ActionEvent e) {
        JComboBox listen1 = (JComboBox)e.getSource();
        String otherColor1 = (String)listen1.getSelectedItem();
        System.out.println(otherColor1);
        JComboBox listen2 = (JComboBox)e.getSource();
        String otherColor2 = (String)listen2.getSelectedItem();
        System.out.println(otherColor2);
    }
    private static void createAndShowGUI(JComboBox color1, JComboBox color2) {
        JFrame frame = new JFrame("Does it match?");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent newContentPane = new Main();
        newContentPane.add(color1, color2);
        newContentPane.setOpaque(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
} 
}

显示的错误是:

run:线程“AWT-EventQueue-0”中的异常java.lang.RuntimeException:无法编译的源代码 - 错误的sym类型:java.awt.event.InvocationEvent.dispatch中Main $ 1.run(Main.java:53)的createAndShowGUI( InvocationEvent.java:251)java.awt.EventQueue.access上的java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727),java.awt.EventQueue $ 3.run(EventQueue.java)中的$ 200(EventQueue.java:103) :688)java.awt.EventQueue $ 3.run(EventQueue.java:686)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)at java。位于java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)java.awt.EventDispatchThread.pumpEventsForHierarchy的java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)上的awt.EventQueue.dispatchEvent(EventQueue.java:697) (EventDispatchThread.java:150)java.awt.EventDi上的java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) java.awt.EventDispatchThread.run中的spatchThread.pumpEvents(EventDispatchThread.java:138)(EventDispatchThread.java:91)构建成功(总时间:3秒)

  1. createAndShowGUI期待两个参数,但在你的main方法中你没有传递任何参数。 无论如何,当你在Main类中创建它们时,我无法理解为什么你真的需要它们......
  2. 你创建一个名为newContentPaneMain实例,但实际上从未将它添加到任何东西......
  3. 您正在将label1label2添加到BorderLayout的相同位置,从而有效地隐藏label1
  4. 您要添加color1color2的范围内,以相同位置BorderLayout ,从而有效地隐藏color1
  5. 你永远不会隐藏label1label2导致NullPointerException

另外,你的actionListener逻辑是错误的......

JComboBox listen1 = (JComboBox) e.getSource();
String otherColor1 = (String) listen1.getSelectedItem();
System.out.println(otherColor1);
JComboBox listen2 = (JComboBox) e.getSource();
String otherColor2 = (String) listen2.getSelectedItem();

listen1listen2是一回事......

工作实例

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Main extends JPanel implements ActionListener {

    JFrame frame;
    JPanel panel;
    JLabel label1;
    JLabel label2;

    public Main() {
        // Change the layout to something more useful
        super(new GridLayout(2, 2));

        String[] colorStrings = {"", "Black", "White", "Red", "Blue", "Yellow",
            "Green", "Orange", "Purple"};
        JComboBox color1 = new JComboBox(colorStrings);
        color1.setSelectedIndex(7);
        color1.addActionListener(this);
        JComboBox color2 = new JComboBox(colorStrings);
        color2.setSelectedIndex(7);
        color2.addActionListener(this);

        // Create the instances of the labels to prevent NullPointerException
        label1 = new JLabel("#1");
        label2 = new JLabel("#2");

        add(label1);
        add(color1);
        add(label2);
        add(color2);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    }

    public void actionPerformed(ActionEvent e) {
        JComboBox listen1 = (JComboBox) e.getSource();
        String otherColor1 = (String) listen1.getSelectedItem();
        System.out.println(otherColor1);
        JComboBox listen2 = (JComboBox) e.getSource();
        String otherColor2 = (String) listen2.getSelectedItem();
        System.out.println(otherColor2);
    }

    // This doesn't make sense
//    private static void createAndShowGUI(JComboBox color1, JComboBox color2) {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Does it match?");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent newContentPane = new Main();
        // Actually add the Main to the frame...
        frame.setContentPane(newContentPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // This will now work
                createAndShowGUI();
            }
        });
    }
}

暂无
暂无

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

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