JScrollPane的内容不可见

huangapple go评论77阅读模式
英文:

Contents of JScrollPane are not visible

问题

public class Main {
    public static void main(String[] args) {

        JFrame frame = new JFrame("Hex");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent inputs = new InputPanel();
        JComponent hexGrid = new HexGridPanel(10, 10, 30);
        JComponent outputs = new OutputPanel();
        JComponent toolbar = new ToolbarPanel(); // This one is having problems

        Container pane = frame.getContentPane();

        pane.add(inputs, BorderLayout.LINE_START);
        pane.add(hexGrid, BorderLayout.CENTER);
        pane.add(outputs, BorderLayout.LINE_END);
        pane.add(toolbar, BorderLayout.PAGE_END); // This one is having problems

        frame.pack();
        frame.setVisible(true);
    }
}
public ToolbarPanel() {

    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));

    ButtonGroup buttonGroup = new ButtonGroup();

    JRadioButton button = new JRadioButton("Test");
    buttonGroup.add(button);
    content.add(button);

    JScrollPane scroll = new JScrollPane(content);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    content.setBorder(new LineBorder(Color.RED));
    scroll.setBorder(new LineBorder(Color.GREEN));

    this.add(scroll);
    this.setPreferredSize(new Dimension(900, 200));
    this.setBorder(new LineBorder(Color.BLACK)); // Only this is showing up in the UI

}

The ToolbarPanel itself shows up, but not the scroll pane or the radio buttons. It should show up inside the black rectangle at the bottom of this image.

英文:

I have a main class:

public class Main {
	public static void main(String[] args) {

		JFrame frame = new JFrame("Hex");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JComponent inputs = new InputPanel();
		JComponent hexGrid = new HexGridPanel(10,10,30);
		JComponent outputs = new OutputPanel();
		JComponent toolbar = new ToolbarPanel(); // This one is having problems

		Container pane = frame.getContentPane();

		pane.add(inputs,  BorderLayout.LINE_START);
		pane.add(hexGrid, BorderLayout.CENTER);
		pane.add(outputs, BorderLayout.LINE_END);
		pane.add(toolbar, BorderLayout.PAGE_END); // This one is having problems

		frame.pack();
		frame.setVisible(true);
	}
}

And all of my other panels work except for ToolbarPanel that for some reason does not show its content:

public ToolbarPanel(){

	JPanel content = new JPanel();
	content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));

	ButtonGroup buttonGroup = new ButtonGroup();

	JRadioButton button = new JRadioButton("Test");
	buttonGroup.add(button );
	content.add(button );

	JScrollPane scroll = new JScrollPane(content);
	scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
	scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

	content.setBorder(new LineBorder(Color.RED));
	scroll.setBorder(new LineBorder(Color.GREEN));

	this.add(scroll);
	this.setPreferredSize(new Dimension(900, 200));
	this.setBorder(new LineBorder(Color.BLACK)); // Only this is showing up in the UI

}

The ToolbarPanel itself shows up, but not the scroll pane or the radio buttons. It should show up inside of the black rectangle at the bottom of this image:
JScrollPane的内容不可见

答案1

得分: 1

好的,以下是您要翻译的内容:

嗯,您没有包含一个 MRE(最小可复现示例),我也没有看到您的类的声明,但我猜您正在使用:

    public class ToolbarPanel extends JComponent

问题是,默认情况下,JComponent 没有布局管理器,因此您看不到您的组件。

如果您使用:

    public class ToolbarPanel extends JPanel

情况会稍微好一些,但所有组件都会显示在一个小正方形中。

因此,您还需要在构造函数中添加

    setLayout( new BorderLayout() );

注意:

这就是为什么每个问题都应该包含一个最小可复现示例,我们不应该花时间猜测您可能正在做什么的原因。
英文:

Well, you didn't include a MRE and I don't see the declaration of your class but I'm guessing you are using:

public class ToolbarPanel extends JComponent

The problem is that by default a JComponent doesn't have a layout manager so you won't see your components.

If you use:

public class ToolbarPanel extends JPanel

It will be a little better, but all the components will be displayed in a small square.

So you will also want to add

setLayout( new BorderLayout() );

to your constructor.

Note:

This is why a minimal reproducible example should be included with every question. We should not have to spend time guessing what you may or may not be doing.

huangapple
  • 本文由 发表于 2020年8月24日 08:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63553157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定