我想繪製一個簡單的GUI(在eclipse中使用windowbuilder創建),我想在它們之間有2個按鈕和一個可滾動的文本區域。我創建了下面的代碼來實現上述:簡單gui中的java.awt.Container.checkNotAWindow錯誤
public class Main extends JFrame implements ActionListener{
public Font font; //used for the font file
public JTextArea txtDataWillBe;
public Main() throws FontFormatException, IOException{
setTitle("Main title ");
setBounds(100, 100, 1200, 600);
getContentPane().setLayout(null);
txtDataWillBe = new JTextArea();
txtDataWillBe.setText("Your data will display here");
txtDataWillBe.setFont(new Font("Droid Sans", Font.BOLD, 18));
txtDataWillBe.setEditable(false);
txtDataWillBe.setColumns(1);
txtDataWillBe.setBounds(0, 40, 919, 484);
getContentPane().add(txtDataWillBe);
JButton button = new JButton("CLICK TO OPEN");
button.setBounds(0, 0, 940, 40);
button.setFont(new Font("Coalition", Font.PLAIN, 18));
getContentPane().add(button);
JButton btnPrint = new JButton("PRINT");
btnPrint.setBounds(0, 531, 940, 40);
btnPrint.setFont(new Font("Coalition", Font.PLAIN, 18));
getContentPane().add(btnPrint);
}
private final String JTextFile = null;
JFileChooser chooser;
String choosertitle;
public static File deletefile;
編輯:
public static void main(String s[]) {
JFrame frame = new JFrame("Reader");
Main panel = null;
try {
panel = new Main();
} catch (FontFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
File deleteme = new File (deletefile + "mx.txt");
deleteme.delete();
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(panel.getPreferredSize());
frame.setVisible(true);
}
我本來裏面一個JScrollPane(以爲那就是,以獲得最佳的方式JTextArea的滾動我要工作)。我刪除了JScrollPane,認爲這是導致控制檯錯誤,但我仍然收到錯誤。
控制檯輸出是:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:439)
at java.awt.Container.addImpl(Container.java:1035)
at java.awt.Container.add(Container.java:923)
編輯:主上面添加。
我在做什麼錯我的GUI?
我是否需要JScrollPane和JTextArea來啓用垂直滾動加載的文本?
感謝您的幫助;
安迪
編輯:
我已經市價修改的建議之下,所以我的代碼,原先爲:
public Main() throws FontFormatException, IOException{
JFrame frame = new JFrame("Reader ");
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
File deleteme = new File (deletefile + "mx.txt");
deleteme.delete();
System.exit(0);
}
}
);
frame.getContentPane().add(panel,"Center");
frame.setSize(getPreferredSize());
frame.setVisible(true);
代碼的其餘部分之前,但所有我收到顯示是一個空白的灰色框架,沒有任何我的組件(儘管它們都顯示在windowbuilder中)。
感謝您的繼續幫助。
主要不在您的示例中顯示,但我懷疑您將框架添加到另一個框架或對話框。 – 2013-04-11 12:40:45
不相關:不要做任何手動調整/定位組件。而是使用合適的LayoutManager。 – kleopatra 2013-04-11 12:52:53
謝謝。我猜我用指定方式的問題是,如果用戶最大化,那麼沒有東西會正確繪製?我沒有相對佈局,所以去了絕對。 – andy 2013-04-11 15:48:21