检查背景颜色并更改另一帧的背景

时间:2014-06-08 12:36:21

标签: java button user-interface checkbox jframe

我是java的新手,我正在尝试编写一个程序让用户检查他们想要的颜色(红色,绿色或蓝色),用户在决定后按Enter键然后再按另一帧弹出一个简笔画和选择的背景。然而,该程序显示了这些错误:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:431)
at java.awt.Container.addImpl(Container.java:1039)
at java.awt.Container.add(Container.java:959)
at javax.swing.JFrame.addImpl(JFrame.java:540)
at java.awt.Container.add(Container.java:365)
at Background.<init>(Background.java:48)
at Test.main(Test.java:4)`

这是其中一个类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.*;
public class Background extends JFrame{
 private JLabel messageLabel;
 private JCheckBox red;
 private JCheckBox green;
 private JCheckBox blue;
 private JButton enter;
 private JFrame frame;
 private Stick stick;
 private Color color;
 private final int WINDOW_WIDTH=400;
 private final int WINDOW_HEIGHT=400;
 public Background(){
  setTitle("Choose your background");
  setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  messageLabel=new JLabel("Check which background color do you want?");
  red=new JCheckBox("red");
  green=new JCheckBox("green");
  blue=new JCheckBox("blue");
  enter=new JButton("ENTER");
  frame=new JFrame();
  red.addItemListener(new CheckBoxListener());
  green.addItemListener(new CheckBoxListener());
  blue.addItemListener(new CheckBoxListener());
  enter.addActionListener(new ButtonListener());
  setLayout(new FlowLayout());
  add(messageLabel);
  add(red);
  add(green);
  add(blue);
  add(enter);
  add(frame);
  setVisible(true);
 }
 private class CheckBoxListener implements ItemListener{
  public void itemStateChanged(ItemEvent e){
   if(e.getSource()==red){
    getContentPane().setBackground(Color.RED);
    color=Color.RED;
   }
   else if(e.getSource()==green){
    getContentPane().setBackground(Color.GREEN);
    color=Color.green;
   }
   else if(e.getSource()==blue){
    getContentPane().setBackground(Color.BLUE);
    color=Color.blue;
   }
  }
 }
 private class ButtonListener implements ActionListener{
     public void actionPerformed (ActionEvent e){
         if(enter.isSelected()){
             Stick component=new Stick();
                    frame.getContentPane().setBackground(color);
                frame.setSize(400, 400 );
                frame.setTitle("Welcome Home");
                frame.add(component);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setBackground(color);
         }
     }
 }
}

主要方法:

public class Test {
public static void main(String[] args) {
    // TODO Auto-generated method stub
      Background fr=new Background();
      }  
}

1 个答案:

答案 0 :(得分:0)

stacktrace有(隐含)问题的答案。在尝试将帧添加到自身时,应删除此语句

add(frame);