JTextField width在GridBagJava中不起作用

JTextField width在GridBagJava中不起作用,java,swing,Java,Swing,我想减小用户名和密码旁边的文本字段的宽度,使它们不是100%宽度,但我不知所措。 用户名和密码TextField添加在GridBagConstraints中的以下代码中: 公共类登录页面扩展了JPanel{ 私人行动听者行动; 私人JLabel扫描JLabel; 私人JLabel登录标签; 私人JLabel密码JLabel; 私有JTextField用户名JTextField; 私有JPasswordField密码JPasswordField; 私有JButton配置JButton; 公共登录页

我想减小用户名和密码旁边的文本字段的宽度,使它们不是100%宽度,但我不知所措。
用户名和密码
TextField
添加在
GridBagConstraints
中的以下代码中:

公共类登录页面扩展了JPanel{
私人行动听者行动;
私人JLabel扫描JLabel;
私人JLabel登录标签;
私人JLabel密码JLabel;
私有JTextField用户名JTextField;
私有JPasswordField密码JPasswordField;
私有JButton配置JButton;
公共登录页(){
init();
}
私有void init(){
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JPanel content=newJPanel(newGridBagLayout());
新的空订单(20,20,20,20));
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
gbc.gridx=0;
gbc.gridy=0;
scanningJLabel=新JLabel();
loginJLabel=newjlabel();
passwordJLabel=newjlabel();
usernameJTextField=新JTextField();
passwordJPasswordField=新的JPasswordField();
configureJButton=新JButton();
尺寸标注标签设置水平对齐(SwingConstants.CENTER);
scanningJLabel.setForeground(新颜色(0,0255));
扫描jlabel.setText(“扫描”);
loginJLabel.setFont(loginJLabel.getFont().deriveFont(18.0f));
loginJLabel.setHorizontalAlignment(SwingConstants.LEFT);
setForeground(新颜色(0,0,255));
loginJLabel.setBorder(BorderFactory.createEmptyByOrder(20,20,20,20));
loginJLabel.setText(“用户名”);
passwordJLabel.setFont(passwordJLabel.getFont().deriveFont(18.0f));
passwordJLabel.setHorizontalAlignment(SwingConstants.LEFT);
设置前景(新颜色(0,0,255));
passwordJLabel.setboorder(BorderFactory.createEmptyBorder(20,20,20,20));
passwordJLabel.setText(“密码”);
usernameJTextField.setFont(loginJLabel.getFont().deriveFont(18.0f));
usernameJTextField.setHorizontalAlignment(SwingConstants.LEFT);
setForeground(新颜色(0,0255));
usernameJTextField.setToolTipText(“输入您的用户名”);
usernameJTextField.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
jTextField1_已执行的操作(e);
}
});
passwordJPasswordField.setFont(passwordJLabel.getFont().deriveFont(18.0f));
passwordJPasswordField.setHorizontalAlignment(SwingConstants.LEFT);
设置前景(新颜色(0,0255));
passwordJPasswordField.setToolTipText(“输入密码”);
passwordJPasswordField.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
jPasswordField1_执行的操作(e);
}
});
setBounds(70,30,150,20);
passwordJPasswordField.setBounds(70,65,150,20);
setPreferredSize(新维度(140,40));
setBackground(新颜色(204204204));
setForeground(新颜色(0,0255));
配置jbutton.setFont(loginJLabel.getFont().deriveFont(16.0f));
configureJButton.setText(“Configure”);
configureJButton.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
jButton1_执行的操作(e);
}
});
添加(登录标签,gbc);
gbc.gridy++;
添加(密码jlabel,gbc);
gbc.gridx++;
gbc.gridy=0;
gbc.gridwidth=2;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;
添加(usernameJTextField,gbc);
gbc.gridy++;
添加(密码jpasswordfield,gbc);
gbc.gridx=1;
gbc.gridy++;
gbc.gridwidth=1;
gbc.weightx=0;
gbc.fill=GridBagConstraints.NONE;
添加(配置jbutton,gbc);
}
受保护的无效jButton1_actionPerformed(ActionEvent e){
字符串用户名=新字符串(usernameJTextField.getText());
字符串密码=新字符串(passwordJPasswordField.getText());
if(username.isEmpty()){
showMessageDialog(null,“用户名不应为空”,“信息框:”+“警告”,JOptionPane.INFORMATION\u消息);
回来
}
if(password.isEmpty()){
JOptionPane.showMessageDialog(null,“密码不应为空”,“信息框:”+“警告”,JOptionPane.INFORMATION\u消息);
回来
}
if(username.equals(“admin”)和password.equals(“admin”)){
JPanel parent=(JPanel)getParent();
CardLayout=(CardLayout)parent.getLayout();
cardLayout.next(父级);
}否则{
System.out.println(“输入有效的用户名和密码”);
JOptionPane.showMessageDialog(null,“用户名和密码不正确,请重试”,“信息框:“+”警告”,JOptionPane.INFORMATION\u消息);
}
}
受保护的无效jTextField1\u已执行的操作(操作事件e){
System.out.println(“给定用户名”);
}
受保护的无效jPasswordField1\u actionPerformed(ActionEvent e){
System.out.println(“给定密码”);
}
}
但它填充了整个屏幕宽度,如下所示:

此屏幕的主页如下所示

公共类主页{
私有静态最终字符串CARD\u LOGIN=“CARD LOGIN”;
私人车站
JTextField textField = new JTextField("",20);
textField.setBounds(int x, int y, int width,int height);
public class MainFrame extends JFrame {

    public MainFrame() throws HeadlessException {
        super("Test Frame");
        createGUI();
    }

    private void createGUI() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new GridBagLayout());

        JLabel userNameLabel = new JLabel("User Name");
        JLabel passwordLabel = new JLabel("Password");

        JTextField userNameTextField = new JTextField(20);
        JPasswordField passwordField = new JPasswordField(20);

        JButton configureButton = new JButton("Configure");

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5, 5, 5, 5);
        c.gridx = 0;
        c.gridy = 0;

        c.weightx = 0.0;
        add(userNameLabel, c);

        c.gridy++;
        add(passwordLabel, c);

        c.weightx = 1.0;
        c.gridx++;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LINE_START;
        add(userNameTextField, c);

        c.gridy++;
        add(passwordField, c);

        c.gridy++;
        add(configureButton, c);

        pack();
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
    }
}