我们在学习swing的时候,可能总会有这样一种需求,从一个面板到另一个面板,甚至到另一个面板,然后再从另一面板回到这个面板,说白了就是界面切换吧!下面说一下我的小demo,希望给大家带一个领头作用。个人编写,转载注明出处。
package com.dzy.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainFrame extends JFrame implements ActionListener {
private MainFrame mainFrame;
private FirstPanel firstPanel;
private JButton go;
//下面定义为了切换面板方便
private SecondPanel secondPanel;
private ThirdPanel thirdPanel;
public void setSecondPanel(SecondPanel secondPanel) {
this.secondPanel = secondPanel;
}
public SecondPanel getSecondPanel() {
return secondPanel;
}
public FirstPanel getFirstPanel() {
return firstPanel;
}
public void setFirstPanel(FirstPanel firstPanel) {
this.firstPanel = firstPanel;
}
public ThirdPanel getThirdPanel() {
return thirdPanel;
}
public void setThirdPanel(ThirdPanel thirdPanel) {
this.thirdPanel = thirdPanel;
}
public MainFrame getMainFrame() {
return mainFrame;
}
public MainFrame() {
mainFrame=this;
firstPanel=new FirstPanel(mainFrame);
secondPanel=new SecondPanel(mainFrame);
firstPanel.setBounds(0, 0, 400, 800);
this.add(firstPanel);
firstPanel.setEnabled(true);
//firstPanel.setVisible(true);
this.add(secondPanel);
this.setTitle("多面板测试");
this.setSize(400, 800);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
/**
* @param args
*/
public static void main(String[] args) {
new MainFrame();
}
}
第一个面板
package com.dzy.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FirstPanel extends JPanel implements ActionListener{
private JButton back;
private JButton go;
private MainFrame mainFrame;
public FirstPanel(MainFrame mainFrame) {
this.mainFrame=mainFrame;
back = new JButton("返 回");
go = new JButton("前进");
this.setLayout(null);
this.add(back);
back.addActionListener(this);
back.setActionCommand("back");
go.addActionListener(this);
go.setActionCommand("go");
back.setBounds(30, 40,80, 40);
System.out.println("大家好,我倒第一个面板来了");
this.add(go);
go.setBounds(230, 40, 80, 40);
this.setEnabled(false);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("go")) {
System.out.println("已经将这个设置为了false");
mainFrame.getFirstPanel().setEnabled(false);
mainFrame.getFirstPanel().setVisible(false);
//System.out.println(mainFrame.getFirstPanel().isValid());
mainFrame.getSecondPanel().setVisible(true);
mainFrame.getSecondPanel().setEnabled(true);
// mainFrame.validate();
// mainFrame.repaint();
}else if (e.getActionCommand().equals("back")) {
}
}
}
第二面板
package com.dzy.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SecondPanel extends JPanel implements ActionListener {
private JButton back;
private JButton go;
private MainFrame mainFrame;
public SecondPanel(MainFrame mainFrame) {
this.mainFrame=mainFrame;
back = new JButton("second返回");
go = new JButton("second前进");
this.setLayout(null);
this.add(back);
back.addActionListener(this);
back.setActionCommand("back");
go.addActionListener(this);
go.setActionCommand("go");
back.setBounds(30, 40,100, 40);
System.out.println("大家好,我倒第二个面板这里来了");
this.add(go);
go.setBounds(230, 40, 100, 40);
this.setEnabled(false);
this.setVisible(false);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("go")) {
JOptionPane.showConfirmDialog(this, "大家好,我是第二个面板第二次点击引起的");
}if (e.getActionCommand().equals("back")) {
mainFrame.getFirstPanel().setEnabled(true);
mainFrame.getFirstPanel().setVisible(true);
//System.out.println(mainFrame.getFirstPanel().isValid());
mainFrame.getSecondPanel().setVisible(false);
mainFrame.getSecondPanel().setEnabled(false);
System.out.println("已经将第一个面板社会自微ture");
}
}
}
其实核心思想就是低耦合!如果有朋友需要源代码,可在这里下载
http://download.csdn.net/detail/u011410529/8588819