我有一个JFrame
与一些子JPanel
对象。当我调整JFrame的大小并使其变大时,一切都工作正常,子面板也正确地调整了大小。但是,如果我收缩JFrame,面板保持不变,并且它们被裁剪。无论我使用什么布局,都会发生这种情况。
我知道我可以使用EventListener
并手动设置大小,但我的问题是:为什么会发生这种情况?为什么放大时效果很好,缩小时却不行?我可以在没有EventListener
的情况下解决它吗(也许是一些配置问题)?
我使用Netbeans 7.3,如果它是相关的。
==== EDIT ====
在试图得到一个最小的例子时,我意识到问题是我试图添加的组件之一,这是我做的。它是一个扩展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);
}
}
可能有许多原因导致所描述的行为。例如,设置在面板上的最小尺寸。
应该为你工作的是Java布局管理器。教程:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
您可能正在混合Swing和AWT,轻量级和重量级组件。我的建议是只使用Swing/轻量级组件。
此外,在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);
}
});
}
}