2012-02-25 113 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);