首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

纠结呀

2012-09-18 来源:读书人网 【读书人网(Reader8.cn):综合教育门户网站】
纠结呀,求助为什么我运行了CheckBoDemo之后,并没有调用父类的构造方法了,显示的图形界面依然只有CheckBoDe

纠结呀,求助
为什么我运行了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);


else if(e.getSource()==jbtRight)
messagePanel.moveRight(20);
}

}
[code=Java][/code]/*
 * 带有主函数,并且实现三个复选框,进而构成整个图形界面
 */
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
public class CheckBoDemo extends ButtonDemo{
private JCheckBox jchkCentered=new JCheckBox("Centered");
private JCheckBox jchkBold=new JCheckBox("Bold");
private JCheckBox jchkItalic=new JCheckBox("Italic");
public CheckBoDemo(){
super();
setVisible(true);
jchkCentered.setMnemonic('c');
jchkBold.setMnemonic('b');
jchkItalic.setMnemonic('s');
JPanel jchkPanel=new JPanel();
jchkPanel.setLayout(new GridLayout(3,0));
jchkPanel.add(jchkCentered);
jchkPanel.add(jchkBold);
jchkPanel.add(jchkItalic);
jchkItalic.addActionListener(this);
jchkBold.addActionListener(this);
jchkCentered.addActionListener(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jchkPanel,BorderLayout.EAST);
jchkCentered.addActionListener(this);
jchkBold.addActionListener(this);
jchkItalic.addActionListener(this);

}
public void actionPerformed(ActionEvent e){
super.actionPerformed(e);
if(e.getSource()==jchkCentered){
messagePanel.setCentered(jchkCentered.isSelected());
repaint();
}
else if(e.getSource()==jchkBold||e.getSource()==jchkItalic){
int fontStyle=Font.PLAIN;
fontStyle+=(jchkBold.isSelected()?Font.BOLD:0);
fontStyle+=(jchkItalic.isSelected()?Font.ITALIC:0);
Font font=messagePanel.getFont();
messagePanel.setFont(new Font(font.getName(),fontStyle,font.getSize()));
}
}
public static void main(String[] args){
CheckBoDemo frame=new CheckBoDemo();
frame.setTitle("CheckBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,300);
//frame.setVisible(true);
}

}


[解决办法]
放到 [\code=\Java] [\/code]里面去,我用\是怕这个格式跳出来。或者就是等级不够吧,等级低不知道可不可以发。