Hallo,
ich versuche gerade mir GridBagLayout ohne GuiBuilder anzueignen. Mit dem folgenden Beispiel will ich ein Fenster erstellen, dass oben eine Menü-Leiste hat und darunter die Arbeitsfläche. Dazu setze ich in einen Container "rootContainer" ein JPanel "rootPanel", das eine menuBar vom Typ JPanel und ein "contentPanel" ebenfalls ein JPanel für die eigentlichen Formular-Elemente enthält.
Das rootPanel soll lediglich ein BorderLayout mit den Ausrichtungen NORTH, CENTER und SOUTH (für ein JPanel buttonBar) erhalten. Dagegen erhält contentPanel das GridBagLayout.
Wenn ich den Code ausführen lasse - ich nutze IntelliJ Community - erhalte ich eine NullPointerException.
Könnt ihr mir zeigen, was ich falsch mache?
Beste Grüße
Sherry
ich versuche gerade mir GridBagLayout ohne GuiBuilder anzueignen. Mit dem folgenden Beispiel will ich ein Fenster erstellen, dass oben eine Menü-Leiste hat und darunter die Arbeitsfläche. Dazu setze ich in einen Container "rootContainer" ein JPanel "rootPanel", das eine menuBar vom Typ JPanel und ein "contentPanel" ebenfalls ein JPanel für die eigentlichen Formular-Elemente enthält.
Das rootPanel soll lediglich ein BorderLayout mit den Ausrichtungen NORTH, CENTER und SOUTH (für ein JPanel buttonBar) erhalten. Dagegen erhält contentPanel das GridBagLayout.
Java:
package com.example.workingdays;
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class MainGui extends JFrame {
public MainGui() {
initComponents();
}
private void initComponents() {
contentPanel = new JPanel();
buttonBar = new JPanel();
//======== this ========
Container rootContainer = getContentPane();
rootContainer.setLayout(new BorderLayout());
//======== rootPanel ========
{
rootPanel.setBorder(new EmptyBorder(12, 12, 0, 0));
rootPanel.setLayout(new BorderLayout());
//======== contentPaneL ========
{
contentPanel.setLayout(new GridBagLayout());
((GridBagLayout)contentPanel.getLayout()).columnWidths = new int[] {80, 110, 96, 0};
((GridBagLayout)contentPanel.getLayout()).rowHeights = new int[] {0, 0, 0, 0};
((GridBagLayout)contentPanel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 1.0};
((GridBagLayout)contentPanel.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 1.0};
//---- Row 1 ----
firstWorkingDayLabel.setText("Erster Arbeitstag");
contentPanel.setBackground(Color.cyan);
contentPanel.add(firstWorkingDayLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 5), 0, 0));
}
rootPanel.add(contentPanel, BorderLayout.CENTER);
//======== MenuBar ========
{
//======== Menu 1 ========
{
file.setText("Datei");
//---- menuItems ----
menuItemExit.setText("Beenden");
file.add(menuItemExit);
}
menuBar.add(file);
//======== Menu 2 ========
{
help.setText("Hilfe");
//---- menuItems ----
menuItemAbout.setText("Über ...");
help.add(menuItemAbout);
}
menuBar.add(help);
}
rootPanel.add(menuBar, BorderLayout.NORTH);
}
rootContainer.add(rootPanel, BorderLayout.CENTER);
pack();
setTitle("Berechnung der Arbeitstage");
setSize(500, 300);
setResizable(true);
setLocationRelativeTo(getOwner());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//======== Main Methode ========
public static void main(String[] args) {
/* Create and display the form */
EventQueue.invokeLater(new Runnable() {
public void run() {
MainGui start = new MainGui();
start.setVisible(true);
}
});
}
// Variables declaration
private JPanel rootPanel;
private JPanel contentPanel;
private JPanel buttonBar;
private JPanel menuBar;
private JMenu file;
private JMenu help;
private JMenuItem menuItemExit;
private JMenuItem menuItemAbout;
private JLabel firstWorkingDayLabel = new JLabel();
private JDateChooser firstWorkingDayDateChooser = new JDateChooser();
private JLabel lastWorkingDayLabel = new JLabel();
private JDateChooser lastWorkingDayDateChooser = new JDateChooser();
private JLabel daysLabel = new JLabel();
private JLabel resultLabel = new JLabel();
private JButton calculateButton = new JButton();
private JButton endButton = new JButton();
private JButton resetButton = new JButton();
private JCheckBox workSaturday = new JCheckBox();
private JCheckBox christmasEve = new JCheckBox();
private JCheckBox silvester = new JCheckBox();
private JComboBox<String> stateComboBox = new JComboBox<String>();
// End of variables declaration
}
Wenn ich den Code ausführen lasse - ich nutze IntelliJ Community - erhalte ich eine NullPointerException.
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.example.workingdays.MainGui.initComponents(MainGui.java:27)
at com.example.workingdays.MainGui.<init>(MainGui.java:14)
at com.example.workingdays.MainGui$1.run(MainGui.java:82)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Könnt ihr mir zeigen, was ich falsch mache?
Beste Grüße
Sherry
Zuletzt bearbeitet: