Swing布局问题,多个Jpanel切换

我们在学习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

  • 11
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值