用按钮框打地鼠
实现页面间的转换与交互
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class a2 extends JFrame
{
private static a2 single = null;
private a2()
{
setBounds(100, 100, 500, 500);
setContentPane(Panel_011.getInstance(this));
}
public static a2 getInstance()// 对外只暴露getInstance()方法,这是获取唯一实例方法。
{
if (single == null)
single = new a2();
return single;
}
public static void main(String args[]) {
a2 singleton_ = new a2();// 实例化唯一窗口与
singleton_.setTitle("单例模式窗口");
singleton_.setVisible(true);
singleton_.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
singleton_.setResizable(false);
}
}
class Panel_011 extends JPanel implements ActionListener
{
private JButton but_01 = new JButton("界面1");;
private JPanel jPanel;
private JFrame JF;
private Panel_011(final JFrame jFrame)
{
this.setLayout(null);
JF = jFrame;
but_01.addActionListener(this);
but_01.setBounds(100, 100, 100, 100);
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(0, 0, 500, 500);
jPanel.add(but_01);
add(jPanel);
}
public void actionPerformed(ActionEvent e) //事件发生时的处理操作
{
JButton btn = (JButton)e.getSource(); //创建按钮对象并获取它的资源
if (e.getSource() == but_01)
{
JF.setContentPane(Panel_022.getInstance(JF));
JF.validate();// 使面板生效,刷新
}
}
private static Panel_011 panel = null;
public static Panel_011 getInstance(JFrame jFrame)
{
panel = new Panel_011(jFrame);
return panel;
}
}
class Panel_022 extends JPanel implements ActionListener
{
private JButton but_02;
private JPanel jPanel;
private JFrame JF;
private Panel_022(final JFrame jFrame)
{
this.setLayout(null);
but_02 = new JButton("返回");
JF = jFrame;
but_02.addActionListener(this);
but_02.setBounds(200, 200, 100, 100);
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(0, 0, 500, 500);
jPanel.add(but_02);
add(jPanel);
}
public void actionPerformed(ActionEvent e) //事件发生时的处理操作
{
JButton btn = (JButton)e.getSource(); //创建按钮对象并获取它的资源
if (e.getSource() == but_02)
{
JF.setContentPane(Panel_011.getInstance(JF));
JF.validate();// 使面板生效,刷新
}
}
private static Panel_022 panel = null;
public static Panel_022 getInstance(JFrame jFrame)
{
panel = new Panel_022(jFrame);
return panel;
}
}