为什么我运行了CheckBoDemo之后,并没有调用父类的构造方法了,显示的图形界面依然只有CheckBoDemo中创建的图形界面[code=Java][/code]/*
* 实现在一个面板上写入字符,并且具备居中显示,左右上下移动的能力
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.Graphics;
public class MessagePanel extends JPanel{
private String message="Welcome to java";
private int xCoordinate=20;
private int yCoordinate=20;
private boolean centered;
private int interval = 10;
public void setBackground(Color bg)
{
super.setBackground(bg);
}
public MessagePanel(){
}
public MessagePanel(String message){
this.message=message;
}
public void setMessage(String message){
this.message=message;
repaint();
}
public int getXCoordinate(){
return xCoordinate;
}
public void setXCoordinate(int xCoordinate){
this.xCoordinate=xCoordinate;
repaint();
}
public int getYCoordinate(){
return yCoordinate;
}
public void setYCoordinate(int yCoordinate){
this.yCoordinate=yCoordinate;
repaint();
}
public boolean getCentered(){
return centered;
}
public void setCentered(boolean centered){
this.centered=true;
repaint();
}
public int getInterval(){
return interval;
}
public void setInterval(int interval){
this.interval=interval;
repaint();
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
if(centered){
FontMetrics fm=g.getFontMetrics();
int stringWidth=fm.stringWidth(message);
int stringAscent=fm.getAscent();
xCoordinate=getWidth()/2-stringWidth;
yCoordinate=getHeight()/2-stringAscent;
}
g.drawString(message,xCoordinate,yCoordinate);
}
public void moveLeft(int interval){
xCoordinate-=interval;
this.getParent().repaint();
repaint();
}
public void moveRight(int interval){
xCoordinate+=interval;
this.getParent().repaint();
//repaint();
}
public void moveUp(int interval){
yCoordinate-=interval;
repaint();
}
public void moveDown(int interval){
yCoordinate+=interval;
repaint();
}
public Dimension getPreferedSize(){
return new Dimension(200,30);
}
}
[code=Java][/code]/*
* 实现一个面板和两个按钮
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonDemo extends JFrame implements ActionListener{
protected MessagePanel messagePanel=new MessagePanel("Welcome to java");
private JButton jbtLeft=new JButton("Left");
private JButton jbtRight=new JButton("Right");
public ButtonDemo(){
messagePanel.setBackground(Color.blue);
messagePanel.setForeground(Color.red);
//messagePanel.setCentered(true);
JPanel p=new JPanel();
p.setBackground(Color.blue);
p.setLayout(new FlowLayout());
p.add(jbtLeft);
p.add(jbtRight);
ImageIcon left=new ImageIcon("123/lfw.gif");
ImageIcon right=new ImageIcon("123/szl.gif");
jbtLeft.setIcon(left);
jbtRight.setIcon(right);
jbtLeft.setMnemonic('l');
jbtRight.setMnemonic('r');
jbtLeft.setToolTipText("Move message to Left");
jbtRight.setToolTipText("Move message to Right");
getContentPane().setLayout(new BorderLayout());
getContentPane().add(messagePanel,BorderLayout.CENTER);
getContentPane().add(p,BorderLayout.SOUTH);
jbtLeft.addActionListener(this);
jbtRight.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbtLeft)
messagePanel.moveLeft(20);