2016-04-09 24 views
0

所以我試圖使用MVC(模型,視圖,控制器)來格式化我的代碼,當試圖將視圖添加到實際應用程序時,我得到一個錯誤,指出「java.lang.IllegalArgumentException:添加一個窗口對容器 在java.awt.Container.checkNotAWindow 在java.awt.Container.addImpl 在java.awt.Container.add「 雖然我知道錯誤是什麼,我不知道我應該做什麼(不使用MVC或找一些解決方法),並會感謝任何幫助。下面我會有兩個類的代碼。IllegalArgumentException錯誤

這裏就是應用程序是從運行:

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); 
    } 
} 

這裏是視圖類:

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) 
    { 
    } 
} 

在此先感謝您的幫助!

回答

0

Container是例如PanelJPanelWindowJFrame

JFrame是一個窗口,所以你不應該(而且事實上不能,因爲你發現了這裏)將它添加到另一個組件。 JFrametop-level container

實際上,可能是這種情況,您根本不應該直接使用new Container()。例如,如果你想要一個面板,你應該使用JPanel。對於我來說,確切地告訴你想要什麼是很難的,因爲向另一個組件添加JFrame是一個錯誤。我看到你給c添加東西,但我沒有看到你用它做任何事情。

所以:

  • JFrame是一個窗口。
  • JFrame有一個內容窗格,它是窗口內的面板。 (默認內容窗格實際上是JPanel,儘管getContentPane()將其返回爲Container。)
  • 如果要將東西放在JFrame中,請將這些內容添加到內容窗格中。
  • 你不需要添加JFrame任何東西,只需使用new創建它並致電setVisible(true)

這些是如何正確使用JFrame的基礎知識。

如果您還沒有閱讀,我強烈推薦Swing tutorials。他們真的很不錯。

相關問題