下面是一个程序,用兰色标记的地方看不能懂(根说关于超类的),我把它贴出来哪位帮忙讲解一下。谢谢拉
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyFrame extends JFrame
implements ActionListener
{
public MyFrame()
{
setTitle("Swing示例程序9");
setSize(300, 200);
setLocation(100,100);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
//增加复选框
JPanel p = new JPanel();
ckDog = addCheckBox(p, "Dog");
ckCat = addCheckBox(p, "Cat");
getContentPane().add(p, "South");
panel = new MyPanel();
getContentPane().add(panel, "Center");
}
//增加复选框方法
public JCheckBox addCheckBox(JPanel p, String name)
{
JCheckBox c = new JCheckBox(name);
c.addActionListener(this);
p.add(c);
return c;
}
public void actionPerformed(ActionEvent evt)
{
if (ckDog.isSelected())
{
panel.bDog = true;
}
else
{
panel.bDog = false;
}
if (ckCat.isSelected())
{
panel.bCat = true;
}
else
{
panel.bCat = false;
}
//调用MyPanel.paintComponent(...),绘制MyPanel上的内容
panel.repaint();
}
private MyPanel panel;
private JCheckBox ckDog;
private JCheckBox ckCat;
}
class MyPanel extends JPanel
{
public MyPanel()
{
bDog = false;
bCat = false;
}
//绘制信息
public void paintComponent(Graphics g)
{
super.paintComponent(g);//?这个语法是什么意思?
if (bDog)
g.drawString("I love the dog",20, 30);
if (bCat)
g.drawString("I love the cat",20, 60);
}
public boolean bDog;
public boolean bCat;
}
public class Swing13_9
{ public static void main(String[] args)
{ JFrame frame = new MyFrame();
frame.setVisible(true);
}
}
----------------解决方案--------------------------------------------------------
调用超类的方法
很多东西超类可以帮你完在
----------------解决方案--------------------------------------------------------
看一下api文档,JPanel类的这个方法中有很详细的说明。
----------------解决方案--------------------------------------------------------
super代表父类
super.method()就是调用父类的方法
----------------解决方案--------------------------------------------------------
问题搞懂了 我觉得大家说的都很好~!对我都有启发。谢谢拉
----------------解决方案--------------------------------------------------------