代码完全按计划运行但我仍然有例外吗?

时间:2015-04-16 12:17:20

标签: java

今天这只是让我的脑子发出嘎嘎声,我觉得它很容易解决。 该程序创建一个带有三个按钮的框架,根据按下的按钮改变该框架的背景颜色。

该程序正常工作,出现一个框架,我点击红色' ..将其变为红色,反之亦然,其余2种颜色,但是当我运行该程序时,我收到此错误:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at ThreeColorsViewer.main(ThreeColorsViewer.java:11)

代码

 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JPanel;

    /**
    This frame contains a panel that display one of three colors
    specified by the user.
    */
    public class ThreeColorsFrame extends JFrame {
    private JPanel colorPanel;
    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 300;

    public ThreeColorsFrame()
    {
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        colorPanel = new JPanel();
        add(colorPanel, BorderLayout.CENTER);

        createControlPanel();
    }

    /**
     Creates the control panel with buttons at the bottom of the frame.
     */
    private void createControlPanel()
    {
        JPanel southPanel = new JPanel();
        southPanel.add(makeButton("Red", Color.RED));
        southPanel.add(makeButton("Green", Color.GREEN));
        southPanel.add(makeButton("Blue", Color.BLUE));

        add(southPanel, BorderLayout.SOUTH);
    }

    /**
     Makes a button to change the color of the panel.
     @param label the button label
     @param color the color to set
     @return the button to change the color of the panel
     */
    public JButton makeButton(String label, final Color color)
    {
        JButton button = new JButton(label);

        class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                colorPanel.setBackground(color);
            }
        }

        ButtonListener listener = new ButtonListener();
        button.addActionListener(listener);
        return button;    
    }      
}

然后我有一个观众类'

import javax.swing.JFrame;

public class ThreeColorsViewer {
    public static void main(String[] args){

        JFrame viewer = new JFrame();
        ThreeColorsFrame viewerFrame = new ThreeColorsFrame();

        viewerFrame.setVisible(true);
        viewer.add(viewerFrame);
    }
}

2 个答案:

答案 0 :(得分:1)

编写以下代码,它应该适合您 无需将其添加到另一个JFrame

public static void main(String[] args){

    //JFrame viewer = new JFrame();
    ThreeColorsFrame viewerFrame = new ThreeColorsFrame();

    viewerFrame.setVisible(true);
    //viewer.add(viewerFrame);

}

答案 1 :(得分:1)

您不需要将ThreeColorsFrame添加到另一个JFrame。它本身已经是一个JFrame。这应该有用。

public class ThreeColorsViewer {
   public static void main(String[] args){
        ThreeColorsFrame viewerFrame = new ThreeColorsFrame();
        viewerFrame.setVisible(true);
    }
}