2013-03-13 54 views
2

我有一個JFrame與一些兒童JPanel對象。當我調整JFrame的大小並使其變大時,一切正常,子面板正確調整大小。但是,如果我縮小JFrame,面板將保持不變,並且會裁剪掉。無論使用什麼佈局,都會發生這種情況。收縮JFrame不工作

我知道我可以使用EventListener並手動設置尺寸,但我的問題是:爲什麼會發生這種情況?爲什麼擴大時它可以正常工作,但在收縮時不會收縮?我可以解決它沒有EventListener(也許一些配置問題)?

我正在使用Netbeans 7.3,以防萬一。

==== ====編輯

雖然試圖獲得一個最小的例子,我意識到這個問題是我想補充,這是我所提出的組成部分之一。這是一個延伸java.awt.Canvas並吸引排球場的對象。

但是,我一直無法找出它爲什麼不能正確收縮。代碼如下:

import java.awt.*; 
import java.util.Arrays; 
import javax.print.attribute.standard.OrientationRequested; 

public class CourtCanvas extends Canvas { 
    private int courtHeight = 100; 
    private int courtWidth = 200; 
    private int left = 10; 
    private int top = 10; 
    private Point center = new Point(); 

    private Color bgColor = new Color(52, 153, 204); 
    private Color lineColor = new Color(255, 255, 255); 
    private Color floorColor = new Color(255, 153, 0); 

    private OrientationRequested orientation; 

    public CourtCanvas() { 
     calcDimensions(); 
     setBackground(bgColor); 

     for (int i = 0; i < localCoords.length; i++) { 
      localCoords[i] = new Point(); 
      visitCoords[i] = new Point(); 
     } 
    } 

    private void calcDimensions() { 
     if (this.getHeight() > this.getWidth()) { 
      orientation = OrientationRequested.PORTRAIT; 
      courtHeight = (int) Math.min(this.getHeight() * 0.9, this.getWidth() * 1.8); 
      courtWidth = (int) (courtHeight/2.0); 
     } 
     else { 
      orientation = OrientationRequested.LANDSCAPE; 
      courtWidth = (int) Math.min(this.getWidth()* 0.9, this.getHeight() * 1.8); 
      courtHeight = (int) (courtWidth/2.0); 

     } 

     center.x = (int) (getWidth()/2.0); 
     center.y = (int) (getHeight()/2.0); 

     left = (int) (center.x - courtWidth/2.0); 
     top = (int) (center.y - courtHeight/2.0); 
    } 

    @Override 
    public void paint(Graphics g) { 
     setBackground(bgColor); 
     calcDimensions(); 

     drawFloor(g); 
     drawLines(g); 
    } 

    private void drawFloor(Graphics g) { 
     g.setColor(floorColor); 
     g.fillRect(left, top, courtWidth, courtHeight); 
    } 

    private void drawLines(Graphics g) { 
     if (orientation == OrientationRequested.PORTRAIT) { 
      drawLines_Portrait(g); 
     } 
     else { 
      drawLines_Landscape(g); 
     } 
    } 

    private void drawLines_Portrait(Graphics g) { 
     g.setColor(lineColor); 

     // perimeter 
     g.drawRect(left, top, courtWidth, courtHeight); 

     // center line 
     g.drawLine(left, center.y, left + courtWidth, center.y); 

     // local attack line 
     g.drawLine(left, center.y + courtHeight/6, left + courtWidth, center.y + courtHeight/6); 

     // visitor attack line 
     g.drawLine(left, center.y - courtHeight/6, left + courtWidth, center.y - courtHeight/6); 
    } 

    private void drawLines_Landscape(Graphics g) { 
     g.setColor(lineColor); 

     // perimeter 
     g.drawRect(left, top, courtWidth, courtHeight); 

     // center line 
     g.drawLine(center.x, top, center.x, top + courtHeight); 

     // local attack line 
     g.drawLine(center.x - courtWidth/6, top, center.x - courtWidth/6, top + courtHeight); 

     // visitor attack line 
     g.drawLine(center.x + courtWidth/6, top, center.x + courtWidth/6, top + courtHeight); 
    } 

} 
+1

