Trying to teach myself Java GUI (Swing), should be an easy fix

。_饼干妹妹 提交于 2019-12-11 03:26:24

问题


Alright, like the title says, this should be an easy question but I'm very new to doing gui and I'm having a little problem.
I'm trying to separate my window into 3 bordered sections (horizontal). So far I've got two, however, the one in the middle extends down to the bottom of the window, blocking the bottom section. I'm guessing it has something to do with my use of NORTH, CENTER, and SOUTH? I attached a picture of the window and some of my code, let me know if you need more!

Top section

public NamePanel(){
    Dimension size = getPreferredSize();
    size.height = 125;
    setPreferredSize(size);
    //setBorder(BorderFactory.createTitledBorder("Who owes who money?"));
    setBorder(BorderFactory.createTitledBorder("Who?"));

    JRadioButton button;
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    button = new JRadioButton("Bob");
    gc.anchor = GridBagConstraints.CENTER;
    gc.weightx = 0.5;
    gc.weighty = 0.0;
    gc.gridx = 1;
    gc.gridy = 0;
    add(button, gc);

    button = new JRadioButton("Sue");
    gc.weighty = 0.5;
    gc.gridx = 3;
    gc.gridy = 0;
    add(button, gc);

Middle Section

public ActionsPanel(){
    Dimension size = getPreferredSize();
    size.height = 75;
    setPreferredSize(size);
    setBorder(BorderFactory.createTitledBorder("What would you like to do?"));

    JRadioButton button;
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    button = new JRadioButton("Add");
    gc.anchor = GridBagConstraints.NORTH;
    gc.weightx = 0.5;
    gc.weighty = 0.0;
    gc.gridx = 1;
    gc.gridy = 0;
    add(button, gc);

Bottom Section (hidden in the picture)

public EntryPanel(){
     Dimension size = getPreferredSize();
     size.height = 75;
     setPreferredSize(size);
     setBorder(BorderFactory.createTitledBorder("Enter the transaction"));

     JLabel why = new JLabel("Why?: ");

     JTextField transName = new JTextField(10);
     setLayout(new GridBagLayout());
     GridBagConstraints gc = new GridBagConstraints();
     gc.anchor = GridBagConstraints.SOUTH;
     add(transName, gc);

回答1:


This is pretty much how BorderLayout works. BorderLayout has five positions in which components can be displayed, but only one component can occupy each position. Have a look at How to Use BorderLayout for more details

Depending on what you want, you could use GridBagLayout, something like...

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;

add(namePanel, gbc);
add(actionsPanel, gbc);
add(entryPanel, gbc);

Which will layout the three components vertically within the container, but only honor there preferred heights, so the won't expand to fill the entire container, vertically

Have a look at Laying Out Components Within a Container and How to Use GridBagLayout for more details

Don't forget, you can use multiple layouts (through the use of multiple containers) to generate your desired results

You should avoid using setPreferredSize, just let the container and layout manager take care of it. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details.

You should also try using pack on JFrame to allow the window to pack itself around the contents



来源:https://stackoverflow.com/questions/31738207/trying-to-teach-myself-java-gui-swing-should-be-an-easy-fix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!