上传照片时,为什么我的GUI布局会更改?

时间:2019-01-23 15:35:26

标签: java image swing layout-manager gridbaglayout

我的程序用户界面当前使用网格袋布局,我希望它的尺寸​​固定,但是当我将图片上传到标签时,整个界面的尺寸都会改变。

下面是我的布局管理器的代码

public SearchService() throws Exception {

        setSize(600, 600);
        setResizable(false);

        JPanel mainPanel = new JPanel();
        JPanel templatePanel = new JPanel();
        JPanel toolPanel = new JPanel();

        JLabel picLabel = new JLabel();
        JLabel tools = new JLabel("Tools");
        JLabel templates = new JLabel("Templates");

        JButton upload = new JButton("Upload");
        JButton search = new JButton("Search");
        JButton save = new JButton("Save");

        //Main panel
        GridBagLayout GBPanel = new GridBagLayout();
        GridBagConstraints GBC = new GridBagConstraints();
        mainPanel.setLayout( GBPanel );

        //Template panel
        GBC.gridx = 0;
        GBC.gridy = 0;
        GBC.gridwidth = 1; 
        GBC.gridheight = 3; 
        GBC.fill = GridBagConstraints.BOTH; 
        GBC.weightx = 1; 
        GBC.weighty = 0;
        GBC.anchor = GridBagConstraints.WEST;
        GBPanel.setConstraints( leftPanel, GBC );
        leftPanel.add(templates); 
        mainPanel.add( leftPanel ); 

        //Picture label
        GBC.gridx = 1;
        GBC.gridy = 0;
        GBC.gridwidth = 2;
        GBC.gridheight = 1;
        GBC.fill = GridBagConstraints.BOTH;
        GBC.weightx = 0;
        GBC.weighty = 1;
        GBC.anchor = GridBagConstraints.CENTER;
        GBPanel.setConstraints( picLabel, GBC );
        mainPanel.add( picLabel );

        //Tool panel
        GBC.gridx = 4;
        GBC.gridy = 0;
        GBC.gridwidth = 1;
        GBC.gridheight = 3;
        GBC.fill = GridBagConstraints.BOTH;
        GBC.weightx = 1;
        GBC.weighty = 0;
        GBC.anchor = GridBagConstraints.EAST;
        GBPanel.setConstraints( rightPanel, GBC );
        rightPanel.add(tools);
        mainPanel.add( rightPanel );

        //Upload button
        GBC.gridx = 1;
        GBC.gridy = 1;
        GBC.gridwidth = 1;
        GBC.gridheight = 1;
        GBC.fill = GridBagConstraints.BOTH;
        GBC.weightx = 1;
        GBC.weighty = 0;
        GBC.anchor = GridBagConstraints.PAGE_START;
        GBPanel.setConstraints( upload, GBC );
        mainPanel.add( upload );

        //Save button
        GBC.gridx = 2;
        GBC.gridy = 1;
        GBC.gridwidth = 1;
        GBC.gridheight = 1;
        GBC.fill = GridBagConstraints.BOTH;
        GBC.weightx = 1;
        GBC.weighty = 0;
        GBC.anchor = GridBagConstraints.PAGE_START;
        GBPanel.setConstraints( save, GBC );
        mainPanel.add( save );

        //Search button
        GBC.gridx = 1;
        GBC.gridy = 2;
        GBC.gridwidth = 2;
        GBC.gridheight = 1;
        GBC.fill = GridBagConstraints.BOTH;
        GBC.weightx = 1;
        GBC.weighty = 0;
        GBC.anchor = GridBagConstraints.PAGE_START;
        GBPanel.setConstraints( search, GBC );
        mainPanel.add( search );

        add(mainPanel);

及以下是添加图片的代码

upload.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser("C:\\Users); 
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Image", "jpg", "png", "bmp");
                chooser.setFileFilter(filter);
                int result = chooser.showOpenDialog(null);

                if (result == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = chooser.getSelectedFile();
                    BufferedImage bi;
                    userPhoto = chooser.getSelectedFile().getPath();

                    try {
                        bi = ImageIO.read(selectedFile);
                        Image dimg = bi.getScaledInstance(picLabel.getWidth(), picLabel.getHeight(), Image.SCALE_SMOOTH);
                        picLabel.setIcon(new ImageIcon(dimg));
                    }
                    catch(IOException IOe) {
                        IOe.printStackTrace();
                    }
                    System.out.println(userPhoto);
                }
            }
        });

我添加了两张照片以显示程序的结果。这是我第一次跑步时的样子,以及我希望布局保持不变的样子 Before

这是上传图片后布局的外观

After

如您所见,左右面板缩小,图片甚至不占整个图片标签。

我还添加了这一行System.out.println(picLabel.getWidth());在动作侦听器中,看到第一次按下按钮时,大小设置为299,但如果我再次按下按钮,则每次都会更改,并且每次都会更改。我想知道是否有可能使图像保持在299的宽度。

1 个答案:

答案 0 :(得分:1)

    GBC.gridwidth = 5; 
    GBC.gridheight = 20

您不能只为组件随机分配gridwith / height。如果要使组件跨越与其他组件相同的高度,则实际上需要20个其他组件。

  

如您所见,左右面板缩小,图片甚至不占整个图片标签。

如果您不想更改布局,请使用其他布局管理器或具有不同布局管理器的嵌套面板。

例如,您从BorderLayout开始。

然后,您可以将面板添加到LINE_STARTLINE_END

然后,您需要在CENTER中使用另一个面板。同样,您可以使用BorderLayout。您将图片添加到CENTER,然后添加到另一个面板,其中按钮位于PAGE_END

现在,除图像外的所有组件的大小都已固定。图像的可用空间将根据框架的大小而变化。

所以基本代码是:

JPanel buttonPanel = new JPanel(...);
JLabel image = new JLabel(...);

JPanel center = new JPanel( new BorderLayout() );
center.add(image, BorderrLayout.CENTER);
center.add(buttonPanel, BorderLayout.PAGE_END);

JPanel leftPanel = new JPanel(...);
JPanel rightPanel = new JPanel(...);

frame.add(leftPanel, BorderLayout.LINE_START);
frame.add(center, BorderLayout.CENTER);
frame.add(rightPanel, BorderLayout.LINE_END);

比尝试使用GridBagLayout的所有约束要容易得多。

现在,子面板(左,右,按钮)可以使用适当的布局管理器。