问题
Here's my code
public class HomeTopPanel extends JPanel {
//BUTTONS
private final JButton myAccountButton = new JButton("My Account");
private final JButton updatePhoto = new JButton("Update Photo");
//PANELS
private final JPanel rightPanel_1 = new JPanel(new GridBagLayout());
private final JPanel rightPanel_2 = new JPanel(new GridBagLayout());
private final JPanel logHistoryPanel = new JPanel(new GridBagLayout());
//BORDERS
private final Border homeTopPanel_LineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
private final Border rightPanel1_LineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
private final Border rightPanel2_LineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
private final TitledBorder logHistoryPanel_TitledBorder = BorderFactory.createTitledBorder("Log History");
//LABELS
private final JLabel imageContainer = new JLabel("User Image");
//CONSTRAINTS
GridBagConstraints gbc = new GridBagConstraints();
//CONSTRUCTOR
public HomeTopPanel(){
//METHOD CALLS
this.setLayout(new GridBagLayout()); //setting of layout should ALWAYS come first before adding constraints and components
constructMyAccountButton();
constructRightPanel_1();
constructRightPanel_2();
constructLeftPanelComponents();
setRightPanelBorders();
}
public final void constructMyAccountButton(){
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.insets = new Insets(5,5,5,5);
gbc.gridx = 0; gbc.gridy = 0;
this.add(myAccountButton,gbc);
}
public final void constructRightPanel_1(){
rightPanel_1.setPreferredSize(new Dimension(1000, 550));
gbc.gridx = 1; gbc.gridy = 0;
this.add(rightPanel_1,gbc);
}
public final void constructRightPanel_2(){
rightPanel_2.setPreferredSize(new Dimension(800, 300));
gbc.gridheight = 3;
rightPanel_1.add(rightPanel_2,gbc);
}
public final void constructLeftPanelComponents(){
gbc.gridx = 0; gbc.gridy = 0;
gbc.ipadx = 0;
gbc.anchor = GridBagConstraints.PAGE_START;
rightPanel_1.add(imageContainer,gbc);
gbc.gridx = 0; gbc.gridy = 1;
gbc.anchor = GridBagConstraints.CENTER;
rightPanel_1.add(updatePhoto,gbc);
gbc.gridx = 0; gbc.gridy = 2;
gbc.anchor = GridBagConstraints.PAGE_END;
logHistoryPanel.setPreferredSize(new Dimension(110, 100));
rightPanel_1.add(logHistoryPanel,gbc);
}
private void setRightPanelBorders(){
rightPanel_1.setBorder(rightPanel1_LineBorder);
rightPanel_2.setBorder(rightPanel2_LineBorder);
logHistoryPanel.setBorder(logHistoryPanel_TitledBorder);
this.setBorder(homeTopPanel_LineBorder);
}
}
Here's what I get:
I have 2 questions:
- How can I push all these 4 objects to top?
- Why is the "User Image" JLabel taking too much space or rather why is it's cell height too tall?
I want to accomplish this:
then push it to top. I tried using insets and weight but still can't get the result I desire.
I'd appreciate any help. At this point, I don't know if it's a problem with ipadx/ipady or weightx / weighty.
回答1:
- How can I push all these 4 objects to top?
You need to assign weights to the components: gbc.weightx
and gbc.weighty
.
- Why is the "User Image" JLabel taking too much space or rather why is it's cell height too tall?
You forgot to reset gbc.gridheight = 1
after adding rightPanel_2
. Also, you set the gbc.anchor
value to CENTER
and PAGE_END
which causes your components to not stack as you want.
I commented out in your code things which should be removed and added the code I explained above that fixes the layout:
public class HomeTopPanel extends JPanel {
// BUTTONS
private final JButton myAccountButton = new JButton("My Account");
private final JButton updatePhoto = new JButton("Update Photo");
// PANELS
private final JPanel rightPanel_1 = new JPanel(new GridBagLayout()) {
public Dimension getPreferredSize() {
return new Dimension(1000, 550);
};
};
private final JPanel rightPanel_2 = new JPanel(new GridBagLayout()) {
public Dimension getPreferredSize() {
return new Dimension(800, 300);
};
};
private final JPanel logHistoryPanel = new JPanel(new GridBagLayout());
// BORDERS
private final Border homeTopPanel_LineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
private final Border rightPanel1_LineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
private final Border rightPanel2_LineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
private final TitledBorder logHistoryPanel_TitledBorder = BorderFactory.createTitledBorder("Log History");
// LABELS
private final JLabel imageContainer = new JLabel("User Image");
// CONSTRAINTS
GridBagConstraints gbc = new GridBagConstraints();
// CONSTRUCTOR
public HomeTopPanel() {
// METHOD CALLS
this.setLayout(new GridBagLayout()); // setting of layout should ALWAYS come first before adding constraints and components
constructMyAccountButton();
constructRightPanel_1();
constructRightPanel_2();
constructLeftPanelComponents();
setRightPanelBorders();
}
public final void constructMyAccountButton() {
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(myAccountButton, gbc);
}
public final void constructRightPanel_1() {
rightPanel_1.setBackground(Color.RED);
// rightPanel_1.setPreferredSize(new Dimension(1000, 550));
gbc.fill = GridBagConstraints.BOTH; // Optional: used for the 2 right panels
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 1;
gbc.gridy = 0;
this.add(rightPanel_1, gbc);
}
public final void constructRightPanel_2() {
rightPanel_2.setBackground(Color.BLUE);
// rightPanel_2.setPreferredSize(new Dimension(800, 300));
gbc.gridheight = 3;
rightPanel_1.add(rightPanel_2, gbc);
}
public final void constructLeftPanelComponents() {
gbc.fill = GridBagConstraints.NONE; // Remove if you didn't use it above
gbc.weightx = 0;
gbc.weighty = 0;
gbc.gridheight = 1;
gbc.gridx = 0;
gbc.gridy = 0;
// gbc.ipadx = 0;
// gbc.anchor = GridBagConstraints.PAGE_START;
rightPanel_1.add(imageContainer, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
// gbc.anchor = GridBagConstraints.CENTER;
rightPanel_1.add(updatePhoto, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
// gbc.anchor = GridBagConstraints.PAGE_END;
logHistoryPanel.setPreferredSize(new Dimension(110, 100));
rightPanel_1.add(logHistoryPanel, gbc);
}
private void setRightPanelBorders() {
rightPanel_1.setBorder(rightPanel1_LineBorder);
rightPanel_2.setBorder(rightPanel2_LineBorder);
logHistoryPanel.setBorder(logHistoryPanel_TitledBorder);
this.setBorder(homeTopPanel_LineBorder);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new HomeTopPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
(Edited) Notes on your usage of GridBagLayout
:
@Override getPreferredSize()
instead of callingsetPreferredSize(...)
. See here and here.I would recommend using a new
GridBagConstraints
for each new container (if not each new component), otherwise you are risking some nasty bugs like the ones you have. The tutorial also mentions:As you might have guessed from the above example, it is possible to reuse the same
GridBagConstraints
instance for multiple components, even if the components have different constraints. However, it is recommended that you do not reuseGridBagConstraints
, as this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each new instance.When you set a
GridBagConstraints
property, it remains until changed. Setting thegridheight
to 1 at some point will affect alladd
invocations after setting it, so there is no need to set it again to the same value for each invocation. This is also true forgridx
which you set to0
each time - once is actually enough. The reason I left all of the redundant calls is that it is sometimes easier to see the exact location before adding the component instead of searching the code for the last time the property was set.Coloring components during production phase helps to see how the GUI really looks.
Edit: notes on your general code
Start the GUI on the EDT (see Concurrency in Swing):
public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame(); frame.add(new HomeTopPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }); }
According to Java naming conventions, variables names should not use underscores (
_
) unless they are constants. SorightPanel_1
should berightPanel1
.You are creating 3 times the same border,
BorderFactory.createLineBorder(Color.BLACK, 1);
and you are also keeping them as fields when local variables will do. You can even just create the border inside thesetBorder(...)
call.I think that instead of setting all the borders in one place, you can set each component's border where you configure it. In your code you divided your methods to match components, so it makes sense to fully configure the component in that method.
Splitting a long method into smaller
final
methods is an excellent way to deal with readability. I wish I saw it more rather than 100 lines of components initializations. However, as in your case, if you have a shared object (likeGridBagConstraints
) then you have to be careful how it changes between the methods.
回答2:
Try these principles:
public class GridBagLayoutTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
test();
}
});
}
private static void test() {
JFrame frame = new JFrame("GridBagLayoutTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel userImageLabel = new JLabel("User Image");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0;
gbc.weighty = 0;
gbc.insets.bottom = 80;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.PAGE_START;
contentPane.add(userImageLabel, gbc);
JButton updatePhotoButton = new JButton("Update Photo");
gbc.gridy++;
gbc.insets.bottom = 5;
gbc.fill = GridBagConstraints.HORIZONTAL;
contentPane.add(updatePhotoButton, gbc);
JPanel logHistoryPanel = new JPanel();
logHistoryPanel.setBorder(BorderFactory.createTitledBorder("Log History"));
logHistoryPanel.setPreferredSize(new Dimension(110, 100));
gbc.gridy++;
gbc.weighty = 0.001;
gbc.insets.bottom = 0;
contentPane.add(logHistoryPanel, gbc);
gbc.gridy++;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
contentPane.add(Box.createVerticalGlue(), gbc);
JPanel rightPanel = new JPanel();
rightPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridheight = 4;
gbc.weightx = 1;
gbc.insets.left = 5;
gbc.anchor = GridBagConstraints.CENTER;
contentPane.add(rightPanel, gbc);
frame.setContentPane(contentPane);
frame.setSize(new Dimension(500, 500));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
UPD:
why it is advisable to include SwingUtilities.invokeLater(new Runnable(). Also, I noticed you put all the initialization within one method. Is this something I should be practicing instead of calling my methods within the constructor like what I did in my post?
- Everything you do with graphics and UI should be perfomed in AWT thread.
SwingUtilities.invokeLater
will do it for sure. - Components' initialization is rather about style than rules. But, as @user1803551 said, "I would recommend using a new GridBagConstraints for each new container (if not each new component), otherwise you are risking some nasty bugs like the ones you have."
来源:https://stackoverflow.com/questions/34770139/gridbag-layout-how-to-push-components-north