缩小JFrame无法正常工作

时间:2013-03-13 09:59:29

标签: java swing user-interface resize jframe

我有JFrame个子JPanel个对象。当我调整JFrame的大小并使其变大时,一切正常并且子面板正确调整大小。但是,如果我缩小JFrame,则面板保持不变,并且它们会被裁剪。无论我使用什么布局,都会发生这种情况。

我知道我可以使用EventListener并手动设置大小,但我的问题是:为什么会发生这种情况?为什么在放大时它能正常工作,但在缩小时却不能正常工作?我可以在没有EventListener(可能是某些配置问题)的情况下解决它吗?

我正在使用Netbeans 7.3,以防它相关。

====编辑====

在尝试获取最小的示例时,我意识到问题是我试图添加的组件之一,这是我做的。这是一个延伸java.awt.Canvas的对象,并绘制了一个VolleyBall球场。

但是,我无法找出它为什么不能正常收缩。这是代码:

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

}

2 个答案:

答案 0 :(得分:2)

可能有许多原因导致所描述的行为。 例如,设置为面板的最小尺寸。

Java布局管理器应该对您有用。 教程:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

答案 1 :(得分: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);
            }
        });
    }
}