【问题标题】:Java Swing setting JPanel SizeJava Swing 设置 JPanel 大小
【发布时间】:2012-10-31 23:19:33
【问题描述】:

谁能指出我在使用这个 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);  
}

【问题讨论】:

  • 您的代码没有多大意义。您正在使用 GridLayout,但正在将 PaintSurface 添加到 BorderLayout.CENTER。你不应该设置尺寸。让布局根据其布局的组件的大小来决定合适的大小。
  • 那个编辑也不好。

标签: java swing layout layout-manager


【解决方案1】:

这可能无法回答您的直接问题...但是...

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 else 的调用,因为这将覆盖 setSize 的值

已更新(从更改到问题)

请记住,JFrame 的默认布局管理器是 BorderLayout,因此即使您调用 buttonPanel.setSize,它也很可能已经开始被布局管理器覆盖。

我会仔细阅读 A Visual Guide to Layout ManagersUsing Layout Managers 以找到最符合您要求的布局管理器。

如果您找不到单个组件,请考虑使用具有不同布局管理器的复合组件,以使布局更接近您想要实现的目标。

【讨论】:

    【解决方案2】:

    好的,我给你一个解决方案:

    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);
                }
            });
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      根据您更新的答案,您没有在任何东西上设置布局。

      无论如何,如果您使用 LayoutManager(您应该这样做),调用 setSize()/setBounds()/setLocation() 是没有意义的,因为它将被 LayoutManager 覆盖(这实际上是它的工作)。

      并且猜测您的Test 类扩展了JFrame,通过调用this.add(buttonPanel); this.add(new PaintSurface());,您正在添加两个具有相同约束的组件(BorderLayout.CENTER,因为BorderLayout 是@ 内容窗格的默认布局管理器987654328@) 到内容窗格。

      考虑阅读LayoutManager tutorial

      仅供参考,虽然远非完美,但这表明一些“工作”:

      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();
                  }
              });
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        • 1970-01-01
        • 2014-10-03
        • 1970-01-01
        • 1970-01-01
        • 2014-03-15
        相关资源
        最近更新 更多