当前位置: 技术问答>java相关
帮忙改一下小代码
来源: 互联网 发布时间:2015-08-12
本文导语: 代码如下: public class op { public static void main(String ars[]) { win obj=new win(); } } class win { one oneobj; two twoobj; JFrame fra; JPanel pan; JButton but1; JButton but2; public win...
代码如下:
public class op
{
public static void main(String ars[])
{
win obj=new win();
}
}
class win
{
one oneobj;
two twoobj;
JFrame fra;
JPanel pan;
JButton but1;
JButton but2;
public win()
{
oneobj=new one();
twoobj=new two();
oneButton onechick=new oneButton();
twoButton twochick=new twoButton();
fra=new JFrame();
pan=new JPanel();
but1=new JButton("one");
but2=new JButton("two");
but1.addActionListener(onechick);
but2.addActionListener(twochick);
pan.add(but1);
pan.add(but2);
fra.getContentPane().add(pan);
fra.setSize(100,100);
fra.show();
}
class oneButton implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
oneobj.fra.show();
}
}
class twoButton implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
twoobj.fra.show();
}
}
}
class one
{
JFrame fra;
JPanel pan;
JLabel lab;
JButton but;
JTextField text;
public one()
{
okButton okchick=new okButton();
fra=new JFrame();
pan=new JPanel();
lab=new JLabel("ad");
text=new JTextField(6);
but=new JButton("ok");
but.addActionListener(okchick);
pan.add(lab);
pan.add(text);
pan.add(but);
fra.getContentPane().add(pan);
fra.setSize(100,100);
}
class okButton implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
...........
}
}
}
class two extends one
{
public two()
{
canelButton canelchick=new canelButton();
lab.setText("fg");
but.setText("canel");
but.addActionListener(canelchick);
}
class canelButton implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
.............
}
}
}
two继承one,但事件侦听不继承,如何改?
public class op
{
public static void main(String ars[])
{
win obj=new win();
}
}
class win
{
one oneobj;
two twoobj;
JFrame fra;
JPanel pan;
JButton but1;
JButton but2;
public win()
{
oneobj=new one();
twoobj=new two();
oneButton onechick=new oneButton();
twoButton twochick=new twoButton();
fra=new JFrame();
pan=new JPanel();
but1=new JButton("one");
but2=new JButton("two");
but1.addActionListener(onechick);
but2.addActionListener(twochick);
pan.add(but1);
pan.add(but2);
fra.getContentPane().add(pan);
fra.setSize(100,100);
fra.show();
}
class oneButton implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
oneobj.fra.show();
}
}
class twoButton implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
twoobj.fra.show();
}
}
}
class one
{
JFrame fra;
JPanel pan;
JLabel lab;
JButton but;
JTextField text;
public one()
{
okButton okchick=new okButton();
fra=new JFrame();
pan=new JPanel();
lab=new JLabel("ad");
text=new JTextField(6);
but=new JButton("ok");
but.addActionListener(okchick);
pan.add(lab);
pan.add(text);
pan.add(but);
fra.getContentPane().add(pan);
fra.setSize(100,100);
}
class okButton implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
...........
}
}
}
class two extends one
{
public two()
{
canelButton canelchick=new canelButton();
lab.setText("fg");
but.setText("canel");
but.addActionListener(canelchick);
}
class canelButton implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
.............
}
}
}
two继承one,但事件侦听不继承,如何改?
|
如果子类中的一个方法与它的超类中的方法名字相同,参数类型也相同,则子类中的方法就会覆盖超类中的方法了。参数类型不同的话,就是重载。
所以你只要在two中定义它自己的方法覆盖掉one中的方法就可以了。
所以你只要在two中定义它自己的方法覆盖掉one中的方法就可以了。