在jframe上的jpanel之间切换

toe95027  于 2021-06-26  发布在  Java
关注(0)|答案(2)|浏览(484)

目前,仅出于测试目的,每次单击jframe上的jpanel时,我都想更改它。我在3个面板/页面中循环,每个面板/页面都有其独特的背景色。
目前,它将改变,但只有当它涉及到第一个面板,否则显示默认的灰色。
代码:

@Override
    public void mouseClicked(MouseEvent e) {
        mainFrame.changePage();
    }

public void changePage() {
    if(currentPage != null) {
        this.remove(currentPage);
    }
    currentPage = pages[pageNumber];
    this.add(currentPage);
    currentPage.setPage();
    super.repaint();
    pageNumber++;
    if(pageNumber > 2) {
        pageNumber = 0;
    }
}
deyfvvtc

deyfvvtc1#

除了@camickr的评论之外 CardLayout ,你需要 revalidate . 更改:

super.repaint();

revalidate();
v09wglhw

v09wglhw2#

编辑:由于@camickr提到我下面的代码不符合最佳实践,请参考以下关于如何使用cardlayout的链接。
如何使用cardlayout-java教程
关联源
只是为了测试
重点放在上面,下面是一些示例代码。这不是生产级别,只是一个poc,不需要@camickr提到的cardlayout。

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class FirstSwingExample {  

    public static void main(String[] args) {  
        Swinger s = new Swinger();
    }

}

class Swinger implements ActionListener {

    int currFrame = 1;
    JFrame f;
    JLabel lblPanelOne, lblPanelTwo, lblPanelThree;
    JPanel panelOne, panelTwo, panelThree;
    JButton b;

    public Swinger() {
        setup();
    }

    private void setup() {

        f=new JFrame();

        panelOne = new JPanel();
        lblPanelOne = new JLabel("Panel One");
        panelOne.setBounds(0,0,100, 40);
        panelOne.setBackground(Color.BLACK);
        panelOne.add(lblPanelOne);

        panelTwo = new JPanel();
        lblPanelTwo = new JLabel("Panel Two");
        panelTwo.setBounds(0,0,100, 40);
        panelTwo.setBackground(Color.BLUE);
        panelTwo.add(lblPanelTwo);

        panelThree = new JPanel();
        lblPanelThree = new JLabel("Panel Three");
        panelThree.setBounds(0,0,100, 40);
        panelThree.setBackground(Color.GREEN);
        panelThree.add(lblPanelThree);

        b=new JButton("Panel Two");
        b.setBounds(130,100,100, 40);
        b.setActionCommand("click");
        b.addActionListener(this);

        panelOne.setVisible(true);
        f.add(panelOne);
        f.add(b);

        f.setSize(400,500);  
        f.setLayout(null);  
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        if(e.getActionCommand().equals("click")) {
            switch(currFrame) {
                case 1:
                    panelOne.setVisible(false);
                    f.remove(panelOne);
                    panelTwo.setVisible(true);
                    f.add(panelTwo);
                    b.setText("Panel Three");
                    currFrame++;
                    break;

                case 2:
                    panelTwo.setVisible(false);
                    f.remove(panelTwo);
                    f.add(panelThree);
                    panelThree.setVisible(true);
                    b.setText("Panel One");
                    currFrame++;
                    break;

                default:
                    panelThree.setVisible(false);
                    f.remove(panelThree);
                    f.add(panelOne);
                    panelOne.setVisible(true);
                    b.setText("Panel Two");
                    currFrame = 1;
                    break;
            }
        }
    }
}

相关问题