KungFuKugelfisch KungFuKugelfisch - 3 months ago 14
Java Question

I've trying to add a JPanel to my JFrame on click,but i dont find my error

i am trying to get some Java skills back because i worked for a long time in webdevelopment. Now iam just create a main Jframe where are a little Menu and a Class createSchueler extends JPanel.

So what i want to do if you go into the Menu click Neu > Schueler

A new Jpanel should be created and added to the Window

Sry for my bad english...
i am so bad in it hope you can understand me :(

Main Class

public class MainWindow {

private JFrame frame;


/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public MainWindow() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 807, 541);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JPanel createSchueler = new createSchueler();

JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

JMenu mnNewMenu = new JMenu("Neu");
mnNewMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
createSchueler.setVisible(true);
frame.getContentPane().add(createSchueler);

}
});
menuBar.add(mnNewMenu);

JMenuItem mntmSchler = new JMenuItem("Sch\u00FCler");
mnNewMenu.add(mntmSchler);

JMenu mnBearbeiten = new JMenu("Bearbeiten");
menuBar.add(mnBearbeiten);

JMenuItem mntmSchler_1 = new JMenuItem("Sch\u00FCler");
mnBearbeiten.add(mntmSchler_1);


}
}


The createSchueler Class extends JPanel that i want to add to the frame

public class createSchueler extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;

/**
* Create the panel.
*/
public createSchueler() {
setLayout(null);

JLabel lblNeuenSchuelerErstellen = new JLabel("Neuen Schueler erstellen");
lblNeuenSchuelerErstellen.setFont(new Font("Tahoma", Font.PLAIN, 22));
lblNeuenSchuelerErstellen.setBounds(29, 27, 268, 27);
add(lblNeuenSchuelerErstellen);

JLabel lblVorname = new JLabel("Vorname");
lblVorname.setBounds(29, 102, 46, 14);
add(lblVorname);

textField = new JTextField();
textField.setBounds(97, 99, 172, 20);
add(textField);
textField.setColumns(10);

JLabel lblNachname = new JLabel("Nachname");
lblNachname.setBounds(29, 133, 69, 14);
add(lblNachname);

textField_1 = new JTextField();
textField_1.setBounds(97, 130, 172, 20);
add(textField_1);
textField_1.setColumns(10);

JLabel lblGeburtstag = new JLabel("Geburtstag");
lblGeburtstag.setBounds(29, 169, 69, 14);
add(lblGeburtstag);

textField_2 = new JTextField();
textField_2.setBounds(97, 166, 172, 20);
add(textField_2);
textField_2.setColumns(10);

ButtonGroup bg = new ButtonGroup();

JRadioButton rdbtnMnnlich = new JRadioButton("M\u00E4nnlich");
rdbtnMnnlich.setBounds(29, 206, 69, 23);
bg.add(rdbtnMnnlich);

JRadioButton rdbtnWeiblich = new JRadioButton("Weiblich");
rdbtnWeiblich.setBounds(97, 206, 109, 23);
bg.add(rdbtnWeiblich);



}
}


Everything is importet hopefully :D

thanks guy again sry for my poor english

Answer

Use a CardLayout to swap JPanels rather than adding them by hand. If you must add them by hand, be sure that the receiving container's layout manager is up to the task and handles the addition well. For example BorderLayout would take it fine, but GroupLayout won't. If you do add or remove by hand, call revalidate(); and repaint() on the container after these changes.

Also you're using null layout in your added JPanel and contentPane which will completely ruin it's preferredSize calculation. Never use this layout. Learn to use and then use the layout managers. This is your main mistake.

For more on this, please read: Why is it frowned upon to use a null layout in Swing?