IllegalArgumentException 错误
IllegalArgumentException error
所以我尝试使用 MVC(模型、视图、控制器)来格式化我的代码,当我尝试将视图添加到实际应用程序时,我收到一条错误消息“java.lang.IllegalArgumentException:添加一个window 到一个容器
在 java.awt.Container.checkNotAWindow
在 java.awt.Container.addImpl
在 java.awt.Container.add"
虽然我知道错误是什么,但我不知道我应该做什么(不使用 MVC 或寻找某种变通方法)并且希望得到任何帮助。下面我将有两个classes.
的代码
这里是申请 运行 来自:
import javax.swing.*;
import java.awt.*;
/**
* Write a description of class FencingApplication here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Application
{
public static void main(String[] args){
InputView view = new InputView();
InputModel model = new InputModel();
InputController ctrl = new InputController(view, model);
JFrame window = new JFrame("");
window.setSize(500, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = new Container();
c.setLayout(new BorderLayout());
c.add( view, BorderLayout.CENTER );
JButton btList = new JButton( "List" );
JButton btPools = new JButton("Pools");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
buttonPanel.add(btList);
buttonPanel.add(btPools);
c.add( buttonPanel, BorderLayout.NORTH );
btList.addActionListener( ctrl );//where is the action performed method defined
btPools.addActionListener( ctrl );
window.setVisible( true );
}
}
这里是视图 class:
import javax.swing.*; //Jframe/JButton/JLabel/etc
import java.awt.*; //container
import java.util.*;
/**
* Write a description of class InputView here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class InputView extends JFrame implements Observer
{
JLabel lbPaste = new JLabel("Please paste the seeding here.");
JTextArea taPaste = new JTextArea();
JButton btPools = new JButton("Pools");
JLabel lbNum = new JLabel("Please input the number of pools you want to have.");
JTextField tfNum = new JTextField();
public InputView()
{
JPanel numPanel = new JPanel();
numPanel.setLayout(new BorderLayout());
numPanel.add(lbNum, BorderLayout.NORTH);
numPanel.add(tfNum, BorderLayout.CENTER);
JPanel pastePanel = new JPanel();
pastePanel.setLayout(new BorderLayout());
pastePanel.add(lbPaste, BorderLayout.NORTH);
pastePanel.add(new JScrollPane(taPaste), BorderLayout.CENTER);
Container c = getContentPane();
c.setLayout(new GridLayout(2, 1));
c.add(numPanel);
c.add(pastePanel);
setTitle( "Pools" );
setSize( 350, 500 );//width then height
setVisible(true);
setResizable(false);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void update( Observable obs, Object obj )
{
}
}
在此先感谢您的帮助!
Container
是例如的超类Panel
、JPanel
、Window
、JFrame
等
JFrame
是一个 window,所以您不应该(实际上不能,正如您在此处发现的那样)将它添加到另一个组件。 JFrame
是 top-level container.
实际上,您可能根本不应该直接使用 new Container()
。例如,如果你想要一个面板,你应该使用 JPanel
。我很难准确地说出您的意图,因为向另一个组件添加 JFrame
是错误的。我看到您向 c
添加内容,但我没有看到您用它做任何其他事情。
所以:
JFrame
是一个 window.
JFrame
有一个内容面板,也就是window里面的面板。 (默认内容窗格实际上是 JPanel
,尽管 getContentPane()
returns 它是 Container
。)
- 如果您想将内容放入
JFrame
,请将内容添加到内容窗格。
- 您不需要向任何内容添加
JFrame
,只需使用 new
创建它并调用 setVisible(true)
.
这些是如何正确使用 JFrame
的基础知识。
如果您还没有读过,我也强烈推荐 Swing tutorials。他们真的很不错。
所以我尝试使用 MVC(模型、视图、控制器)来格式化我的代码,当我尝试将视图添加到实际应用程序时,我收到一条错误消息“java.lang.IllegalArgumentException:添加一个window 到一个容器 在 java.awt.Container.checkNotAWindow 在 java.awt.Container.addImpl 在 java.awt.Container.add" 虽然我知道错误是什么,但我不知道我应该做什么(不使用 MVC 或寻找某种变通方法)并且希望得到任何帮助。下面我将有两个classes.
的代码这里是申请 运行 来自:
import javax.swing.*;
import java.awt.*;
/**
* Write a description of class FencingApplication here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Application
{
public static void main(String[] args){
InputView view = new InputView();
InputModel model = new InputModel();
InputController ctrl = new InputController(view, model);
JFrame window = new JFrame("");
window.setSize(500, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = new Container();
c.setLayout(new BorderLayout());
c.add( view, BorderLayout.CENTER );
JButton btList = new JButton( "List" );
JButton btPools = new JButton("Pools");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
buttonPanel.add(btList);
buttonPanel.add(btPools);
c.add( buttonPanel, BorderLayout.NORTH );
btList.addActionListener( ctrl );//where is the action performed method defined
btPools.addActionListener( ctrl );
window.setVisible( true );
}
}
这里是视图 class:
import javax.swing.*; //Jframe/JButton/JLabel/etc
import java.awt.*; //container
import java.util.*;
/**
* Write a description of class InputView here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class InputView extends JFrame implements Observer
{
JLabel lbPaste = new JLabel("Please paste the seeding here.");
JTextArea taPaste = new JTextArea();
JButton btPools = new JButton("Pools");
JLabel lbNum = new JLabel("Please input the number of pools you want to have.");
JTextField tfNum = new JTextField();
public InputView()
{
JPanel numPanel = new JPanel();
numPanel.setLayout(new BorderLayout());
numPanel.add(lbNum, BorderLayout.NORTH);
numPanel.add(tfNum, BorderLayout.CENTER);
JPanel pastePanel = new JPanel();
pastePanel.setLayout(new BorderLayout());
pastePanel.add(lbPaste, BorderLayout.NORTH);
pastePanel.add(new JScrollPane(taPaste), BorderLayout.CENTER);
Container c = getContentPane();
c.setLayout(new GridLayout(2, 1));
c.add(numPanel);
c.add(pastePanel);
setTitle( "Pools" );
setSize( 350, 500 );//width then height
setVisible(true);
setResizable(false);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void update( Observable obs, Object obj )
{
}
}
在此先感谢您的帮助!
Container
是例如的超类Panel
、JPanel
、Window
、JFrame
等
JFrame
是一个 window,所以您不应该(实际上不能,正如您在此处发现的那样)将它添加到另一个组件。 JFrame
是 top-level container.
实际上,您可能根本不应该直接使用 new Container()
。例如,如果你想要一个面板,你应该使用 JPanel
。我很难准确地说出您的意图,因为向另一个组件添加 JFrame
是错误的。我看到您向 c
添加内容,但我没有看到您用它做任何其他事情。
所以:
JFrame
是一个 window.JFrame
有一个内容面板,也就是window里面的面板。 (默认内容窗格实际上是JPanel
,尽管getContentPane()
returns 它是Container
。)- 如果您想将内容放入
JFrame
,请将内容添加到内容窗格。 - 您不需要向任何内容添加
JFrame
,只需使用new
创建它并调用setVisible(true)
.
这些是如何正确使用 JFrame
的基础知识。
如果您还没有读过,我也强烈推荐 Swing tutorials。他们真的很不错。