Hallo,
ich versuche mich gerade mit Swing und habe ier auch den Windows Builder in Eclipse installiert.
Ich scheitere aber gerade bei einem "Seitenwechsel".
Also sowas möche ich machen:
- Je nachdem wo man draufklickt wird in dem "Contentbereich" Seite1 oder Seite2 angezeigt.
Aber das funktioniert nicht.
Was mache ich falsch?
ich versuche mich gerade mit Swing und habe ier auch den Windows Builder in Eclipse installiert.
Ich scheitere aber gerade bei einem "Seitenwechsel".
Also sowas möche ich machen:
- Je nachdem wo man draufklickt wird in dem "Contentbereich" Seite1 oder Seite2 angezeigt.
Aber das funktioniert nicht.
Java:
public class MainWindow {
private JFrame frame;
private JTextField textField;
/**
* 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("Test");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EventQueue.invokeLater(() -> frame.setVisible(true));
JPanel cardLayout = new JPanel(new CardLayout());
GridBagConstraints gbcCards = new GridBagConstraints();
gbcCards.gridx = 0;
gbcCards.gridy = 1;
gbcCards.gridwidth = 2;
gbcCards.fill = java.awt.GridBagConstraints.BOTH;
gbcCards.weightx = 1.0;
gbcCards.weighty = 1.0;
frame.getContentPane().add(cardLayout);
final JPanel panel = new JPanel(new FlowLayout());
cardLayout.add(panel, "page1");
textField = new JTextField();
textField.setBounds(277, 5, 130, 26);
panel.add(textField);
textField.setColumns(10);
final JPanel panel_1 = new JPanel(new FlowLayout());
cardLayout.add(panel_1, "page2");
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(311, 5, 61, 16);
panel_1.add(lblNewLabel);
ItemListener buttonSelectListener = (ItemEvent e) -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
showCard(((AbstractButton) e.getSource()).getActionCommand(), cardLayout);
}
};
//////////
// Menu
//////////
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnNewMenu_2 = new JMenu("Allgemein");
menuBar.add(mnNewMenu_2);
JMenuItem mntmNewMenuItem = new JMenuItem("Dashboard");
mntmNewMenuItem.setActionCommand("page1");
mntmNewMenuItem.addItemListener(buttonSelectListener);
mnNewMenu_2.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("Einstellung");
mntmNewMenuItem_2.setActionCommand("page2");
mntmNewMenuItem_2.addItemListener(buttonSelectListener);
mnNewMenu_2.add(mntmNewMenuItem_2);
}
/**
* holder muss ein CardLayout haben
*
* @param key
* @param holder
*/
private void showCard(String key, JPanel holder) {
((CardLayout) holder.getLayout()).show(holder, key);
}
Was mache ich falsch?