setPreferredSize()、getPreferredSize()与pack()的配合使用以及setSize()方法

通过几个程序来说明:

程序一:corejava程序清单7-6,显示图像,通过getPreferredSize()及pack()配合使用,先在ImageComponent类中覆盖getPreferredSize()方法,然后在ImageFrame类的构造器中通过下述部分增加ImageComponent类组件并调用pack()方法自动调整窗口大小

public ImageFrame()
  {
 add(new ImageComponent());
 pack();
  
  }

完整程序如下:

package image;
import java.awt.*;
import javax.swing.*;


public class ImageTest
{
   public static void main(String[] args)
  {
     EventQueue.invokeLater(new Runnable()
  {
   public void run()
   {
    JFrame frame=new ImageFrame();
    frame.setTitle("ImageTest");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   
   }
  });
 
  }
}


class ImageFrame extends JFrame
{
  public ImageFrame()
  {
 add(new ImageComponent());
 pack();
  
  }
}

class ImageComponent extends JComponent
{
 private static final int DEFAULT_WIDTH=800;
 private static final int DEFAULT_HEIGHT=500;
 private Image image;
 
 public ImageComponent()
 { 
  image=new ImageIcon("blue-ball.gif").getImage();
 }
 
 public void paintComponent(Graphics g)
 {
     if(image==null)return;
  int imageWidth=image.getWidth(this);
  int imageHeight=image.getHeight(this);
  g.drawImage(image,0,0,null);
  
  for(int i=0;i*imageWidth<=getWidth();i++)
   for(int j=0;j*imageHeight<=getHeight();j++)
    if(i+j>0)
     g.copyArea(0,0,imageWidth,imageHeight,i*imageWidth,j*imageHeight);
  
 }
 
 public Dimension getPreferredSize()
 {
  return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
 }

}

 

程序二:COREJAVA中的程序清单8-1,包含ButtonTest.java和ButtonFrame.java两个文件,做YELLOW、BLUE、RED三个按扭,点击相应按扭,即将窗口背景色改变成对应颜色。这个程序中如果用下面标红的方法,无法达到自动适配窗口大小的目的。

public Dimension getPreferredSize()
 {
  return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
 }

pack();

 

 

但是有2个方法均可调整好适配的窗口:

方法1: setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

方法2:

  /**the following function is equal with setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  *
  * buttonPanel.setPreferredSize(new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT));
  * pack();
  */

 

ButtonTest.java文件:

package button;
import java.awt.*;
import javax.swing.*;


public class ButtonTest
{
   public static void main(String[] args)
  {
     EventQueue.invokeLater(new Runnable()
  {
   public void run()
   {
    JFrame frame=new ButtonFrame();
    frame.setTitle("ButtonTest");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   
   }
  });
 
  }
}

ButtonFrame.java文件:

package button;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonFrame  extends JFrame
{
 private static final int DEFAULT_WIDTH=400;
 private static final int DEFAULT_HEIGHT=200;
 private JPanel buttonPanel;
 
 public ButtonFrame()
 {
  setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  
  JButton yellButton=new JButton("Yellow");
  JButton blueButton=new JButton("Blue");
  JButton redButton=new JButton("Red");
  
  buttonPanel=new JPanel();
        
  buttonPanel.add(yellButton);
  buttonPanel.add(blueButton);
  buttonPanel.add(redButton);
  
  add(buttonPanel);
  
  ColorAction yellowAction=new ColorAction(Color.YELLOW);
  ColorAction blueAction=new ColorAction(Color.BLUE);
  ColorAction redAction=new ColorAction(Color.RED);
  
  yellButton.addActionListener(yellowAction);
  blueButton.addActionListener(blueAction);
  redButton.addActionListener(redAction);
  
  /**the following function is equal with setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  *
  * buttonPanel.setPreferredSize(new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT));
  * pack();
  */

  
   
    
 }
  

 
 private class ColorAction implements ActionListener
 {
  private Color backgroundColor;
  
  public ColorAction(Color backgroundColor)
  {
   this.backgroundColor=backgroundColor;
  }
  
  public void actionPerformed(ActionEvent event)
  {
   buttonPanel.setBackground(backgroundColor);
  }
 }
 

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值