I want to make the password label and text field below the ID..but i`m kind of new to GriBagLayout.
I hope you can help me.
Here is my code:
class LoginPanel extends JPanel {//login components
private JButton exitbtn = new JButton("Exit");
private JLabel idLabel = new JLabel("Staff ID : ");
private JTextField idJtf = new JTextField(10);
private JLabel pwLabel = new JLabel("Password : ");
private JPasswordField pwJtf = new JPasswordField(10);
LoginPanel() {
setOpaque(false);
setLayout(new GridBagLayout());
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
//add(new JLabel("Staff ID: ")); add(new JTextField(10));
//add(new JLabel("Password: ")); add(new JPasswordField(10));
add(idLabel);
add(idJtf);
add(pwLabel);
add(pwJtf);
//add(exitbtn);
}
}
就像阅读本教程一样简单:GridBagLayout
If you are familiar with creating HTML pages using tables, GridBagLayout should be easy for you
Placing controls one below the other is like placing them to a table with single column. It means that you need to set 0 as a column number and [0..3] as row numbers:
GridBagConstraints c = new GridBagConstraints();
int rowNum = 0
c.gridx = 0;
c.gridy = rowNum;
add(idLabel, c);
c.gridy++;
add(idJtf, c);
c.gridy++;
add(pwLabel, c);
c.gridy++;
add(pwJtf, c);
However for such simple layout you can use another layout managers
Do yourself a favor and stop using GridBagLayout. GridBagLayout is a layoutmanager from a previous era. I'll realize it is in all the "official" Oracle Java docs but most people avoid it now a days (and for good reasons). A good alternative for complex layouts GridBag is meant to tackle is Jgoodies FormLayout ( http://www.jgoodies.com/freeware/libraries/forms/ ). I'll guarantee you that you will maintain your sanity and your life's quality will improve measurably :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.