Java Swing设置JPanel大小

3

有人能指出这个Java Swing GUI代码的问题所在吗?我试图将两个按钮添加到一个JPanel中,然后在设置了大小之后将其添加到框架中,但它似乎不响应传递给它的setSize值。

public Test() {
    GridLayout layout = new GridLayout(1, 2);
    //this.setLayout(layout);
    this.setSize(700, 700);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    buttonPanel = new JPanel();
    buttonPanel.setSize(new Dimension(30, 100));

    JButton rectButton = new JButton("Rectangle");
    JButton ovalButton = new JButton("Oval");
    buttonPanel.add(rectButton);
    buttonPanel.add(ovalButton);
    this.add(buttonPanel);
    this.add(new PaintSurface());
    this.setVisible(true);  
}

1
你的代码不太合理。你正在使用GridLayout,但却将PaintSurface添加到BorderLayout.CENTER中。你不应该设置大小。应让布局根据其所布局的组件的大小决定适当的大小。 - JB Nizet
那个编辑也不好。 - Branislav Lazic
3个回答

7

这可能并不直接回答你的问题...但是...

GridLayout layout = new GridLayout(1, 2);
this.setLayout(layout);
// You're original code...
// Why are you using `BorderLayout.CENTER` on a `GridLayout`
this.add(new PaintSurface(), BorderLayout.CENTER);

您将布局设置为GridLayout,但是您正在使用BorderLayout约束来应用其中一个组件?此外,请确保您的代码中没有其他对Test#pack的调用,因为这将覆盖setSize的值。
请记住,JFrame的默认布局管理器是BorderLayout,因此即使您调用了buttonPanel.setSize,它很可能还是会被布局管理器覆盖。我建议您阅读一下A Visual Guide to Layout ManagersUsing Layout Managers,以找到最符合您要求的布局管理器。如果找不到一个布局管理器,则考虑使用具有不同布局管理器的复合组件,以使布局更接近您想要实现的效果。

1
根据您的更新答案,您没有在任何地方设置布局。
无论如何,如果您使用LayoutManager(您应该这样做),调用setSize()/ setBounds()/ setLocation()是没有意义的,因为它将被LayoutManager覆盖(这实际上是它的工作)。
猜测您的Test类扩展了JFrame,通过调用this.add(buttonPanel); this.add(new PaintSurface()); 您正在向内容窗格添加两个具有相同约束条件(BorderLayout.CENTER,因为BorderLayout是JFrame内容窗格的默认LayoutManager)的组件。
考虑阅读LayoutManager教程。
仅供参考,尽管远非完美,但这显示了一些“工作内容”:
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test extends JFrame {
    private JPanel buttonPanel;

    public class PaintSurface extends JButton {
        public PaintSurface() {
            super("Paint surface dummy");
        }
    }

    public Test() {
        GridLayout layout = new GridLayout(1, 2);
        this.setLayout(layout);
        this.setSize(700, 700);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buttonPanel = new JPanel();
        buttonPanel.setSize(new Dimension(30, 100));

        JButton rectButton = new JButton("Rectangle");
        JButton ovalButton = new JButton("Oval");
        buttonPanel.add(rectButton);
        buttonPanel.add(ovalButton);
        this.add(buttonPanel);
        this.add(new PaintSurface());
        this.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
}

1

好的,我会给您提供一个解决方案:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Cobie extends JFrame{
    JButton rectButton = new JButton("Rectangle");
    JButton ovalButton = new JButton("Oval");

    JPanel buttonPanel = new JPanel();
    JPanel paintSurface = new JPanel();

    public Cobie(){
        setLayout(new GridLayout(2,1));
        buttonPanel.setBackground(Color.RED);
        paintSurface.setBackground(Color.BLUE);
        buttonPanel.add(rectButton);
        buttonPanel.add(ovalButton);
        add(buttonPanel);
        add(paintSurface);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable(){
            public void run(){
                Cobie c = new Cobie();
                c.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                c.setSize(600,400); //Avoid using this method
                c.setVisible(true);
            }
        });

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接