Hallo zusammen,
ich habe das erste Mal mit dem GridbagLayout herum expermentiert und es funktioniert leider nicht so wie ich es gerne hätte.
Meine ganzen Labels und Textfelder, die in einem mainPanel liegen erscheinen nicht sofort, sondern erst wenn ich das Frame verklineere oder vergrößere. :autsch:
Ich habe vor ein Adressbuch zu erstellen, aber nur zu Übungszwecken, da ich in der Swing-Programmierung noch ziemlich grün hinter den Ohren bin.
Ich bin für jeden Hinweis und Verbesserungstipp sehr Dankbar.
Das ist mein Code für die GUI:
Vorab vielen Dank für eure Hilfe.
Viele Grüße
Edfred
ich habe das erste Mal mit dem GridbagLayout herum expermentiert und es funktioniert leider nicht so wie ich es gerne hätte.
Meine ganzen Labels und Textfelder, die in einem mainPanel liegen erscheinen nicht sofort, sondern erst wenn ich das Frame verklineere oder vergrößere. :autsch:
Ich habe vor ein Adressbuch zu erstellen, aber nur zu Übungszwecken, da ich in der Swing-Programmierung noch ziemlich grün hinter den Ohren bin.
Ich bin für jeden Hinweis und Verbesserungstipp sehr Dankbar.
Das ist mein Code für die GUI:
Java:
package de.home.addressbook.gui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import de.home.addressbook.actions.CloseButtonActionListenerImpl;
import de.home.addressbook.entry.AddressbookEntry;
import de.home.addressbook.utils.Constants;
public class AddressbookGUI implements ActionListener{
/**
* Autogenerated serial version ID
*/
private static final long serialVersionUID = -4103836616194940083L;
private JTextField givennameField, secondGivennameField, surenameField, birthdayField,
streetField, houseNumberField, placeField, zipField,
emailField, phoneField, mobileField;
private JLabel givennameLabel, secondGivennameLabel, surenameLabel, birthdayLabel,
streetLabel, houseNumberLabel, placeLabel, zipLabel,
emailLabel, phoneLabel, mobileLabel, pictureLabel;
private JList listOfEntries;
private JPanel picturePanel;
private JPanel mainContentPanel;
private JScrollPane listScrollPane;
private JMenuBar myMenuBar;
private JMenu file;
private JMenuItem newPerson, close, savetoDatabase, saveToFile;
private ImageIcon profilePicture;
private AddressbookEntry entry;
private ArrayList<String> listData;
private JFrame frame;
public AddressbookGUI() {
this.entry = new AddressbookEntry();
this.frame = new JFrame(Constants.FRAME_TITLE_DE);
this.listData = new ArrayList<String>();
}
public void createView() {
initFrame();
initLabelsAndFields();
initContent();
}
private void initContent() {
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints constraintsPictureL = new GridBagConstraints();
GridBagConstraints constraintsPictureP = new GridBagConstraints();
GridBagConstraints constraintsGivennameL = new GridBagConstraints();
GridBagConstraints constraintsGivennameF = new GridBagConstraints();
GridBagConstraints constraints2ndGivennameL = new GridBagConstraints();
GridBagConstraints constraints2ndGivennameF = new GridBagConstraints();
GridBagConstraints constraintsSurenameL = new GridBagConstraints();
GridBagConstraints constraintsSurenameF = new GridBagConstraints();
GridBagConstraints constraintsBirthdayL = new GridBagConstraints();
GridBagConstraints constraintsBirthdayF = new GridBagConstraints();
GridBagConstraints constraintsStreetL = new GridBagConstraints();
GridBagConstraints constraintsStreetF = new GridBagConstraints();
GridBagConstraints constraintsHouseNoL = new GridBagConstraints();
GridBagConstraints constraintsHouseNoF = new GridBagConstraints();
GridBagConstraints constraintsPlaceL = new GridBagConstraints();
GridBagConstraints constraintsPlaceF = new GridBagConstraints();
GridBagConstraints constraintsZipL = new GridBagConstraints();
GridBagConstraints constraintsZipF = new GridBagConstraints();
GridBagConstraints constraintsEmailL = new GridBagConstraints();
GridBagConstraints constraintsEmailF = new GridBagConstraints();
GridBagConstraints constraintsPhoneL = new GridBagConstraints();
GridBagConstraints constraintsPhoneF = new GridBagConstraints();
GridBagConstraints constraintsMobileL = new GridBagConstraints();
GridBagConstraints constraintsMobileF = new GridBagConstraints();
mainContentPanel = new JPanel();
mainContentPanel.setLayout(gbl);
picturePanel = new JPanel();
/*
* If no entries available, no information should be available.
* mainContentPanel can be set to not enabled.
*/
// if(listData.size() != 0 || listData == null) {
int countY = 0;
int none = GridBagConstraints.NONE;
int horizontal = GridBagConstraints.HORIZONTAL;
constraintsPictureL.fill = horizontal;
constraintsPictureL.gridx = 0;
constraintsPictureL.gridy = countY;
constraintsPictureL.insets = new Insets(2, 5, 25, 50); // top left bot right
constraintsPictureL.anchor = GridBagConstraints.CENTER;
mainContentPanel.add(pictureLabel, constraintsPictureL);
constraintsPictureP.fill = horizontal;
constraintsPictureP.gridx = 1;
constraintsPictureP.gridy = ++countY;
constraintsPictureP.anchor = GridBagConstraints.CENTER;
constraintsPictureP.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(picturePanel, constraintsPictureP);
constraintsGivennameL.fill = horizontal;
constraintsGivennameL.gridx = 0;
constraintsGivennameL.gridy = ++countY;
constraintsGivennameL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsGivennameL.weightx = 1.5;
constraintsGivennameL.gridwidth = 2;
constraintsGivennameL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(givennameLabel,constraintsGivennameL);
constraintsGivennameF.fill = horizontal;
constraintsGivennameF.gridx = 1;
constraintsGivennameF.gridy = countY;
constraintsGivennameF.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(givennameField, constraintsGivennameF);
constraints2ndGivennameL.fill = none;
constraints2ndGivennameL.gridx = 0;
constraints2ndGivennameL.gridy = ++countY;
constraints2ndGivennameL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraints2ndGivennameL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(secondGivennameLabel,constraints2ndGivennameL);
constraints2ndGivennameF.fill = horizontal;
constraints2ndGivennameF.gridx = 1;
constraints2ndGivennameF.gridy = countY;
constraints2ndGivennameF.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(secondGivennameField, constraints2ndGivennameF);
constraintsSurenameL.fill = none;
constraintsSurenameL.gridx = 0;
constraintsSurenameL.gridy = ++countY;
constraintsSurenameL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsSurenameL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(surenameLabel, constraintsSurenameL);
constraintsSurenameF.fill = horizontal;
constraintsSurenameF.gridx = 1;
constraintsSurenameF.gridy = countY;
constraintsSurenameF.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(surenameField, constraintsSurenameF);
constraintsBirthdayL.fill = none;
constraintsBirthdayL.gridx = 0;
constraintsBirthdayL.gridy = ++countY;
constraintsBirthdayL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsBirthdayL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(birthdayLabel, constraintsBirthdayL);
constraintsBirthdayF.fill = horizontal;
constraintsBirthdayF.gridx = 1;
constraintsBirthdayF.gridy = countY;
constraintsBirthdayF.insets = new Insets(2, 5, 2, 125);
mainContentPanel.add(birthdayField, constraintsBirthdayF);
constraintsStreetL.fill = none;
constraintsStreetL.gridx = 0;
constraintsStreetL.gridy = ++countY;
constraintsStreetL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsStreetL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(streetLabel, constraintsStreetL);
constraintsStreetF.fill = horizontal;
constraintsStreetF.gridx = 1;
constraintsStreetF.gridy = countY;
constraintsStreetF.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(streetField, constraintsStreetF);
constraintsHouseNoL.fill = none;
constraintsHouseNoL.gridx = 0;
constraintsHouseNoL.gridy = ++countY;
constraintsHouseNoL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsHouseNoL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(houseNumberLabel, constraintsHouseNoL);
constraintsHouseNoF.fill = horizontal;
constraintsHouseNoF.gridx = 1;
constraintsHouseNoF.gridy = countY;
constraintsHouseNoF.insets = new Insets(2, 5, 2, 150);
mainContentPanel.add(houseNumberField, constraintsHouseNoF);
constraintsZipL.fill = none;
constraintsZipL.gridx = 0;
constraintsZipL.gridy = ++countY;
constraintsZipL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsZipL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(zipLabel, constraintsZipL);
constraintsZipF.fill = horizontal;
constraintsZipF.gridx = 1;
constraintsZipF.gridy = countY;
constraintsZipF.insets = new Insets(2, 5, 2, 150);
mainContentPanel.add(zipField, constraintsZipF);
constraintsPlaceL.fill = none;
constraintsPlaceL.gridx = 0;
constraintsPlaceL.gridy = ++countY;
constraintsPlaceL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsPlaceL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(placeLabel, constraintsPlaceL);
constraintsPlaceF.fill = horizontal;
constraintsPlaceF.gridx = 1;
constraintsPlaceF.gridy = countY;
constraintsPlaceF.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(placeField, constraintsPlaceF);
constraintsEmailL.fill = none;
constraintsEmailL.gridx = 0;
constraintsEmailL.gridy = ++countY;
constraintsEmailL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsEmailL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(emailLabel, constraintsEmailL);
constraintsEmailF.fill = horizontal;
constraintsEmailF.gridx = 1;
constraintsEmailF.gridy = countY;
constraintsEmailF.insets = new Insets(5, 5, 5, 50);
mainContentPanel.add(emailField, constraintsEmailF);
constraintsPhoneL.fill = none;
constraintsPhoneL.gridx = 0;
constraintsPhoneL.gridy = ++countY;
constraintsPhoneL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsPhoneL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(phoneLabel, constraintsPhoneL);
constraintsPhoneF.fill = horizontal;
constraintsPhoneF.gridx = 1;
constraintsPhoneF.gridy = countY;
constraintsPhoneF.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(phoneField, constraintsPhoneF);
constraintsMobileL.fill = none;
constraintsMobileL.gridx = 0;
constraintsMobileL.gridy = ++countY;
constraintsMobileL.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING;
constraintsMobileL.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(mobileLabel, constraintsMobileL);
constraintsMobileF.fill = horizontal;
constraintsMobileF.gridx = 1;
constraintsMobileF.gridy = countY;
constraintsMobileF.insets = new Insets(2, 5, 2, 50);
mainContentPanel.add(mobileField, constraintsMobileF);
// } else {
// mainContentPanel.setEnabled(false);
// }
listOfEntries = new JList(listData.toArray());
listOfEntries.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listOfEntries.setLayoutOrientation(JList.VERTICAL);
listOfEntries.setSize(600, 800);
listOfEntries.setFixedCellWidth(200);
listOfEntries.setFixedCellHeight(30);
listScrollPane = new JScrollPane(listOfEntries,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
listScrollPane, mainContentPanel);
split.setOneTouchExpandable(true);
split.setDividerLocation(175);
DefaultListModel listModel = new DefaultListModel();
listModel.addElement(listData);
frame.getContentPane().add(split);
}
private void initFrame() {
/*
* Setting layout of the main frame
*/
frame.getContentPane().setLayout(new BorderLayout());
frame.setSize(Constants.FRAME_SIZE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation( (Constants.SCREEN_SIZE.width - frame.getSize().width)/2,
(Constants.SCREEN_SIZE.height - frame.getSize().height)/2);
myMenuBar = new JMenuBar();
myMenuBar.setBackground(Constants.GRAY);
file = new JMenu(Constants.MENU_FILE_DE);
file.setBackground(Constants.GRAY);
newPerson = new JMenuItem(Constants.MENU_NEW_PERSON_DE);
savetoDatabase = new JMenuItem(Constants.MENU_SAVE_TO_DB_DE);
saveToFile = new JMenuItem(Constants.MENU_SAVE_TO_FILE_DE);
close = new JMenuItem(Constants.MENU_CLOSE_DE);
file.add(newPerson);
file.add(savetoDatabase);
file.add(saveToFile);
file.add(close);
newPerson.addActionListener(this);
// savetoDatabase.addActionListener();
// saveToFile.addActionListener();
close.addActionListener(new CloseButtonActionListenerImpl(close));
myMenuBar.add(file);
frame.getContentPane().add(myMenuBar, BorderLayout.NORTH);
frame.setVisible(Constants.FRAME_VISIBILITY);
frame.validate();
frame.repaint();
}
private void initLabelsAndFields() {
// ---------------------------------------------------
// * Init labels *
// ---------------------------------------------------
givennameLabel = new JLabel(Constants.GIVENNAME_LABEL_DE);
givennameLabel.setSize(Constants.LABEL_SIZE);
secondGivennameLabel = new JLabel(Constants.SECOND_GIVENNAME_LABEL_DE);
secondGivennameLabel.setSize(Constants.LABEL_SIZE);
surenameLabel = new JLabel(Constants.SURENAME_LABEL_DE);
surenameLabel.setSize(Constants.LABEL_SIZE);
birthdayLabel = new JLabel(Constants.BIRTHDAY_LABEL_DE);
birthdayLabel.setSize(Constants.LABEL_SIZE);
streetLabel = new JLabel(Constants.STREET_LABEL_DE);
streetLabel.setSize(Constants.LABEL_SIZE);
houseNumberLabel = new JLabel(Constants.HOUSENUMBER_LABEL_DE);
houseNumberLabel.setSize(Constants.LABEL_SIZE);
placeLabel = new JLabel(Constants.PLACE_LABEL_DE);
placeLabel.setSize(Constants.LABEL_SIZE);
zipLabel = new JLabel(Constants.ZIP_LABEL_DE);
zipLabel.setSize(Constants.LABEL_SIZE);
emailLabel = new JLabel(Constants.EMAIL_LABEL_DE);
emailLabel.setSize(Constants.LABEL_SIZE);
phoneLabel = new JLabel(Constants.PHONE_LABEL_DE);
phoneLabel.setSize(Constants.LABEL_SIZE);
mobileLabel = new JLabel(Constants.MOBILE_LABEL_DE);
mobileLabel.setSize(Constants.LABEL_SIZE);
pictureLabel = new JLabel("Bild von: ");
pictureLabel.setSize(Constants.LABEL_SIZE);
// ---------------------------------------------------
// * Init fields *
// ---------------------------------------------------
int count = 0;
givennameField = new JTextField(
entry.getPerson().getGivenname() == null ? "" : entry.getPerson().getGivenname());
givennameField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
secondGivennameField = new JTextField(
entry.getPerson().getSecondGivenname() == null ? "" : entry.getPerson().getSecondGivenname());
secondGivennameField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
surenameField = new JTextField(
entry.getPerson().getSurename() == null ? "" : entry.getPerson().getSurename());
surenameField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
birthdayField = new JTextField(
entry.getPerson().getBirthday() == null ? "" : entry.getPerson().getBirthday().toString());
birthdayField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
streetField = new JTextField(
entry.getAddress().getStreet() == null ? "" : entry.getAddress().getStreet());
streetField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
houseNumberField = new JTextField(
entry.getAddress().getHousenumber() == null ? "" : entry.getAddress().getHousenumber());
houseNumberField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
placeField = new JTextField(
entry.getAddress().getPlace() == null ? "" : entry.getAddress().getPlace());
placeField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
zipField = new JTextField(
entry.getAddress().getZip() == 0 ? "" : String.valueOf(entry.getAddress().getZip()));
zipField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
emailField = new JTextField(
entry.getPerson().getEmail() == null ? "" : entry.getPerson().getEmail());
emailField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
phoneField = new JTextField(
entry.getPerson().getPhone() == null ? "" : entry.getPerson().getPhone());
phoneField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
mobileField = new JTextField(
entry.getPerson().getMobile() == null ? "" : entry.getPerson().getMobile());
mobileField.setBackground(count%2==0 ? Constants.GRAY : Constants.WHITE);
count += 1;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(newPerson)) {
new NewPersonEntryGUI(this, frame).initDialog();
}
}
/*
* For testing
*/
public static void main(String[] args) {
new AddressbookGUI().createView();
}
// ---------------------------------------------------------------
// * Getter- and Setter methods *
// ---------------------------------------------------------------
/**
* @return the givennameField
*/
public JTextField getGivennameField() {
return givennameField;
}
/**
* @return the secondGivennameField
*/
public JTextField getSecondGivennameField() {
return secondGivennameField;
}
/**
* @return the surenameField
*/
public JTextField getSurenameField() {
return surenameField;
}
/**
* @return the birthdayField
*/
public JTextField getBirthdayField() {
return birthdayField;
}
/**
* @return the streetField
*/
public JTextField getStreetField() {
return streetField;
}
/**
* @return the houseNumberField
*/
public JTextField getHouseNumberField() {
return houseNumberField;
}
/**
* @return the placeField
*/
public JTextField getPlaceField() {
return placeField;
}
/**
* @return the zipField
*/
public JTextField getZipField() {
return zipField;
}
/**
* @return the emailField
*/
public JTextField getEmailField() {
return emailField;
}
/**
* @return the phoneField
*/
public JTextField getPhoneField() {
return phoneField;
}
/**
* @return the mobileField
*/
public JTextField getMobileField() {
return mobileField;
}
/**
* @return the profilePicture
*/
public ImageIcon getProfilePicture() {
return profilePicture;
}
/**
* @return the listData
*/
public ArrayList<String> getListData() {
return listData;
}
/**
* @param givennameField the givennameField to set
*/
public void setGivennameField(JTextField givennameField) {
this.givennameField = givennameField;
}
/**
* @param secondGivennameField the secondGivennameField to set
*/
public void setSecondGivennameField(JTextField secondGivennameField) {
this.secondGivennameField = secondGivennameField;
}
/**
* @param surenameField the surenameField to set
*/
public void setSurenameField(JTextField surenameField) {
this.surenameField = surenameField;
}
/**
* @param birthdayField the birthdayField to set
*/
public void setBirthdayField(JTextField birthdayField) {
this.birthdayField = birthdayField;
}
/**
* @param streetField the streetField to set
*/
public void setStreetField(JTextField streetField) {
this.streetField = streetField;
}
/**
* @param houseNumberField the houseNumberField to set
*/
public void setHouseNumberField(JTextField houseNumberField) {
this.houseNumberField = houseNumberField;
}
/**
* @param placeField the placeField to set
*/
public void setPlaceField(JTextField placeField) {
this.placeField = placeField;
}
/**
* @param zipField the zipField to set
*/
public void setZipField(JTextField zipField) {
this.zipField = zipField;
}
/**
* @param emailField the emailField to set
*/
public void setEmailField(JTextField emailField) {
this.emailField = emailField;
}
/**
* @param phoneField the phoneField to set
*/
public void setPhoneField(JTextField phoneField) {
this.phoneField = phoneField;
}
/**
* @param mobileField the mobileField to set
*/
public void setMobileField(JTextField mobileField) {
this.mobileField = mobileField;
}
/**
* @param profilePicture the profilePicture to set
*/
public void setProfilePicture(ImageIcon profilePicture) {
this.profilePicture = profilePicture;
}
/**
* @param listData the listData to set
*/
public void setListData(ArrayList<String> listData) {
this.listData = listData;
}
public void setEntry(AddressbookEntry entry) {
this.entry = entry;
}
public AddressbookEntry getEntry() {
return entry;
}
/**
* @return the frame
*/
// public JFrame getFrame() {
// return frame;
// }
/**
* @param frame the frame to set
*/
// public void setFrame(JFrame frame) {
// this.frame = frame;
// }
/**
* @return the mainContentPanel
*/
public JPanel getMainContentPanel() {
return mainContentPanel;
}
/**
* @param mainContentPanel the mainContentPanel to set
*/
public void setMainContentPanel(JPanel mainContentPanel) {
this.mainContentPanel = mainContentPanel;
}
}
Vorab vielen Dank für eure Hilfe.
Viele Grüße
Edfred