两个JTextEdit上的GroupLayout.linkSize()使它们的大小为零

时间:2012-06-03 20:18:12

标签: java swing jtextfield grouplayout

我正在编写一个Java程序(下面的最小可编译示例),并且我试图通过使一些元素排成一行来完善用户界面。如果你运行它,你可以看到d_ipd_pword看起来好多了,如果我能让它们大小相同的话。而且我希望它们在调整窗口大小时动态保持相同的大小。

GroupLayout有一个linkSize()方法,可让您链接不相关对象的大小。这适用于按钮和标签。但是使用JTextField,这是一场灾难。即使我将其中一个的最小尺寸设置为大尺寸,它仍然坚持将尺寸设置为零。

我还尝试过告诉GroupLayout水平分钟和首选大小为GroupLayout.DEFAULT_SIZEGroupLayout.PREFERRED_SIZE无效的内容。谁能告诉我是否有任何方法可以强迫JTextField与linkSize一起正常工作?

请注意,要打开linkSize,有一行标有注释,您需要取消注释。当你这样做时,JTextField宽度搞砸了。另外,尝试将d_username添加到链接对象列表中。现在,即使你可以说,即使是'紧张',要将用户名/密码行水平放置在窗口的宽度上,字段也会变为零,并且不会调整大小。

谢谢!

package textfield;

import java.awt.Container;
import java.awt.Dimension;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class TextField extends JFrame {
    JTextField d_mount, d_ip, d_share, d_username, d_pword;
    JRadioButton nfs, smb;
    JButton save, revert;

    TextField() {
        Container pane = this.getContentPane();

        JLabel lmount = new JLabel("Mount Point:");
        JLabel ltype = new JLabel("Share Type:");
        JLabel lip = new JLabel("IP Address:");
        JLabel lshare = new JLabel("Remote Share:");
        JLabel lusername = new JLabel("Username:");
        JLabel lpassword = new JLabel("Password:");

        d_mount = new JTextField();
        nfs = new JRadioButton("NFS");
        smb = new JRadioButton("SMB");
        ButtonGroup group = new ButtonGroup();
        group.add(nfs);
        group.add(smb);
        d_ip = new JTextField();
        d_share = new JTextField();
        d_username = new JTextField();
        d_pword = new JTextField();

        d_ip.setMinimumSize(new Dimension(200, 0));

        save = new JButton("Save");
        revert = new JButton("Revert");

        GroupLayout layout = new GroupLayout(pane);
        pane.setLayout(layout);

        layout.setVerticalGroup(
            layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(lmount)
                    .addComponent(d_mount)
                    )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(ltype)
                    .addComponent(nfs)
                    .addComponent(smb)
                //    )
                //.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(lip)
                    .addComponent(d_ip)
                    )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(lshare)
                    .addComponent(d_share)
                    )
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE, true)
                    .addComponent(lusername)
                    .addComponent(d_username)
                    .addComponent(lpassword)
                    .addComponent(d_pword)
                )
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(revert)
                    .addComponent(save)
                )
            );
        layout.setHorizontalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(lmount)
                        .addComponent(ltype)
                        .addComponent(lshare)
                        .addComponent(lusername)
                        )
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(d_mount)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(nfs)
                            .addComponent(smb)
                            .addGap(20, 20, Integer.MAX_VALUE)
                            .addComponent(lip)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(d_ip, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            )
                        .addComponent(d_share)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(d_username, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(lpassword)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(d_pword, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            )
                        )
                    )
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Integer.MAX_VALUE)
                    .addComponent(revert)
                    .addComponent(save)
                    )
            );


        // ****** ADD THIS LINE BACK IN ******
        //layout.linkSize(SwingConstants.HORIZONTAL, d_pword, d_ip);

        pack();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        try {         
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     
        }      catch (Exception e) {        
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TextField().setVisible(true);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:3)

立即解决方案是建议列中的大小。即

d_pword = new JTextField(15);

注意:

  • 我不同意当这两个字段对齐时,GUI看起来最好。它们之间没有逻辑联系,因此链接大小似乎无关紧要。误导。
  • d_pword字段应为JPasswordField

答案 1 :(得分:0)