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

时间:2013-05-23 03:21:59

标签: java swing this jcombobox runtimeexception

我处于初步阶段,无论我尝试什么,似乎无法让程序运行。我认为这个问题与我如何调用关键字“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();
        }
    });
} 
}

显示的错误是:

  

运行:   线程“AWT-EventQueue-0”中的异常java.lang.RuntimeException:无法编译的源代码 - 错误的sym类型:createAndShowGUI       在Main $ 1.run(Main.java:53)       at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)       at java.awt.EventQueue.access $ 200(EventQueue.java:103)       at java.awt.EventQueue $ 3.run(EventQueue.java:688)       at 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.awt.EventQueue.dispatchEvent(EventQueue.java:697)       at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)       at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)       at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)   建立成功(总时间:3秒)

1 个答案:

答案 0 :(得分:4)

  1. createAndShowGUI期待两个参数,但在main方法中,您没有传递任何参数。当你在Main课程中创建它们时,我无法理解为什么你真的需要它们......
  2. 您创建了一个名为Main的{​​{1}}实例,但实际上从未将其添加到任何内容中......
  3. 您要将newContentPanelabel1添加到label2中的同一位置,有效隐藏BorderLayout
  4. 您要将label1color1添加到color2中的同一位置,有效隐藏BorderLayout
  5. 您永远不会隐瞒color1label1导致label2
  6. 另外,你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(); listen1是一回事......

    工作示例

    listen2