设置GridBagLayout网格的大小

胡安科

我正在尝试使用Java Swing(GridBagLayout)创建一个控制台。

我不知道为什么,但是如您所见,在左边距处,网格的尺寸不正确。

代码输出

应该以这种方式显示: 在此处输入图片说明

列表中浅蓝色,图像为绿色,文本面板为橙色,文本字段为黄色。

我不知道如何使列表变大和图像变小。文本字段的网格也绑定到列表一,即使列表位于y 1且文本字段位于y 2也是如此。

这是一些代码。

// Command List
DefaultListModel<String> listInput = new DefaultListModel<String>();
JList<String> list = new JList<String>(listInput);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(list);
list.setBackground(new Color(160, 160, 160));
list.setSelectionBackground(new Color(150, 150, 150));
scrollPane.setPreferredSize(new Dimension(20, 20));
manager.setCommandList(listInput);

    c.insets = new Insets(2, 2, 2, 2);
    c.ipady = 0;
    c.ipadx = 100;
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 2;
    c.weightx = 0.1;
    c.weighty = 0.6;
    c.fill = GridBagConstraints.BOTH;
console.add(scrollPane, c);

// Image Displayer
JLabel image = new JLabel(new ImageIcon());
manager.setImageField(image);

    c.insets = new Insets(0, 0, 0, 0);
    c.ipady = 0;
    c.ipadx = 0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 0.1;
    c.weighty = 0.3;
    c.fill = GridBagConstraints.BOTH;
console.add(image, c);

where 'c' is a grid bag constraint and console the main JPanel. As you can see, the list has a grid height of 2 and weight of 0.6, and the image a grid height of 1 and weight of 0.9, so not sure why the list is 4 times smaller than the image.

Another issue, I've added a listener to the JLabel holding the image (on resize), anyways, it isn't called. Should I add the listener to the main panel? as the image is only being resized by the layout manager.

Thanks ^^

EDIT: SSCCE:

package co.relieved.jelly.application.display.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;

@SuppressWarnings("serial")
public class Test extends JPanel {

    static JLabel image;

    public static void main(String[] args) {
        display();
        try {
            BufferedImage buffer = ImageIO
                .read(new File("/home/juanco/Pictures/Screenshot from 2016-02-08 22-43-22.png"));
            image.setIcon(new ImageIcon(
                buffer.getScaledInstance(image.getWidth(), image.getHeight(), BufferedImage.SCALE_SMOOTH)));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public Test() {
        super(new GridLayout(1, 1));
        JTabbedPane tabs = new JTabbedPane();

        /*** >>> Console Pane <<< ***/
        JPanel console = new JPanel();
        console.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.ipadx = 0;
        c.ipady = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.CENTER;
        c.fill = GridBagConstraints.BOTH;

        // Console Screen
        JTextPane screen = new JTextPane();
        screen.setEditable(false);

        c.gridx = 1;
        c.gridy = 0;
        c.gridheight = 2;
        c.weightx = 0.8;
        c.weighty = 1;
        console.add(screen, c);

        // Console Input
        JTextField input = new JTextField();

        c.insets = new Insets(2, 0, 2, 0);
        c.ipady = 3;
        c.gridy = 2;
        c.gridheight = 1;
        c.weighty = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        console.add(input, c);

        // Command List
        DefaultListModel<String> listInput = new DefaultListModel<String>();
        listInput.setSize(1);
        JList<String> list = new JList<String>(listInput);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scrollPane = new JScrollPane(list);

        c.insets = new Insets(2, 2, 2, 2);
        c.ipady = 0;
        c.ipadx = 100;
        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 2;
        c.weightx = 0.1;
        c.weighty = 0.6;
        c.fill = GridBagConstraints.BOTH;
        console.add(scrollPane, c);

        // Image Displayer
        image = new JLabel(new ImageIcon());

        c.insets = new Insets(0, 0, 0, 0);
        c.ipadx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.weighty = 0.3;
        console.add(image, c);

        // General
        tabs.addTab("Console", console);

        /*** >>> Logs Pane <<< ***/
        JPanel logs = new JPanel();

        tabs.addTab("Logs", logs);

        // Setup
        tabs.setSelectedIndex(0);
        add(tabs);
    }

    static void display() {
        JFrame frame = new JFrame("Relieved Console");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setPreferredSize(new Dimension(800, 500));
        frame.add(new Test(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
camickr

Here's some code.

Which doesn't help. The grid can only be completed with the entire code. That is we need to know the gridwidth and gridheight for all components in order to determine the allocation of space to each component in the grid.

the text field's grid is binded to the list one, even tough the list is on y 1 and the text field on y 2.

You can't just randomly assign a component to a grid. The component will only go to grid 2 if the component above it has a grid height of 2. So basically each of your columns needs to have components with a total grid height of 3.

I don't know how to make the list bigger

Setting a preferred size of (20, 20) doesn't help. Anyway you should not be using the setPreferredSize() method.

Instead you should be using:

list.setVisibleRowCount(...);

to specify the visible rows. Then the JList can determine its own preferred size.

Another layout option is to use nested panels which can simplify the layout.

So you could start with a "west" panel that uses a BorderLayout. Then you add the label to "PAGE_START" and the list to "CENTER".

然后,您创建一个“中心”面板。将主要组件添加到“ CENTER”,并将文本字段添加到“ PAGE_START”。

然后将两个面板添加到框架:

frame.add(westPanel, BorderLayout.LINE_START);
frame.add(centerPanel, BorderLayout.CENTER);

编辑:

抱歉,我回想一下有关使每列的网格高度为3的评论。您不能仅将总网格高度指定为3,因为每列中只有2个组件,所以每个组件的高度只能为1 。

在这篇文章中查看我的答案:为什么此GridBagLayout不能按计划显示?允许您通过在行/列中使用不可见组件来操纵gridHeight / Weight的hack。

但是,我不推荐这种方法。使用BorderLayout(或嵌套面板上的其他布局管理器)使用嵌套面板的建议将容易得多。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章