_I我使用Netbeans 7.3,以防萬一它是相關的._沒有太大的相關性。顯示你的代碼更相關。沒有看到你做了什麼,很難猜測問題是什麼。 – 2013-03-13 10:25:02

+0

@GuillaumePolet我的代碼只是Netbeans自動生成的代碼,我還沒有添加任何功能;但我會上傳一個顯示問題的簡單例子。 – ilvidel 2013-03-13 10:49:41

+2

的確,發佈一個[SSCCE](http://sscce.org)來展示問題。否則,幾乎不可能說出來。雖然NetBeans將生成編譯的代碼,但這並不意味着它不會做任何不正確的事情。 – 2013-03-13 11:47:36

回答

1

您可能正在混合Swing和AWT,輕量級和重量級組件。我的建議是隻使用Swing/Lightweight組件。

此外,在Swing中,您擴展了paintComponent而不是paint,並且您調用super方法。

Here is a rework of your code: 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Point; 

import javax.print.attribute.standard.OrientationRequested; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class CourtCanvas extends JPanel { 
    private int courtHeight = 100; 
    private int courtWidth = 200; 
    private int left = 10; 
    private int top = 10; 
    private Point center = new Point(); 

    private Color bgColor = new Color(52, 153, 204); 
    private Color lineColor = new Color(255, 255, 255); 
    private Color floorColor = new Color(255, 153, 0); 

    private OrientationRequested orientation; 
    private Point[] localCoords = new Point[5]; 
    private Point[] visitCoords = new Point[5]; 

    public CourtCanvas() { 
     calcDimensions(); 
     setBackground(bgColor); 

     for (int i = 0; i < localCoords.length; i++) { 
      localCoords[i] = new Point(); 
      visitCoords[i] = new Point(); 
     } 
    } 

    private void calcDimensions() { 
     if (this.getHeight() > this.getWidth()) { 
      orientation = OrientationRequested.PORTRAIT; 
      courtHeight = (int) Math.min(this.getHeight() * 0.9, this.getWidth() * 1.8); 
      courtWidth = (int) (courtHeight/2.0); 
     } else { 
      orientation = OrientationRequested.LANDSCAPE; 
      courtWidth = (int) Math.min(this.getWidth() * 0.9, this.getHeight() * 1.8); 
      courtHeight = (int) (courtWidth/2.0); 

     } 

     center.x = (int) (getWidth()/2.0); 
     center.y = (int) (getHeight()/2.0); 

     left = (int) (center.x - courtWidth/2.0); 
     top = (int) (center.y - courtHeight/2.0); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     calcDimensions(); 

     drawFloor(g); 
     drawLines(g); 
    } 

    private void drawFloor(Graphics g) { 
     g.setColor(floorColor); 
     g.fillRect(left, top, courtWidth, courtHeight); 
    } 

    private void drawLines(Graphics g) { 
     if (orientation == OrientationRequested.PORTRAIT) { 
      drawLines_Portrait(g); 
     } else { 
      drawLines_Landscape(g); 
     } 
    } 

    private void drawLines_Portrait(Graphics g) { 
     g.setColor(lineColor); 

     // perimeter 
     g.drawRect(left, top, courtWidth, courtHeight); 

     // center line 
     g.drawLine(left, center.y, left + courtWidth, center.y); 

     // local attack line 
     g.drawLine(left, center.y + courtHeight/6, left + courtWidth, center.y + courtHeight/6); 

     // visitor attack line 
     g.drawLine(left, center.y - courtHeight/6, left + courtWidth, center.y - courtHeight/6); 
    } 

    private void drawLines_Landscape(Graphics g) { 
     g.setColor(lineColor); 

     // perimeter 
     g.drawRect(left, top, courtWidth, courtHeight); 

     // center line 
     g.drawLine(center.x, top, center.x, top + courtHeight); 

     // local attack line 
     g.drawLine(center.x - courtWidth/6, top, center.x - courtWidth/6, top + courtHeight); 

     // visitor attack line 
     g.drawLine(center.x + courtWidth/6, top, center.x + courtWidth/6, top + courtHeight); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new CourtCanvas()); 
       frame.setSize(500, 400); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
+0

好得多。重繪速度更快。非常感謝! – ilvidel 2013-03-17 12:22:22