想使用BoxLayout,但是出现故障,故障如下,麻请达人指点,谢谢!Exceptioninthread"main"java.awt.AWTError:BoxLayoutcan'tbeshared-----------------------------------------------...
想使用BoxLayout,但是出现故障,故障如下,麻请达人指点,谢谢!
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
-----------------------------------------------------
import javax.swing.*;
/**
*
* @author Administrator
*/
public class Login extends JFrame {
private JLabel jLabel1, jLabel2, jLabel3;
private JButton jConnect, jCancel;
private JTextField jUID;
private JPasswordField jPwd;
Login() {
super("登录界面");
jLabel1 = new JLabel("家庭收入管理系统");
jLabel2 = new JLabel("用户名");
jLabel3 = new JLabel("密码");
jConnect = new JButton("连接");
jCancel = new JButton("取消");
jUID = new JTextField("");
jPwd = new JPasswordField("");
Box title = Box.createHorizontalBox();
Box userName = Box.createHorizontalBox();
Box password = Box.createHorizontalBox();
Box submitButton = Box.createHorizontalBox();
title.add(jLabel1);
userName.add(jLabel2);
userName.add(jUID);
password.add(jLabel3);
password.add(jPwd);
submitButton.add(jConnect);
submitButton.add(jCancel);
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.add(title);
this.add(userName);
this.add(password);
this.add(submitButton);
title.setVisible(true);
userName.setVisible(true);
password.setVisible(true);
submitButton.setVisible(true);
this.setLocation(50, 50);
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO code application logic here
new Login();
}
}
吴琼的答案是使用FlowLayout,是可以运行的。
但我觉得比较困惑的是为什么BoxLayout不行??
展开