2012-10-31 202 views
2

任何人都可以指出我的错在哪里这个java swing gui代码。我想两个按钮添加到JPanel,然后将其设置规模后加入到一个框架,但它似乎没有响应传递给它Java Swing设置JPanel大小

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,但正在向BorderLayout.CENTER添加PaintSurface。你不应该设置大小。根据布局组件的大小让布局决定合适的尺寸。 –

+0

编辑也不好。 –

回答

4

这可能不是回答你的眼前问题setSize值。 ..但...

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(你应该),因为它会被LayoutManager覆盖(这实际上是它的工作),所以调用setSize()/setBounds()/setLocation()毫无意义。

和猜测,你Test类扩展JFrame,通过调用this.add(buttonPanel); this.add(new PaintSurface());要添加两种成分以相同的约束(BorderLayout.CENTER,因为BorderLayoutJFrame的内容窗格中的默认布局管理)的内容窗格。

考虑阅读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(); 
      } 
     }); 
    } 
} 
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); 
      } 
     }); 

    } 
}