我做了这个小测试:(剔除了所有与问题无关的东西)

package de.poller.StorrageBox;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass implements KeyListener {

    JFrame frame;
    JPanel panel1;
    JPanel panel2;

    public static void main(String[] args) {
        MainClass c=new MainClass();
    }

    public MainClass() {
        frame=new JFrame("test");
        frame.setSize(new Dimension(500, 500));
        frame.addKeyListener(this);

        panel1=new JPanel();
        JButton butt1=new JButton("test1111");
        panel1.add(butt1);

        panel2=new JPanel();
        JButton butt2=new JButton("test222");
        panel2.add(butt2);

        frame.add(panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.requestFocus();

    }


    @Override
    public void keyPressed(KeyEvent arg0) {

        if(arg0.getKeyChar()=='a') {
            activate1();
            val();
        }
        if(arg0.getKeyChar()=='b') {
            activate2();
            val();
        }
    }

    private void activate2() {
        frame.remove(panel1);
        frame.add(panel2);
    }

    private void activate1() {
        frame.remove(panel2);
        frame.add(panel1);
    }

    private void val() {
        frame.revalidate();
        frame.requestFocus();
    }


    public void keyReleased(KeyEvent arg0) {}


    public void keyTyped(KeyEvent arg0) {}

}

从面板1到2的第一次更改是可以的,但是如果我切换回1,则需要花费相当长的时间(比我希望用户等待的时间更长),并且这只是一个带有1个单按钮的面板。没有完整的GUI计划。

这里的问题是:重新验证(我认为是这个过程)太慢了还是我做错了什么?

最佳答案

多亏了ControllAltDel解决方案:

(CardLayout)

package de.poller.StorrageBox;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass implements KeyListener {

    JFrame frame;
    JPanel panel;

    JPanel panel1;
    JPanel panel2;

    public static void main(String[] args) {
        MainClass c=new MainClass();
    }

    public MainClass() {
        frame=new JFrame("test");
        frame.setSize(new Dimension(500, 500));
        frame.addKeyListener(this);

        panel=new JPanel(new CardLayout());
        frame.add(panel);

        panel1=new JPanel();
        JButton butt1=new JButton("test1111");
        panel1.add(butt1);

        panel2=new JPanel();
        JButton butt2=new JButton("test222");
        panel2.add(butt2);

        panel.add(panel1,"1");
        panel.add(panel2,"2");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.requestFocus();

    }


    public void keyPressed(KeyEvent arg0) {

        if(arg0.getKeyChar()=='a') {
            activate1();
        }
        if(arg0.getKeyChar()=='b') {
            activate2();
        }
    }

    private void activate2() {
        CardLayout cl=(CardLayout)(panel.getLayout());
        cl.show(panel, "2");
    }

    private void activate1() {
        CardLayout cl=(CardLayout)(panel.getLayout());
        cl.show(panel, "1");
    }

    public void keyReleased(KeyEvent arg0) {}

    public void keyTyped(KeyEvent arg0) {}

}

08-26 07:03