在Gridlayout中增加行大小

时间:2015-10-13 13:11:29

标签: java swing grid-layout

上面的框架包含有两行的gridlayout。第一行是文本区域,第二行是带有两个复选框的面板。我想增加第一行的高度,这样第一行应该是总高度的75%,第二行应该是25%。我怎样才能做到这一点?这是我的代码片段:

   setLayout(new GridLayout(2, 0, 0, 0));       
    Panel text_panel = new Panel();
    add(text_panel);
    text_panel.setLayout(new GridLayout(1, 0, 0, 0));   
    JTextArea textArea = new JTextArea();
    textArea.setText("text to be displayed");
    JScrollPane scroll = new JScrollPane (textArea);
    text_panel.add(scroll);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setEditable(false);
    Border border = BorderFactory.createLineBorder(Color.GRAY);
    textArea.setBorder(border);
    textArea.setFont(new Font("Arial",Font.PLAIN,12));
    textArea.setCaretPosition(0);
    textArea.requestFocus();
    Panel checebox_panel = new Panel();
    checebox_panel.setBackground(Color.WHITE);
    add(checebox_panel);
    checebox_panel.setLayout(new GridLayout(1, 0, 0, 0));


    androidCheckBox = new JCheckBox("Open start page");
    androidCheckBox.setBackground(Color.WHITE);
    androidCheckBox.addItemListener(itemListener);
    androidCheckBox.setSelected(true);
    checebox_panel.add(androidCheckBox);

    eclipseCheckBox = new JCheckBox("register for updates");
    eclipseCheckBox.setBackground(Color.WHITE);
    eclipseCheckBox.addItemListener(itemListener);
    eclipseCheckBox.setSelected(true);
    checebox_panel.add(eclipseCheckBox);

3 个答案:

答案 0 :(得分:2)

使用GridLayout,您不能拥有两个不同大小的行。看看BoxLayout。像这样:

JPanel content = new JPanel();
frame.getContentPane().add(content);
LayoutManager layout = new BoxLayout(content, BoxLayout.Y_AXIS);
Box boxes[] = new Box[2];
boxes[0] = Box.createHorizontalBox();
boxes[1] = Box.createHorizontalBox();

boxes[0].createGlue();
boxes[1].createGlue();

content.add(boxes[0]);
content.add(boxes[1]);  

JPanel panel = new JPanel();    
JPanel panel2 = new JPanel();

panel.setPreferredSize(new Dimension(500,300));
panel2.setPreferredSize(new Dimension(500,200));

boxes[0].add(panel);
boxes[1].add(panel2);

使用setPreferredSize永远不是最佳选择,但它有效。这只是你如何做到的一个例子,我敢肯定有更好的方法! ;)

答案 1 :(得分:1)

GridLayout无法做到这一点。 GridLayout将始终使用均匀间距。查看另一个布局管理器。

这是一个很好的参考:

https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

答案 2 :(得分:0)

“在Gridlayout中增加行大小” 我遇到了这一要求,并且通过尝试使用一种解决方案,我得到了一个解决方案,尝试使用边框代替网格布局。可能会有所帮助:)

代码如下:

import ComponentMeta.RequiredComp;
import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PromptPopup extends JDialog {
    private final JScrollPane scrollPane = new JScrollPane();
    private int rows;
    private int cols;
    private int vGap;
    private int hGap;
    private Map<String, Component> componentRepo;

    public PromptPopup(JFrame parent) {
        super(parent);
        componentRepo = new HashMap<>();
        this.setModalityType(ModalityType.APPLICATION_MODAL);
    }

    public void setComponentsDisplayStyle(int rows, int cols, int vGap, int hGap) {
        this.rows = rows;
        this.cols = cols;
        this.vGap = vGap;
        this.hGap = hGap;
    }

    public void setComponentReop() {
        JTextField dynamicParamTextField = new JTextField();
        this.componentRepo.put("COMPANY_CODE", dynamicParamTextField);
        JTextField dynamicParamTextField2 = new JTextField();
        this.componentRepo.put("DIST_CODE", dynamicParamTextField2);
        JTextField dynamicParamTextField3 = new JTextField();
        this.componentRepo.put("LOCA_CODE", dynamicParamTextField3);
        JTextField dynamicParamTextField4 = new JTextField();
        this.componentRepo.put("TOKEN_EXEC", dynamicParamTextField4);
    }

    public void initPopupUI() {
        //Setting content panes layout
        getContentPane().setLayout(new BorderLayout(0, 0));

        //Creating a root panel(root container) to hold the child components
        JPanel rootContainer = new JPanel();
        rootContainer.setLayout(new BorderLayout());

        //Creating header panel(header container) to hold the header components
        JPanel header = new JPanel();
        header.setLayout(new FlowLayout());
        JLabel headerText = new JLabel("Source query parameters required ");
        headerText.setForeground(Color.WHITE);
        header.add(headerText);
        header.setBackground(Color.BLUE);

        //Creating footer panel(footer container ) to hold the footer components
        JPanel footer = new JPanel();
        footer.setLayout(new FlowLayout());
        JButton executeWithParamsButton = new JButton("Execute with params");
        executeWithParamsButton.setBackground(Color.BLACK);
        executeWithParamsButton.setForeground(Color.WHITE);
        JButton cancelButton = new JButton("Cancel");
        cancelButton.setBackground(Color.RED);
        cancelButton.setForeground(Color.WHITE);
        footer.add(executeWithParamsButton);
        footer.add(cancelButton);
        footer.setBackground(Color.BLUE);

        //Creating content panel(content container) to hold the all dynamically generated components
        JPanel contentContainer = new JPanel();
        GridLayout gridLayout = new GridLayout(this.rows, this.cols, this.hGap, this.vGap);
        contentContainer.setLayout(gridLayout);
        for (Map.Entry entry : componentRepo.entrySet()) {
            JLabel dynamicParamLabel = new JLabel(entry.getKey().toString());
            contentContainer.add(dynamicParamLabel);
            contentContainer.add((Component) entry.getValue());
        }


        // Adding all the created containers to the root container one by one
        rootContainer.add(header, BorderLayout.NORTH);
        rootContainer.add(contentContainer, BorderLayout.CENTER);
        rootContainer.add(footer, BorderLayout.SOUTH);

        //Adding the root container to the scroll pane in order the view to be scrollable nno matter how many components are there.
        scrollPane.setViewportView(rootContainer);
        getContentPane().add(scrollPane);
    }
}

这是我想要的输出,即,仅基于其组件,页眉位于顶部,而“中心”面板和包含按钮控件的页脚相同。

output