JScrollPane将垂直大小设置为在最大化窗口时不调整大小

时间:2017-05-11 04:01:28

标签: java swing layout-manager gridbaglayout

我在JPanel内使用GridBagLayout JScrollPaneGridBagLayout在某些列(蓝色)之间设置了线条分割边框。问题是我无法使行为正常,同时从JScrollPane中删除不必要的右滚动条。

守则......

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestGui extends JFrame {
    //**************************************************************************************
    //************************************** Variables *************************************
    //**************************************************************************************
    private String[] showHideComboBoxValue = {"Show hiddenLabel", "Hide hiddenLabel"};
    private JComboBox showHideComboBox = createShowHideComboBox(showHideComboBoxValue);
    private JLabel labelHiddenOrShown = createDefaultLabel("This label is hidden or shown depending on the status of the combo box", 14);
    private JPanel topFrame = createTopFrame();
    private JScrollPane topFrameScroll = createTopScrollPane();
    private JScrollPane centerFrameScroll = createCenterScrollPane();

    //**************************************************************************************
    //************************************* Constructor ************************************
    //**************************************************************************************
    private TestGui() {
        add(topFrameScroll, BorderLayout.NORTH);
        add(centerFrameScroll, BorderLayout.CENTER);

        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //**************************************************************************************
    //*********************************** Support Method ***********************************
    //**************************************************************************************
    private static GridBagConstraints setGbc(int gridx, int gridy, int gridWidth, int gridHeight, int ipadx, int ipady, String anchorLocation, double weightx, double weighty, Insets insets, boolean fillCell){
        GridBagConstraints gbc = new GridBagConstraints();

        if (anchorLocation.toUpperCase().equals("NORTHWEST")){
            gbc.anchor = GridBagConstraints.NORTHWEST;
        } else if (anchorLocation.toUpperCase().equals("NORTH")){
            gbc.anchor = GridBagConstraints.NORTH;
        } else if (anchorLocation.toUpperCase().equals("NORTHEAST")){
            gbc.anchor = GridBagConstraints.NORTHEAST;
        } else if (anchorLocation.toUpperCase().equals("WEST")){
            gbc.anchor = GridBagConstraints.WEST;
        } else if (anchorLocation.toUpperCase().equals("EAST")){
            gbc.anchor = GridBagConstraints.EAST;
        } else if (anchorLocation.toUpperCase().equals("SOUTHWEST")){
            gbc.anchor = GridBagConstraints.SOUTHWEST;
        } else if (anchorLocation.toUpperCase().equals("SOUTH")){
            gbc.anchor = GridBagConstraints.SOUTH;
        } else if (anchorLocation.toUpperCase().equals("SOUTHEAST")){
            gbc.anchor = GridBagConstraints.SOUTHEAST;
        } else {
            gbc.anchor = GridBagConstraints.CENTER;
        }

        gbc.gridx = gridx; // column
        gbc.gridy = gridy; // row
        gbc.gridwidth = gridWidth; // number of columns
        gbc.gridheight = gridHeight; // number of rows
        gbc.ipadx = ipadx; // width of object
        gbc.ipady = ipady; // height of object
        gbc.weightx = weightx; // shifts columns to side of set anchor
        gbc.weighty = weighty; // shifts rows to side of set anchor
        gbc.insets = insets; // placement inside cell
        if (fillCell){
            gbc.fill = GridBagConstraints.BOTH;
        }

        return gbc;
    }

    //**************************************************************************************
    //*********************************** Object Methods ***********************************
    //**************************************************************************************
    private JComboBox createShowHideComboBox(String[] comboValues){
        JComboBox comboBox = new JComboBox(comboValues);
        comboBox.setPrototypeDisplayValue("X" + comboValues[0] + "X");

        return comboBox;
    }

    private void createShowHideComboBoxAction(){
        showHideComboBox.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        String selection = ((JComboBox) (e.getSource())).getSelectedItem().toString();
                        if (selection.equals(showHideComboBoxValue[1])){
                            labelHiddenOrShown.setVisible(false);
                        } else {
                            labelHiddenOrShown.setVisible(true);
                        }
                    }
                }
        );
    }

    private JLabel createDefaultLabel(String text, int textSize){
        JLabel lbl = new JLabel(text);
        lbl.setFont(new Font(text, Font.BOLD, textSize));
        return lbl;
    }

    //**************************************************************************************
    //************************************ Panel Methods ***********************************
    //**************************************************************************************
    private JPanel createTopFrame() {
        JPanel pnl = new JPanel();

        Border lineSplitterBoarder = BorderFactory.createMatteBorder(0, 0, 0, 5, Color.BLUE);
        JLabel lineSplitterOne = new JLabel();
        lineSplitterOne.setBorder(lineSplitterBoarder);
        JLabel lineSplitterTwo = new JLabel();
        lineSplitterTwo.setBorder(lineSplitterBoarder);

        pnl.setLayout(new GridBagLayout());
        createShowHideComboBoxAction();
        pnl.add(showHideComboBox,setGbc(0,1, 1,1, 0,0,"CENTER", 0, 0, new Insets(10, 10, 10, 0), false));
        pnl.add(createDefaultLabel("Label",14), setGbc(1,0,1,1,0,0,"CENTER",0,0,new Insets(10,10,0,10),false));
        pnl.add(createDefaultLabel("Label",14), setGbc(1,1,1,1,0,0,"CENTER",0,0,new Insets(0,10,0,10),false));
        pnl.add(createDefaultLabel("Label",14), setGbc(1,2,1,1,0,0,"CENTER",0,0,new Insets(0,10,0,10),false));
        pnl.add(createDefaultLabel("Label",14), setGbc(1,3,1,1,0,0,"CENTER",0,0,new Insets(0,10,10,10),false));
        pnl.add(lineSplitterOne, setGbc(2,0,1,4,0,0,"CENTER",0,0,new Insets(0,0,0,0),true));
        pnl.add(createDefaultLabel("Hidden Label Below", 14), setGbc(3,0,8,1,9,9,"CENTER",0,0,new Insets(10,10,0,10),false));
        JPanel labelHiddenOrShownPanel = new JPanel();
        labelHiddenOrShownPanel.setLayout(new GridLayout(1,1));
        labelHiddenOrShownPanel.add(labelHiddenOrShown);
        pnl.add(labelHiddenOrShownPanel, setGbc(3,1,1,1,0,0,"WEST",0,0,new Insets(0,5,0,0),false));
        pnl.add(lineSplitterTwo, setGbc(11,0,1,4,0,0,"CENTER",0,0,new Insets(0,5,0,0),true));
        pnl.add(createDefaultLabel("Label",14), setGbc(12,0,1,1,0,0,"CENTER",0,0,new Insets(10,10,0,0),false));
        pnl.add(createDefaultLabel("Label",14), setGbc(12,1,1,1,0,0,"CENTER",0,0,new Insets(0,10,0,0),false));
        pnl.add(createDefaultLabel("Label",14), setGbc(12,2,1,1,0,0,"CENTER",0,0,new Insets(0,10,0,0),false));
        pnl.add(createDefaultLabel("Label",14), setGbc(12,3,1,1,0,0,"CENTER",0,0,new Insets(0,10,10,0),false));

        return pnl;
    }

    private JScrollPane createTopScrollPane(){
        JScrollPane scrollPane = new JScrollPane();
        Border raisedBevel = BorderFactory.createRaisedBevelBorder();
        Border lineBorder = BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(224,224,224));
        Border loweredBevel = BorderFactory.createLoweredBevelBorder();
        Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
        Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);

        //scrollPane.setPreferredSize(new Dimension(0, 160));
        scrollPane.setBorder(compoundFinal);
        scrollPane.getViewport().setView(topFrame);
        return scrollPane;
    }

    private JScrollPane createCenterScrollPane(){
        JScrollPane scrollPane = new JScrollPane();
        Border raisedBevel = BorderFactory.createRaisedBevelBorder();
        Border lineBorder = BorderFactory.createMatteBorder(2, 2, 2, 2, new Color(224,224,224));
        Border loweredBevel = BorderFactory.createLoweredBevelBorder();
        Border compoundSetup = BorderFactory.createCompoundBorder(raisedBevel, lineBorder);
        Border compoundFinal = BorderFactory.createCompoundBorder(compoundSetup, loweredBevel);

        scrollPane.setBorder(compoundFinal);
        return scrollPane;
    }

    //**************************************************************************************
    //************************************ Start Program ***********************************
    //**************************************************************************************
    public static void main(String[] args) {
        new TestGui();
    }
}

重现问题的步骤......

第1步:运行程序并观察顶部JScrollPane右侧不必要的滚动条... enter image description here

第2步:最大化窗口,并观察两个滚动条消失,JPanel中的对象放在应该放在哪里... enter image description here 这种行为很好,我们希望保留这一点。您现在可以关闭该程序。

第3步:现在要删除右侧滚动条(在步骤1中),我们可以将一行代码(第#145行)从//scrollPane.setPreferredSize(new Dimension(0, 160));更改为scrollPane.setPreferredSize(new Dimension(0, 160));,并重新运行该程序...... enter image description here 大!正确的栏被删除。但等一下......

第4步:现在让我们最大化窗口...... enter image description here 注意边框线之间添加了额外的空间(我们不希望出现这种情况)。

注1:保持显示/隐藏隐藏标签的选项非常重要,而无需调整GridBagLayout中的单元格大小。经过研究,我发现这样做的唯一方法是使用JLabel将隐藏的JPanel隐藏在GridLayout中。

注2:我注意到在宽度变得足够长以至于需要水平滚动条开始出现之后,这种行为(在步骤1中)开始发生。

我需要的是图像3(窗口未最大化时)和图像2(窗口最大化时)的行为。如果有人有任何想法让这两种行为组合在一起工作,请帮忙。感谢

更新:由于代码投诉过大,我将其缩短为169行。如上所述,此代码中的每个组件都需要监视2个行为。

0 个答案:

没有答案