当前位置: 代码迷 >> java >> 是否可以将Swing对象设置为与其基础容器的大小成比例缩放?
  详细解决方案

是否可以将Swing对象设置为与其基础容器的大小成比例缩放?

热度:48   发布时间:2023-08-02 10:38:37.0

我可以使用我实例化的MyHouse对象之一,并以某种方式将其设置为与其基础NscComponent容器成比例地缩放吗?

当我使用从NscComponent继承的setSize方法时,它不会更改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();
  } 

}

这是MyScene代码,它实例化3个MyHouse对象并将它们放置到场景中。

// Allow short name access to java.awt.Color
import java.awt.Color;

/*
 * This is the shell for assignment: MyScene.
 * This program draws a simple scene using additional
 * classes that the student creates.
 * 
 * @author Dan Jinguji
 * @author Chris Wilson
 * @version Assignment 4: MyScene
 */
public class MyScene extends NscWindow {

   // instance variables
   private MyHouse house1;
   private MyHouse house2;
   private MyHouse house3;
   private MyTree tree1;
   private MyTree tree2;
   private MyTree tree3;

  /**
   * Constructor for objects of class MyScene
   */
  public MyScene() {
    // Specify the constructor for the superclass
    super(10, 10, 400, 300);
    // set characteristics for the object
    setTitle("My Scene");

    // Draw the sky
    getContentPane().setBackground(new Color(0x00, 0xCC, 0xFF));

    // Draw some grass
    NscRectangle grass = new NscRectangle(0, 100, 400, 200);
    grass.setBackground(new Color(0x00, 0x99, 0x00));
    grass.setFilled(true);
    add(grass);

    // instantiate new house objects
    house1 = new MyHouse(100, 50, Color.white);
    add(house1);    
    house2 = new MyHouse(200, 150);
    add(house2);    
    house3 = new MyHouse(0, 200, Color.orange);
    add(house3);    
    // request a rerendering of the window
    repaint();

    // instantiate new tree objects
    tree1 = new MyTree(35, 20, Color.white);
    add(tree1);
    tree2 = new MyTree(290, 50);
    add(tree2);
    tree3 = new MyTree(5, 60, Color.cyan);
    add(tree3);
    // request a rerender
    repaint();

  } 

  /**
   * Alter the scene, by changing color
   */
  public void change1() {
    // change the color of the houses and trees
    house1.setColor(Color.red);
    tree1.setColor(Color.black);    
    house2.setColor(Color.magenta);
    tree2.setColor(Color.orange);    
    house3.setColor(Color.black);
    tree3.setColor(Color.pink);
    // request a rerendering of the window
    repaint();
  }

   /**
    * Alter the scene, by changing location
    */
  public void change2() {
    // change the location of the houses and trees
    house1.setLocation(50, 125);    
    house2.setLocation(150, 25);    
    house3.setLocation(0, 150);
    tree1.setLocation(25, 10);
    tree2.setLocation(300, 20);
    tree3.setLocation(150, 90);
    // request a rerendering of the window
    repaint();
  }

   /**
    * Alter the scene, by changing size of objects and underlying containers   
    * 
    */
  public void change3() {
    house1.setSize(80, 60);
  } 



   /**
    * Alter the scene, restoring original settings
    */
  public void reset() {
    // reset the initial values for the house
    house1.setLocation(100, 50);
    house1.setColor(Color.white);
    house1.setSize(120, 90);    
    // reset the initial values for the house
    house2.setLocation(200, 150);
    house2.setColor(Color.blue);
    house2.setSize(120, 90);    
    // reset the initial values for the house
    house3.setLocation(0, 200);
    house3.setColor(Color.orange);
    house3.setSize(120, 90);
    // rest the initial values for the tree
    tree1.setLocation(35, 20);
    tree1.setColor(Color.white);
    tree1.setSize(60, 120);
    // rest the initial values for the tree
    tree2.setLocation(290, 50);
    tree2.setColor(Color.green);
    tree2.setSize(60, 120);
    // rest the initial values for the tree
    tree3.setLocation(5, 90);
    tree3.setColor(Color.cyan);
    tree3.setSize(60, 120);
    // request a rerendering of the window
    repaint();
  }

   /**
    * The application method, to test your code
    *
    * @param args The command-line arguments
    */
  public static void main(String[] args) {

    // declare a MyScene reference
    MyScene aScene;
    // instantiate MyScene
    aScene = new MyScene();
    // pause
    javax.swing.JOptionPane.showMessageDialog(null, "Press OK to continue");

    // test setColor
    aScene.change1();
    javax.swing.JOptionPane.showMessageDialog(null, "Press OK to continue");

    // test setLocation
    aScene.change2();
    javax.swing.JOptionPane.showMessageDialog(null, "Press OK to continue");

    // test setSize
    aScene.change3();
    javax.swing.JOptionPane.showMessageDialog(null, "Press OK to continue");    


    // reset initial values
    aScene.reset();
  }

}

您的问题是您要手动指定每个组件的大小。 当然,如果您手动指定物件的大小和位置,那将是巨大的痛苦! 当然,您必须使用ComponentListener类的方法再次指定它们。

但是...有更好的方法。 Swing具有的概念,可以为您完成所有这些工作!

就个人而言,某些不同的布局管理器的工作要比其他布局管理器做得更好,我个人认为, 在处理调整窗口大小的特定问题时最强大。

tl; dr因此,请记住,不要使用.setSize()指定尺寸,不要使用setLocation()指定位置,而要使用LayoutManager