简体   繁体   English

当我尝试调整JFrame的大小时,JTextField的大小没有得到调整

[英]JTextField is not getting resized when I try to resize the JFrame

I am using the below code to design my JFrame. 我正在使用下面的代码来设计我的JFrame。 But the JTextfield is not getting resized if I try to change the JFrame size.I have used Border Layout and aligned it to center as per some suggestion for layout to handle the resizing. 但是如果我尝试改变JFrame的大小,JTextfield的大小不会改变。我已经使用Border Layout并按照一些布局建议将其对齐到居中以处理调整大小。

public class Parser extends JFrame{

    static JFrame frame; 
    static JLabel lblFile;
    static JPanel pHdr;
    static JPanel pBody;
    static JPanel pFtr;
    static JPanel pMainPanel;
    static JPanel pOuterMainPanel;
    static JLabel lblImage;
    public Parser()
    {       
        String startUpPath = System.getProperty("user.dir");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame = new JFrame("Log Parser");
        frame.setPreferredSize(new Dimension(700,500));
        frame.setLocation((int)screenSize.getWidth()/3,(int)screenSize.getHeight()/4);
        frame.setLayout(new BorderLayout());

        //initialize the JPanels and add to frames
        pOuterMainPanel = new JPanel(new BorderLayout());

        pHdr  = new JPanel(new BorderLayout(700,100));
        pHdr.setPreferredSize(new Dimension(700,100));      
        pHdr.setOpaque(true);
        pHdr.setBackground(Color.WHITE);
        pOuterMainPanel.add(pHdr,BorderLayout.NORTH);

        pMainPanel = new JPanel(new BorderLayout(700,400));
        pBody = new JPanel(new GridBagLayout());
        pMainPanel.add(pBody,BorderLayout.CENTER);
        pBody.setPreferredSize(new Dimension(700,400));
        pBody.setOpaque(true);
        pBody.setBackground(Color.WHITE);
        pOuterMainPanel.add(pMainPanel,BorderLayout.CENTER);        

        pFtr  = new JPanel(new BorderLayout(700,50));
        pFtr.setOpaque(true);
        pFtr.setBackground(Color.WHITE);        
        pFtr.setPreferredSize(new Dimension(700,50));
        pOuterMainPanel.add(pFtr,BorderLayout.SOUTH);

        //Customize Hdr Panel
        lblImage = new JLabel();
        ImageIcon imgicon = new ImageIcon(startUpPath + "\\Resources\\IM.png");
        lblImage.setIcon(imgicon);
        pHdr.add(lblImage,BorderLayout.CENTER);

        //Customize Body Panel
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx=0;
        gc.gridy=0;
        gc.anchor = GridBagConstraints.CENTER;

        JLabel lblPath = new JLabel("Path         ");
        lblPath.setForeground(Color.darkGray);
        lblPath.setFont(new Font("Times New Roman", Font.BOLD, 15));
        pBody.add(lblPath,gc);
        gc.gridx++;


        final JTextField txtFilePath = new JTextField(30);
        txtFilePath.setPreferredSize(new Dimension(30,30));
        pBody.add(txtFilePath,gc);      
        gc.gridx++;

        JLabel lblsp1 = new JLabel("      ");
        pBody.add(lblsp1,gc);
        gc.gridx++;

        final JButton btnBrowse = new JButton("Browse");
        btnBrowse.setBackground(new Color(224,224,224));
        btnBrowse.setPreferredSize(new Dimension(80,30));
        pBody.add(btnBrowse,gc);
        gc.gridx++; 


        //Finalize the Frame        
        frame.add(pOuterMainPanel,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);


        btnBrowse.addMouseListener(new MouseAdapter()
        {
            public void mouseEntered(MouseEvent evt)
            {
                btnBrowse.setBackground(new Color(30,91,195));
                btnBrowse.setForeground(Color.white);
            }
            public void mouseExited(MouseEvent evt)
            {
                btnBrowse.setBackground(new Color(224,224,224));
                btnBrowse.setForeground(Color.black);
            }

        });

        FocusListener fl = new FocusListener() {

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
                txtFilePath.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
            }

            @Override
            public void focusGained(FocusEvent e) {
                // TODO Auto-generated method stub
                txtFilePath.setBorder(BorderFactory.createLineBorder(new Color(34,5,134),2));
            }
        };

        txtFilePath.addFocusListener(fl);

    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            new Parser();
    }

}

You should definitly learn more about how GridBagLayout works. 您应该明确了解有关GridBagLayout如何工作的更多信息。
For solving your problem I want to introduce you to the weight variables. 为了解决您的问题,我想向您介绍weight变量。 They are part of the GridBagConstraints and since you declared your own instance of this they are initialized with value 0 . 它们是GridBagConstraints一部分,并且由于您声明了自己的实例,因此将它们初始化为值0
So you need to set gc.fill = GridBagConstraints.HORIZONTAL; 因此,您需要设置gc.fill = GridBagConstraints.HORIZONTAL; to encourage the components to use the available horizontal space. 鼓励组件使用可用的水平空间。
The Layout does this by using the weight values to determine how much of the free space will be 'given' to each component regarding their preferredSize . 布局通过使用weight值来确定每个组件相对于preferredSize的可用空间,从而实现此目的。
So if you want your JTextField to get all the free space declare gc.weightx = 1.0f; 因此,如果您希望JTextField获得所有可用空间,请声明gc.weightx = 1.0f; means 100% of free Space in horizontal direction before adding it. 表示在添加之前水平方向上100%的可用空间。 And set it back to 0.0f before adding the next element. 并在添加下一个元素之前将其设置回0.0f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM