本来是这个样子的:怎么通过修改下面代码的参数,把它修改成这个样子啊!:import java.awt.*;
import javax.swing.*;public class LoginFrame extends JFrame {
/* 定义组件 */
private JLabel labelID, labelPSW;
private JTextField textID;
private JPasswordField textPSW;
private JButton btnRegister, btnGetPSW, btnLogin;
private JPanel panelLogin; LoginFrame() {
/* 初始化组件 */
labelID = new JLabel("用户名:");
labelPSW = new JLabel("密    码:");
textID = new JTextField();
textPSW = new JPasswordField();
btnRegister = new JButton("注册账号");
btnGetPSW = new JButton("取回密码");
btnLogin = new JButton("登陆");
panelLogin = new JPanel();
/* 将组件装入容器 */
GridBagLayout layout = new GridBagLayout();
panelLogin.setLayout(layout);
addComponent(layout, labelID, 0, 0, 1, 1, 10, 10, 10, 10, panelLogin);
addComponent(layout, textID, 1, 0, 1, 1, 10, 10, 10, 10, panelLogin);
addComponent(layout, btnRegister, 2, 0, 1, 1, 10, 10, 10, 10,
panelLogin);
addComponent(layout, labelPSW, 0, 1, 1, 1, 10, 10, 10, 10, panelLogin);
addComponent(layout, textPSW, 1, 1, 1, 1, 10, 10, 10, 10, panelLogin);
addComponent(layout, btnGetPSW, 2, 1, 1, 1, 10, 10, 10, 10, panelLogin);
addComponent(layout, btnLogin, 2, 2, 1, 1, 10, 10, 10, 10, panelLogin);
this.add(panelLogin);
} /* 设置布局管理器 */
public void addComponent(GridBagLayout layout, Component component, int x,
int y, int w, int h, int top, int right, int bottom, int left,
JPanel panel) {
GridBagConstraints constraints = new GridBagConstraints();
// constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = w;
constraints.gridheight = h;
constraints.insets = new Insets(top, right, bottom, left);
constraints.weightx = 100;
constraints.weighty = 100;
layout.setConstraints(component, constraints);
panel.add(component);
} /* main函数 */
public static void main(String args[]) {
LoginFrame login = new LoginFrame();
login.setTitle("登陆");
login.setBounds(352, 264, 320, 240);
login.setVisible(true);
login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
像我这样自己写的界面,可不可以同Myeclise 7.0中Swing这个插件来直接拖动面板上的组建来调试它的位置啊,然后代码里面的参数自己跟着变啊!

解决方案 »

  1.   

    只修改这里的参数就可以了,不要动我的布局管理器!
     panelLogin.setLayout(layout);
            addComponent(layout, labelID, 0, 0, 1, 1, 10, 10, 10, 10, panelLogin);
            addComponent(layout, textID, 1, 0, 1, 1, 10, 10, 10, 10, panelLogin);
            addComponent(layout, btnRegister, 2, 0, 1, 1, 10, 10, 10, 10,
                    panelLogin);
            addComponent(layout, labelPSW, 0, 1, 1, 1, 10, 10, 10, 10, panelLogin);
            addComponent(layout, textPSW, 1, 1, 1, 1, 10, 10, 10, 10, panelLogin);
            addComponent(layout, btnGetPSW, 2, 1, 1, 1, 10, 10, 10, 10, panelLogin);
            addComponent(layout, btnLogin, 2, 2, 1, 1, 10, 10, 10, 10, panelLogin);
      

  2.   

    对JTextField和JPasswordField改用其他的构造方法
    textID = new JTextField(number);
     textPSW = new JPasswordField(number);
    这里的number即使Field的长度
    就可以显示Field了
      

  3.   

    eclipse里面有插件可以直接拖拉拽的方式设计布局..不用手写这么麻烦!
      

  4.   

    我觉得使用absolute location比较好用。