所以我試圖使用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)
{
}
}
在此先感謝您的幫助!