Java BoxLayout使用中的问题 5
想使用BoxLayout,但是出现故障,故障如下,麻请达人指点,谢谢!Exceptioninthread"main"java.awt.AWTError:BoxLayout...
想使用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不行?? 展开
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不行?? 展开
3个回答
展开全部
我稍微改了一下 用的不是BoxLayout方法,换了一个你看看运行下
import javax.swing.*;
import java.awt.*;
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(15);
jPwd = new JPasswordField(15);
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 FlowLayout());
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();
}
}
如果你飞要用Box的话 就得先获得这个容器组件
this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
import javax.swing.*;
import java.awt.*;
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(15);
jPwd = new JPasswordField(15);
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 FlowLayout());
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();
}
}
如果你飞要用Box的话 就得先获得这个容器组件
this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
展开全部
你用的是Box 不是BoxLayout,它们两个是不相同的,Box是相当于一个容器的。你可以再细找下它们的区别。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将(((((this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.add(title);
this.add(userName);
this.add(password);
this.add(submitButton )))))))
替换为
(((((((Container container=this.getContentPane();
this.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));
container.add(title);
container.add(userName);
container.add(password);
container.add(submitButton);
title.setVisible(true);
userName.setVisible(true);
password.setVisible(true);
submitButton.setVisible(true);))))))))))
this.add(title);
this.add(userName);
this.add(password);
this.add(submitButton )))))))
替换为
(((((((Container container=this.getContentPane();
this.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS));
container.add(title);
container.add(userName);
container.add(password);
container.add(submitButton);
title.setVisible(true);
userName.setVisible(true);
password.setVisible(true);
submitButton.setVisible(true);))))))))))
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询