问题
i am learning GridBagLayout
and did very simple layout that is attached,There are two very small confusions
1- Size of Buttons
i have used New.setPreferredSize(new Dimension(70,23));
is it standard way to make all the buttons same size
2-Placing of components does not look good
now see there is much more padding on all sides of components
, so how to put it in right way from top left corner
, should i decrease size of JFrame?
or use frame.pack();
both work but dont know what is standard practice (i have tried pagestart
etc) ,
this is how my code looks
frame= new JFrame("Hello ");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setLocation(0, 0);
p1= new JPanel (new GridBagLayout());
gbc = new GridBagConstraints();
lblname= new JLabel("Name");
gbc.gridx=0;
gbc.gridy=0;
p1.add(lblname, gbc);
textname = new JTextField(11);
gbc.gridx=1;
gbc.gridy=0 ;
p1.add(textname, gbc);
New= new JButton("New");
gbc.gridx=2;
gbc.gridy=0;
New.setPreferredSize(new Dimension(70,23));
p1.add(New, gbc);
lblEmail = new JLabel("Email ");
gbc.gridx=0;
gbc.gridy=1;
p1.add(lblEmail , gbc);
TextEmail = new JTextField(11);
gbc.gridx=1;
gbc.gridy=1;
p1.add(TextEmail,gbc);
Edit = new JButton("Edit") ;
gbc.gridx= 2 ;
gbc.gridy=1;
Edit.setPreferredSize(new Dimension(70,23));
p1.add(Edit , gbc);
lblgender= new JLabel("Gender");
gbc.gridx=0;
gbc.gridy=2;
p1.add(lblgender, gbc);
TextGender= new JTextField(11);
gbc.gridx=1;
gbc.gridy=2;
p1.add(TextGender, gbc);
Gender= new JButton("Gender");
gbc.gridx=2;
gbc.gridy=2;
Gender.setPreferredSize(new Dimension(70,23));
p1.add(Gender, gbc);
pre= new JButton("<<");
gbc.gridx=0;
gbc.gridy=3;
p1.add(pre, gbc);
count = new JTextField(5);
gbc.gridx=1;
gbc.gridy=3;
p1.add(count, gbc);
next= new JButton(">>");
gbc.gridx=2;
gbc.gridy=3;
next.setPreferredSize(new Dimension(70,23));
p1.add(next, gbc);
p1.setVisible(true);
frame.add(p1);
frame.setVisible(true);
回答1:
GridBagLayout
will drive you nuts, it's also one of the most flexible layout managers available in the JDK.
Don't be afraid to use compound layouts. In the example below, I've moved the navigation controls to there own panel, which makes it much easier to define complex layouts (you can also mix layout managers this way)
GridBagConstraints#fill
allows you to determine how components may be filled within there cell. You have GridBagConstraints.HORIZONTAL
, GridBagConstraints.VERTICAL
and GridBagConstraints.BOTH
... I think there meaning is self explanatory.
In the example below, I've used GridBagConstraints.HORIZONTAL
to allow the buttons to fill all the available space within their cell/column
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class TestLayout20 {
public static void main(String[] args) {
new TestLayout20();
}
public TestLayout20() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
JLabel lblname = new JLabel("Name");
gbc.gridx = 0;
gbc.gridy = 0;
add(lblname, gbc);
JTextField textname = new JTextField(11);
gbc.gridx = 1;
gbc.gridy = 0;
add(textname, gbc);
JLabel lblEmail = new JLabel("Email ");
gbc.gridx = 0;
gbc.gridy = 1;
add(lblEmail, gbc);
JTextField TextEmail = new JTextField(11);
gbc.gridx = 1;
gbc.gridy = 1;
add(TextEmail, gbc);
JLabel lblgender = new JLabel("Gender");
gbc.gridx = 0;
gbc.gridy = 2;
add(lblgender, gbc);
JTextField TextGender = new JTextField(11);
gbc.gridx = 1;
gbc.gridy = 2;
add(TextGender, gbc);
JButton New = new JButton("New");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 12, 0, 0);
gbc.gridx = 2;
gbc.gridy = 0;
add(New, gbc);
JButton edit = new JButton("Edit");
gbc.gridx = 2;
gbc.gridy = 1;
add(edit, gbc);
JButton Gender = new JButton("Gender");
gbc.gridx = 2;
gbc.gridy = 2;
add(Gender, gbc);
JPanel pnlNav = new JPanel(new GridBagLayout());
gbc.insets = new Insets(12, 0, 0, 0);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(pnlNav, gbc);
JTextField count = new JTextField(5);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
pnlNav.add(count, gbc);
JButton pre = new JButton("<<");
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
gbc.gridx = 0;
gbc.gridy = 0;
pnlNav.add(pre, gbc);
JButton next = new JButton(">>");
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 2;
gbc.gridy = 0;
pnlNav.add(next, gbc);
}
}
}
I highly recommend that you have a read through How to use GridBagLayout for a better explanation :P
来源:https://stackoverflow.com/questions/15320180/jframe-components-size-and-location