二王一马无人大满贯:关于 public Dimension getPreferredSize()

来源:百度文库 编辑:高校问答 时间:2024/04/16 12:11:32
public Dimension getPreferredSize(){
//TODO something else
return new Dimension(360,100);
}

这个函数在java程序里面是不是自动调用的?

import java.awt.Button;
import java.awt.*;
import java.awt.event.*;

public class CardLayoutDemo extends BaseDemo implements ActionListener,MouseListener{
Panel cards,buttons;
CardLayout cardLO;
Button card1,card2,card3;

/**see BaseDemo#init()*/

protected void init(){
card1=new Button("The Matrix 1");
card2=new Button("The Matrix 2");
card3=new Button("The Matrix 3");
add(card1);
add(card2);
add(card3);

cardLO=new CardLayout();
cards=new Panel();
cards.setLayout(cardLO); //set panel layout to card layout

Font font=new Font("Dialog",Font.PLAIN,24);

//add components to cards

cards.add(createCard("The Matrix 1 -- First Chapter",Color.BLUE,font),"The Matrix 1");
cards.add(createCard("The Matrix 2 -- RELOADED",Color.GREEN,font),"The Matrix 2");
cards.add(createCard("The Matrix 3 -- REVOLUTION",Color.RED,font),"The Matrix 3");

//add cards to main panel
add(cards);

//register to receive action events
card1.addActionListener(this);
card2.addActionListener(this);
card3.addActionListener(this);

//register mouse events
addMouseListener(this);
}

private Label createCard(String text,Color bgColor,Font font){
Label label=new Label(text);
label.setBackground(bgColor);
label.setFont(font);
return label;
}

/**see BaseDemo#getTitle()*/
public String getTitle(){
return "演示 CardLayoutDemo";

}

/**根据按下的按钮显示对应的card,see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)*/
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof Button){
String label=((Button)e.getSource()).getLabel();
cardLO.show(cards,label);
}
}

/**鼠标按下后切换到下一个card see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)*/
public void mousePressed(MouseEvent e){
cardLO.next(cards);
}

/**see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)*/
public void mouseClicked(MouseEvent e){
//TODO something else
}

/**see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)*/
public void mouseReleased(MouseEvent e){
//TODO something else
}

/**see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)*/
public void mouseEntered(MouseEvent e){

}

/**see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)*/
public void mouseExited(MouseEvent e){
//TODO something else
}

/**see java.awt.Component#getPreFerredSize()*/
public Dimension getPreferredSize(){
//TODO something else
return new Dimension(360,100);
}
}

不是,需要自己重载

云阿,你是干吗得,我不是学JAVA的