2012-02-25 64 views
3

在下面的例子中,我如何让JPanel占用所有的JFrame?我将首选大小设置为800x420,但实际上只填充了792x391。如何使一个JFrame内的JPanel填充整个窗口?

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics2D; 
import java.awt.image.BufferStrategy; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class BSTest extends JFrame { 
    BufferStrategy bs; 
    DrawPanel panel = new DrawPanel(); 

    public BSTest() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new BorderLayout()); // edited line 
     setVisible(true); 
     setSize(800,420); 
     setLocationRelativeTo(null); 
     setIgnoreRepaint(true); 
     createBufferStrategy(2); 
     bs = getBufferStrategy(); 
     panel.setIgnoreRepaint(true); 
     panel.setPreferredSize(new Dimension(800,420)); 
     add(panel, BorderLayout.CENTER);  // edited line 
     panel.drawStuff(); 
    } 

    public class DrawPanel extends JPanel { 
     public void drawStuff() { 
      while(true) { 
       try { 
        Graphics2D g = (Graphics2D)bs.getDrawGraphics(); 
        g.setColor(Color.BLACK); 
        System.out.println("W:"+getSize().width+", H:"+getSize().height); 
        g.fillRect(0,0,getSize().width,getSize().height); 
        bs.show(); 
        g.dispose(); 
        Thread.sleep(20); 
       } catch (Exception e) { System.exit(0); } 
      } 
     } 
    } 

    public static void main(String[] args) { 
     BSTest bst = new BSTest(); 
    } 
} 
+1

Swing引擎有它自己的paint循环,你不需要while(true)。在你的面板类中重写'JComponent.paint(Graphics g)' – 2012-02-25 05:19:30

+0

Hi @Joe,我试着在这里使用'BufferStrategy',为此你不使用'paint()',这就是为什么'while()'循环和'setIgnoreRepaint(true);'是什么。任何关于我如何能够填满整个窗户的想法? – 2012-02-25 05:23:00

+0

是的,尝试将你的JPanel传递给'JFrame.setContentPane()' – 2012-02-29 00:43:55

回答

6

如果您有只有一个框架,没有别的面板那就试试这个:

  • 设置BorderLayout的帧。
  • 帧添加面板BorderLayout.CENTER

可能这是因为在JPanel中while循环的发生。(不知道为什么?找到实际的原因。会更新的时候找到它。)如果你将其替换为paintComponent(g)方法均可正常工作:

public BSTest() { 
    //--- your code as it is 
    add(panel, BorderLayout.CENTER); 
    //-- removed panel.drawStuff(); 
} 

public class DrawPanel extends JPanel { 
    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setColor(Color.BLACK); 
     System.out.println("W:" + getSize().width + ", H:" + getSize().height); 
     g2d.fillRect(0, 0, getSize().width, getSize().height); 
    } 
} 

//your code as it is. 
+0

嗨@哈里,我刚刚尝试过你的建议(参见上面代码中新的*编辑过的行*),但它显示的完全一样! – 2012-02-25 05:24:12

+0

@fleawhale:看我的编辑。 – 2012-02-25 05:32:48

+0

谢谢,但我想在这里使用'BufferStrategy'制作一个游戏,你不使用'paintComponent',你首先绘制东西,然后你用'bs.show()' – 2012-02-25 05:39:26

3

下面是使用包替代的替代方法。

import java.awt.Color; 
import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class PackExample extends JFrame { 

    public PackExample(){ 
     JPanel panel = new JPanel(); 
     panel.setPreferredSize(new Dimension(800,600)); 
     panel.setBackground(Color.green); 
     add(panel); 
     pack(); 
     setVisible(true); 
    } 

    public static void main(String[] args){ 
    new PackExample(); 
    } 

} 
+0

用于'pack()'的+1; -1使用'setPreferredSize()'[不正确](http://stackoverflow.com/q/7229226/230513)。 – trashgod 2012-02-25 06:51:28

+0

@trashgod DYM 1)使用高级布局(AKA'kleopatra方法')2)覆盖'getPreferredSize()'3)其他?由于1)需要第三方布局,因此无法在核心J2SE中完成(这可能会也可能不会成为问题)。2)使组件的首选大小不灵活。我更愿意像jtp那样设置大小。 +1 – 2012-02-26 04:29:49

+1

@AndrewThompson:谢谢你的阐述。我的意思是2)覆盖['getPreferredSize()'](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getPreferredSize%28%29)这里](http://stackoverflow.com/a/9447425/230513),并在第一个[这里](http://stackoverflow.com/a/7229662/230513)中提到。我发现一个无法解释的'setPreferredSize()'在这种情况下可能会引起误解。它取决于面板中的内容:更多组件→'setPreferredSize()'bad;只是绘画→'setPreferredSize()'确定。 – trashgod 2012-02-26 04:52:18

1

如果你想与整个JPanel的需要setUndecorated为真,即frame.setUndecorated(true);填补的JFrame。但现在你不得不担心你的MAXIMIZE < MINIMIZE和CLOSE按钮,朝右上方(Windows操作系统)

0

这花了我一直弄清楚,但它实际上是最简单的代码。 只需创建一个父面板并传递GridLayout,然后像这样添加您的子面板。

JPanel parentPanel= new JPanel(new GridLyout()); 
JPanel childPanel= new JPanel(); 
parentPanel.add(childPanel);