❶ java BoxLayout使用中的問題
我稍微改了一下 用的不是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));