Hallo,
ich bin gerade dabei ein Programm zu erstellen, welches die Funktion besitzt, das Layout eine JPanel's zu ändern.
Anbei der Quelltext (bis jetz fast noch ohne Komentare)
Ich möchte allerdings nicht, dass schon direkt beim Start des Programms die 5 Labels angezeigt werden.
Wisst ihr, wie ich das hinbekomme?
Viele Grüße
ich bin gerade dabei ein Programm zu erstellen, welches die Funktion besitzt, das Layout eine JPanel's zu ändern.
Anbei der Quelltext (bis jetz fast noch ohne Komentare)
Java:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;
public class LayoutChangeGUI extends JFrame {
private JRadioButton optionLayoutWechsel1, optionLayoutWechsel2, optionLayoutWechsel3, optionLayoutWechsel4;
private JButton schaltflaecheBeenden;
private ButtonGroup gruppe;
private JLabel labelFeld1, labelFeld2, labelFeld3, labelFeld4, labelFeld5;
private JPanel panelAuswahl, panelLayout;
class MeinListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if (optionLayoutWechsel1.isSelected() == true) {
panelLayout.setLayout(new GridLayout(0,5));
validate();
}
if (optionLayoutWechsel2.isSelected() == true) {
panelLayout.setLayout(new GridLayout(4,0));
validate();
}
if (optionLayoutWechsel3.isSelected()== true) {
panelLayout.setLayout(new FlowLayout());
validate();
}
if (optionLayoutWechsel4.isSelected() == true) {
panelLayout.setLayout(new GridLayout(0,2));
validate();
}
}
}
public LayoutChangeGUI(String titel) {
super(titel);
panelAuswahl = panelAuswahlErzeugen();
labelFeld1 = new JLabel("Label 1");
labelFeld2 = new JLabel("Label 2");
labelFeld3 = new JLabel("Label 3");
labelFeld4 = new JLabel("Label 4");
labelFeld5 = new JLabel("Label 5");
panelLayout = panelLayoutErzeugen();
add(panelAuswahl);
add(panelLayout);
//die Standard-Aktion setzen
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,0));
//packen und anzeigen
pack();
setVisible(true);
//Größenänderungen sind nicht zugelassen
//damit das mühsam erstellte Layout nicht durcheinander kommt
}
private JPanel panelAuswahlErzeugen() {
JPanel tempPanel = new JPanel();
JPanel gruppenPanel = new JPanel();
JPanel buttonPanel = new JPanel();
gruppe = new ButtonGroup();
optionLayoutWechsel1 = new JRadioButton("Layout 1");
optionLayoutWechsel2 = new JRadioButton("Layout 2");
optionLayoutWechsel3 = new JRadioButton("Layout 3");
optionLayoutWechsel4 = new JRadioButton("Layout 4");
gruppe.add(optionLayoutWechsel1);
gruppe.add(optionLayoutWechsel2);
gruppe.add(optionLayoutWechsel3);
gruppe.add(optionLayoutWechsel4);
MeinListener listener = new MeinListener();
optionLayoutWechsel1.addItemListener(listener);
optionLayoutWechsel2.addItemListener(listener);
optionLayoutWechsel3.addItemListener(listener);
optionLayoutWechsel4.addItemListener(listener);
schaltflaecheBeenden = new JButton("Beenden");
schaltflaecheBeenden.setActionCommand("ende");
tempPanel.setLayout(new GridLayout(0,2,200,0));
tempPanel.setBorder(new TitledBorder ("Ihre Auswahlmöglichkeiten:"));
gruppenPanel.add(optionLayoutWechsel1);
gruppenPanel.add(optionLayoutWechsel2);
gruppenPanel.add(optionLayoutWechsel3);
gruppenPanel.add(optionLayoutWechsel4);
buttonPanel.add(new JLabel());
buttonPanel.add(schaltflaecheBeenden);
buttonPanel.add(new JLabel());
gruppenPanel.setLayout(new GridLayout(4,0));
buttonPanel.setLayout(new GridLayout(3,0));
tempPanel.add(gruppenPanel);
tempPanel.add(buttonPanel);
return tempPanel;
}
private JPanel panelLayoutErzeugen() {
JPanel tempPanel = new JPanel();
tempPanel.add(labelFeld1);
tempPanel.add(labelFeld2);
tempPanel.add(labelFeld3);
tempPanel.add(labelFeld4);
tempPanel.add(labelFeld5);
return tempPanel;
}
}
Ich möchte allerdings nicht, dass schon direkt beim Start des Programms die 5 Labels angezeigt werden.
Wisst ihr, wie ich das hinbekomme?
Viele Grüße