簡體   English   中英

當我嘗試調整JFrame的大小時,JTextField的大小沒有得到調整

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

我正在使用下面的代碼來設計我的JFrame。 但是如果我嘗試改變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();
    }

}

您應該明確了解有關GridBagLayout如何工作的更多信息。
為了解決您的問題,我想向您介紹weight變量。 它們是GridBagConstraints一部分,並且由於您聲明了自己的實例,因此將它們初始化為值0
因此,您需要設置gc.fill = GridBagConstraints.HORIZONTAL; 鼓勵組件使用可用的水平空間。
布局通過使用weight值來確定每個組件相對於preferredSize的可用空間,從而實現此目的。
因此,如果您希望JTextField獲得所有可用空間,請聲明gc.weightx = 1.0f; 表示在添加之前水平方向上100%的可用空間。 並在添加下一個元素之前將其設置回0.0f

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM