通过几个程序来说明:
程序一: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);
}
}
}