我可以使用我实例化的customerId
个对象之一,并以某种方式将其设置为与其基础MyHouse
容器成比例地缩放吗?
当我使用从NscComponent
继承的setSize
方法时,它不会更改NscComponent
对象的大小。它使容器变小,但不是对象,容器调整大小并剪切掉大部分house1 MyHouse
对象。我曾尝试在house1
中创建一个新方法,它调整组成整个房子的每个MyHouse.java
组件的大小,然后工作,但随后坐标全部搞砸了,房子本身看起来像一切都被打破了。
似乎必须操纵MyHouse
对象的每个单独组件才能调整大小,从而无法创建对象。
以下是MyHouse
代码:
MyHouse
这是/**
* A new graphic component. This component is used in
* the notes to highlight the process of designing a
* new object and then implementing it.
*
* @author Dan Jinguji
* @author Chris Wilson
* @version Demo: MyHouse
*/
public class MyHouse extends NscComponent {
// instance variables
private NscUpTriangle theRoof;
private NscRectangle theWalls;
private NscRectangle theDoor;
private NscRectangle theWindow;
private NscRectangle theWindowPane;
/**
* Constructor for objects of class MyHouse.
* This creates a MyHouse object at the specified
* location
* @param x the x-coordinate for the object
* @param y the y-coordinate for the object
*/
public MyHouse(int x, int y) {
// Specify the constructor for the superclass
super(x, y, 120, 90);
// Create the roof object
theRoof = new NscUpTriangle(0, 0, 120, 40);
// Set the characteristics of the roof
theRoof.setFilled(true);
theRoof.setBackground(new java.awt.Color(0x99, 0x33, 0x00));
// Place the roof in the MyHouse object
add(theRoof);
// Create the walls object
theWalls = new NscRectangle(10, 40, 100, 50);
// Set the characteristics of the walls
theWalls.setFilled(true);
theWalls.setBackground(java.awt.Color.blue);
// Place the walls in the MyHouse object
add(theWalls);
// Create the door object
theDoor = new NscRectangle(48, 50, 24, 40);
// Set the characteristics of the door
theDoor.setFilled(true);
theDoor.setBackground(new java.awt.Color(0x99, 0x66, 0x33));
// Place the door in the MyHouse object
add(theDoor);
// Create the window object
theWindow = new NscRectangle(80, 50, 25, 20);
// Set color
theWindow.setFilled(true);
theWindow.setBackground(java.awt.Color.white);
// Create custom glass color
java.awt.Color glass = new java.awt.Color(153, 255, 255);
// Create window pane object
theWindowPane = new NscRectangle(83, 53, 19, 14);
// Set color
theWindowPane.setFilled(true);
theWindowPane.setBackground(glass);
// add window and pane
add(theWindow);
add(theWindowPane);
}
/*
* Overloaded constructor for the MyHouse class
* Creates the house objects and also inputs color variable
*
* @param x the x-coordinate for the object
* @param y the y-coordinate for the object
* @param colorVar the color for the walls of the house
*/
public MyHouse(int x, int y, java.awt.Color colorVar) {
// constructor, call super before anything else
super(x, y, 120, 90);
// build roof and set color, then add
theRoof = new NscUpTriangle(0, 0, 120, 40);
theRoof.setFilled(true);
theRoof.setBackground(new java.awt.Color(0x99, 0x33, 0x00));
add(theRoof);
// Create the walls and pass the 3rd constructor parameter for color conversion, then add
theWalls = new NscRectangle(10, 40, 100, 50);
theWalls.setFilled(true);
theWalls.setBackground(colorVar);
add(theWalls);
// Create door, set color and add
theDoor = new NscRectangle(48, 50, 24, 40);
theDoor.setFilled(true);
theDoor.setBackground(new java.awt.Color(0x99, 0x66, 0x33));
add(theDoor);
// Create the window object
theWindow = new NscRectangle(80, 50, 25, 20);
// Set color
theWindow.setFilled(true);
theWindow.setBackground(java.awt.Color.white);
// Create custom glass color
java.awt.Color glass = new java.awt.Color(153, 255, 255);
// Create window pane object
theWindowPane = new NscRectangle(83, 53, 19, 14);
theWindowPane.setFilled(true);
theWindowPane.setBackground(glass);
// add window and pane
add(theWindow);
add(theWindowPane);
}
/**
* Change the color of the house
*
* @param c The color for the walls of the house
*/
public void setColor(java.awt.Color c) {
theWalls.setBackground(c);
repaint();
}
/**
* Retrieve the color of the house
*
* @return The color of the walls of the house
*/
public java.awt.Color getColor() {
return theWalls.getBackground();
}
}
代码,它实例化3个MyScene
个对象并将它们放入场景中。
MyHouse
答案 0 :(得分:0)
您的问题是您手动指定每个组件的大小。当然,如果您手动指定它们,重新计算物体的大小和位置将是一个巨大的痛苦!当然,您必须使用类似ComponentListener
的内容再次指定它们。
然而......有更好的方法。 Swing有一个LayoutManager
概念,可以帮助你完成所有这些工作!
一些不同的布局管理器比其他布局管理器做得更好,就个人而言,我发现GroupLayout
在处理调整窗口大小的具体问题时最为强大。
tl; dr所以,请记住,不要使用.setSize()
指定尺寸,也不要使用setLocation()
指定尺寸,请使用LayoutManager
。