问题
I am a beginner to Java. I would like some help in Swings to position my components.
I am not able to decide as which layout manager should I use to position my components in the following order
+-----------------------------------+
| |
| Username Text Field |
| Password Password Field |
| |
| Submit button |
| |
+-----------------------------------+
The following is my code
package ssst;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class Test implements ActionListener{
JButton submit;
JFrame j;
JFrame jf;
public Test()
{
j = new JFrame("PLAIN");
j.setBounds(500,150,300,400);
JPanel panel = new JPanel();
j.add(panel);
GridBagLayout gb = new GridBagLayout();
panel.setLayout(gb);
GridBagConstraints c = new GridBagConstraints();
JLabel label = new JLabel("User Name");
c.gridx=0;
c.gridy=0;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.WEST;
c.ipadx=5;
c.ipady=5;
c.insets= new Insets(7,7,7,7);
panel.add(label,c);
JTextField username = new JTextField(10);
c.gridx=1;
c.gridy=0;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.WEST;
c.ipadx=5;
c.insets= new Insets(7,7,7,7);
panel.add(username,c);
JLabel password= new JLabel("Password");
c.gridx=0;
c.gridy=1;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.WEST;
c.ipadx=5;
c.insets= new Insets(7,7,7,7);
panel.add(password,c);
JPasswordField pass = new JPasswordField(10);
c.gridx=1;
c.gridy=1;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.WEST;
c.insets= new Insets(7,7,7,7);
panel.add(pass,c);
submit = new JButton("Submit");
c.gridx=1;
c.gridy=6;
c.fill=GridBagConstraints.HORIZONTAL;
c.anchor=GridBagConstraints.WEST;
c.insets= new Insets(7,7,7,7);
panel.add(submit,c);
submit.addActionListener(this);
j.setVisible(true);
j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
j.setVisible(false);
jf = new JFrame("NEw Window");
jf.setVisible(true);
jf.setBounds(500,150,300,400);
JPanel panel2 = new JPanel();
panel2.setLayout(null);
jf.add(panel2);
JButton logout = new JButton("LOGOUT");
logout.setBounds(100, 30, 400, 30);
panel2.add(logout);
logout.addActionListener(new Test2());
jf.setDefaultCloseOperation(j.EXIT_ON_CLOSE);
}
class Test2 implements ActionListener{
public void actionPerformed(ActionEvent e) {
jf.dispose();
j.setVisible(true);
}
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Test();
}
}
);
}
}
回答1:
You could use a GridBagLayout
and a JOptionPane
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LoginPane extends JPanel {
private JTextField userName;
private JPasswordField password;
public LoginPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(4, 4, 4, 4);
add(new JLabel("Username:"), gbc);
gbc.gridy++;
add(new JLabel("Password:"), gbc);
userName = new JTextField(10);
password = new JPasswordField(10);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(userName, gbc);
gbc.gridy++;
add(password, gbc);
}
public String getUsername() {
return userName.getText();
}
public char[] getPassword() {
return password.getPassword();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
LoginPane loginPane = new LoginPane();
int option = JOptionPane.showOptionDialog(
null,
loginPane,
"Login",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
new Object[]{"Submit"},
"Submit");
if (option == 0) {
System.out.println("Happy");
}
}
});
}
}
You could possibly use GridLayout
with this concept as well.
Take a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html for more ideas and links to other layout managers
回答2:
go for GridBagLayout, logic of layout is simple. It works on X and Y co-ordinates. here
Also you should go through other layout as well, it will help you decide in future designs.
回答3:
Better to use GridBagLayout, in case if u add new components, Layout manager as to take care to fit the components with in the screen if u (maximized/Restore Down).
回答4:
It is subjective to say what layout manager is appropriate for a
design. This design is a no-brainer for the MigLayout
manager.
For a comparison, I provide a solution with a MigLayout
.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutLoginEx extends JFrame {
public MigLayoutLoginEx() {
initUI();
setTitle("Log in");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
setLayout(new MigLayout("ins 15, wrap 2", "[][grow]"));
JLabel lbl1 = new JLabel("User name:");
JTextField field1 = new JTextField(10);
JLabel lbl2 = new JLabel("Password:");
JPasswordField field2 = new JPasswordField(10);
JButton btn = new JButton("Submit");
add(lbl1);
add(field1, "growx");
add(lbl2);
add(field2, "growx");
add(btn, "span 2, center, gaptop 20");
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutLoginEx ex = new MigLayoutLoginEx();
ex.setVisible(true);
}
});
}
}
The layout is achieved with six lines of layout code.
来源:https://stackoverflow.com/questions/24320436/which-layout-manager-would-be-appropriate-to-my-design