Reputation: 3
This is my first Java project so I'm sorry for asking what I'm sure is an obvious answer. I've been searching google and every java book I can get my hands on but some how I'm missing it...
I'm creating a project which includes four internal frames in the first class. I would like each of the internal frames to get its content from other classes (rather than group all the content in the main class. The code looks really sloppy and ends up being a few hundred lines long. Too much for navigation. Should this be a concern or am I being too tedious?).
Anyway, this the GUI from my main class (called Main):
private static void createAndShowGUI() {
frame = new JFrame("Perceptum");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(createMenuBar());
desk = new JDesktopPane();
frame1 = new JInternalFrame("Notepad", true, true, true, true);
frame1.setBounds(0, 0, 1088, 500);
frame1.getContentPane().setLayout(new BorderLayout());
frame1.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame1.setVisible(true);
frame2 = new JInternalFrame("Research", true, true, true, true);
frame2.setBounds(1089, 0, 265, 670);
frame2.getContentPane().setLayout(new BorderLayout());
frame2.setVisible(true);
frame3 = new JInternalFrame("Share", true, true, true, true);
frame3.setBounds(790, 500, 300, 170);
frame3.getContentPane().setLayout(new BorderLayout());
frame3.setVisible(true);
frame4 = new JInternalFrame("References", true, true, true, true);
frame4.setBounds(0, 500, 790, 170);
frame4.setVisible(true);
desk.add(frame1);
desk.add(frame2);
desk.add(frame3);
desk.add(frame4);
frame.add(desk);
frame.setSize(1370, 730);
frame.setVisible(true);
}
I would like to put the classes Notepad
, References
, Research
, and Share
into frames 1-4. I've tried using:
Notepad notes = new Notepad();
frame1.getContentPane().add(notes);
also:
frame1.setContentPane(notes)
and similar variations. This is seriously driving me nuts. Thanks for the help!
Upvotes: 0
Views: 776
Reputation: 691755
JFrame is a top-level component. A main window. You can't add a main window to a JInternalFrame. That's what the stack trace says:
java.lang.IllegalArgumentException: adding a window to a container
It's illegal to add a window to a container.
Notepad and the other classes should extend JPanel, not JFrame. JPanel is a component that you can add to a JInternalFrame.
Lesson to learn: even if an error message makes no sense to you, it contains valuable information. If you don't post it, we don't know what the problem is. Code also contains valuable information. If you don't post it, we don't know what it does.
Upvotes: 0
Reputation: 12985
Try breaking the code for creating each frame into a method that then returns the frame. This breaks up the code into readable parts. You can use a Class per frame but that is overkill unless there is a lot of unique stuff going on in the frame.
private JInternalFrame frame1 = null;
...
private JInternalFrame getFrame1() {
if (frame1 == null) {
frame1 = new JInternalFrame("Notepad", true, true, true, true);
frame1.setBounds(0, 0, 1088, 500);
frame1.getContentPane().setLayout(new BorderLayout());
// TODO: Add code to fill in the contents by calling getXxx()
// for each major contained component OR just creating all the
// minor bits that go inside.
frame1.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame1.setVisible(true);
}
return frame1;
}
Then in the method you showed in your question use this line:
desk.add(getFrame1());
Upvotes: 1