在java的图形界面中,我又要继承JFrame类,又要继承事件中的适配器类,要怎样实现
1个回答
展开全部
//java只支持单继承,但你有如下几种选择:
//1、定义另一个类如MyWindowAdapter1继承自WindowAdapter
//2、定义一个嵌套内部类(静态或非静态)如(MyWindowAdapter2或MyWindowAdapter4)继承自WindowAdapter
//3、用匿名内部类
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyWindowAdapter1 extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public class MyJFrame extends JFrame
{
static class MyWindowAdapter2 extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class MyWindowAdapter4 extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
MyJFrame(String t)
{
super(t);
setSize(400,200);
setLocation(x,y);
y+=200;
setVisible(true);
}
public static void main(String[] args)
{
MyJFrame f1=new MyJFrame("定义另一个类继承自WindowAdapter");
f1.addWindowListener(new MyWindowAdapter1());
MyJFrame f2=new MyJFrame("定义一个嵌套静态内部类继承自WindowAdapter");
//这样添加必须将MyWindowAdapter2定义为静态的
f2.addWindowListener(new MyWindowAdapter2());
MyJFrame f3=new MyJFrame("定义一个非静态嵌套内部类继承自WindowAdapter");
//MyWindowAdapter4不是静态的内部类,必须这样使用
f3.addWindowListener(f3.new MyWindowAdapter4());
MyJFrame f4=new MyJFrame("用匿名内部类");
f4.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
static final int x=400;
static int y=50;
}
//XiTi9_3先前的回答是错误的,但你已经采纳了,我无法在原处更正了,正确的如下:
/*public class XiTi9_3 extends JFrame implements ActionListener
{
static JTextField[] text={new JTextField(10),new JTextField(10)};
static JButton button=new JButton("??");
XiTi9_3(String t)
{
super(t);
}
public static void main(String[] args)
{
XiTi9_3 win=new XiTi9_3("CopyText");
win.setBounds(100,100,400,80);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
win.setLayout(new FlowLayout());
win.add(text[0]);
win.add(button);
win.add(text[1]);
button.addActionListener(win);
}
public void actionPerformed(ActionEvent e)
{
text[1].setText(text[0].getText());
}
}*/
//1、定义另一个类如MyWindowAdapter1继承自WindowAdapter
//2、定义一个嵌套内部类(静态或非静态)如(MyWindowAdapter2或MyWindowAdapter4)继承自WindowAdapter
//3、用匿名内部类
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyWindowAdapter1 extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public class MyJFrame extends JFrame
{
static class MyWindowAdapter2 extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class MyWindowAdapter4 extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
MyJFrame(String t)
{
super(t);
setSize(400,200);
setLocation(x,y);
y+=200;
setVisible(true);
}
public static void main(String[] args)
{
MyJFrame f1=new MyJFrame("定义另一个类继承自WindowAdapter");
f1.addWindowListener(new MyWindowAdapter1());
MyJFrame f2=new MyJFrame("定义一个嵌套静态内部类继承自WindowAdapter");
//这样添加必须将MyWindowAdapter2定义为静态的
f2.addWindowListener(new MyWindowAdapter2());
MyJFrame f3=new MyJFrame("定义一个非静态嵌套内部类继承自WindowAdapter");
//MyWindowAdapter4不是静态的内部类,必须这样使用
f3.addWindowListener(f3.new MyWindowAdapter4());
MyJFrame f4=new MyJFrame("用匿名内部类");
f4.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
static final int x=400;
static int y=50;
}
//XiTi9_3先前的回答是错误的,但你已经采纳了,我无法在原处更正了,正确的如下:
/*public class XiTi9_3 extends JFrame implements ActionListener
{
static JTextField[] text={new JTextField(10),new JTextField(10)};
static JButton button=new JButton("??");
XiTi9_3(String t)
{
super(t);
}
public static void main(String[] args)
{
XiTi9_3 win=new XiTi9_3("CopyText");
win.setBounds(100,100,400,80);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
win.setLayout(new FlowLayout());
win.add(text[0]);
win.add(button);
win.add(text[1]);
button.addActionListener(win);
}
public void actionPerformed(ActionEvent e)
{
text[1].setText(text[0].getText());
}
}*/
追问
谢谢,我知道了,我用内部类
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询