Java 如何按顺序显示重叠的JLabel

Java 如何按顺序显示重叠的JLabel,java,swing,japplet,Java,Swing,Japplet,这一定是一种电梯。如果单击按钮(在GUI的左侧或右侧),矩形必须以一定的延迟逐层移动。我的问题是我的GUI只是拒绝按顺序显示;相反,它会同时显示所有“楼层”:( //这是小程序 导入javax.swing.JApplet; @抑制警告(“串行”) 公共类applet扩展了JApplet{ 公共小程序(){ 添加(新电梯()); }} //这是主课 导入java.awt.*; 导入java.awt.event.ActionListener; 导入java.awt.event.ActionEvent

这一定是一种电梯。如果单击按钮(在GUI的左侧或右侧),矩形必须以一定的延迟逐层移动。我的问题是我的GUI只是拒绝按顺序显示;相反,它会同时显示所有“楼层”:(

//这是小程序
导入javax.swing.JApplet;
@抑制警告(“串行”)
公共类applet扩展了JApplet{
公共小程序(){
添加(新电梯());
}}
//这是主课
导入java.awt.*;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入java.awt.GridLayout;
导入javax.swing.*;
导入javax.swing.border.LineBorder;
@抑制警告(“串行”)
公共类电梯扩展JPanel实现ActionListener{
私有JButton[]外部按钮=新JButton[11];
私有JButton[]内部按钮=新JButton[13];
私有JLabel[]层=新JLabel[11];
私有JLabel o1=新JLabel();
私有JLabel o2=新JLabel();
private JPanel p1=新的JPanel();
private JPanel p2=新的JPanel();
private JPanel p3=新的JPanel();
私有JLabel[]labelBlueLeft=新JLabel[11];
私有JLabel[]labelBlueRight=新JLabel[11];
私有JLabel[]labelRedLeft=新JLabel[11];
私有JLabel[]labelRedRight=新JLabel[11];
private int firstOption=0,lastOption;
公共电梯(){
重新油漆();
p1.设置布局(新网格布局(11,1));
p2.设置布局(新网格布局(11,1));
p3.设置布局(新网格布局(15,1));

对于(inti=1;我不在
paint
方法中调用
repaint
,这将为您设置一个无限循环的绘制请求,这将消耗您的CPU周期您永远不会删除
labelRedLeft[i]
…我认为您需要改变思考问题的方式。创建一个“Floor”类(扩展自
JPanel
),向其添加一系列
JPanel
s或
JLabel
s,指示可用电梯的数量。然后,这将成为状态显示。创建电梯池,其中包含“虚拟”列表电梯,确定其当前楼层和目标楼层(如果有)。然后,您可以使用此池安排请求(找到最可能满足请求的电梯)并更新电梯的状态(通过某种计时器)@谢谢你的建议!在我改变计划后,我发现了另一个问题:如何从池中提取我的“虚拟电梯!”:)我尝试了GridBagLayout和GridBagConstraints,然后是JTabbedPane…最后CardLayout真的起作用了!我在图像中转换了我的电梯,现在我是:我有一个功能电梯!再次感谢!;)
//this is the applet
import javax.swing.JApplet;
@SuppressWarnings("serial")
public class applet extends JApplet{
    public applet(){
        add(new elevator());
    }}
//this is the main class
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.LineBorder;
@SuppressWarnings("serial")

public class elevator extends JPanel implements ActionListener{
    private JButton[] externalButtons=new JButton[11];
    private JButton[] internalButtons=new JButton[13];
    private JLabel[] floors=new JLabel[11];
    private JLabel o1=new JLabel();
    private JLabel o2=new JLabel();
    private JPanel p1=new JPanel();
    private JPanel p2=new JPanel();
    private JPanel p3=new JPanel();
    private JLabel[] labelBlueLeft=new JLabel[11];
    private JLabel[] labelBlueRight=new JLabel[11];
    private JLabel[] labelRedLeft=new JLabel[11];
    private JLabel[] labelRedRight=new JLabel[11];
    private int firstOption=0, lastOption;

    public elevator(){
        repaint();
        p1.setLayout(new GridLayout(11,1));
        p2.setLayout(new GridLayout(11,1));
        p3.setLayout(new GridLayout(15,1));

        for(int i=1; i<11;i++){
            externalButtons[i]=new JButton("Floor " + i);
            externalButtons[i].addActionListener(this);
        }
        externalButtons[0]=new JButton("Ground");
        externalButtons[0].addActionListener(this);
        for(int i=3; i<13; i++){
            internalButtons[i]=new JButton("Floor " + (i-2));
            internalButtons[i].addActionListener(this);
        }
        internalButtons[0]=new JButton("Alarm!");
        internalButtons[1]=new JButton("Stop");
        internalButtons[2]=new JButton("Ground");
        internalButtons[0].addActionListener(this);
        internalButtons[1].addActionListener(this);
        internalButtons[2].addActionListener(this);

        for(int i=0; i<11;i++){
            floors[i]=new JLabel("");
            floors[i].setLayout( new GridLayout());
            floors[i].setBorder(new LineBorder(new Color(150,150,150)));
            floors[i].setPreferredSize(new Dimension(200,25));
            if((i%2)==0) {
                floors[i].setBackground(Color.lightGray);
                floors[i].setOpaque(true);
            }
            labelBlueLeft[i]=new CustomLabel(25,0,35,25,0,0);
            labelBlueRight[i]=new CustomLabel(25,0,35,25,0,1);
            labelRedLeft[i]=new CustomLabel(25,0,35,25,1,0);
            labelRedRight[i]=new CustomLabel(25,0,35,25,1,1);
            }       
        floors[firstOption].add(labelBlueLeft[0]);

        for(int i=10; i>-1; i--){
            p1.add(externalButtons[i]);
            p2.add(floors[i]);
        }
        p3.add(o1); p3.add(o2);
    for(int i=12; i>-1; i--)
        p3.add(internalButtons[i]);
        add(p1, BorderLayout.WEST);
        add(p2, BorderLayout.CENTER);
        add(p3, BorderLayout.EAST);
    }
        //ACTION PERFORMED

        public void actionPerformed(ActionEvent e){
            for(int i=0; i<11; i++){
                if(e.getSource()==externalButtons[i]) lastOption=i;
            }
            for(int i=2; i<13; i++){
                if(e.getSource()==internalButtons[i]) lastOption=i-2;
            }               
            floors[firstOption].remove(labelBlueLeft[0]);



            if(firstOption<lastOption){ 
                    for(int i=firstOption; i<lastOption+1; i++){                             
                        floors[i].add(labelRedLeft[i]);
                        floors[i].validate();   
                        floors[i].repaint();
                    }
                }
            else if(firstOption>lastOption){
                    for(int i=firstOption; i>lastOption-1; i--){
                        floors[i].add(labelRedLeft[i]);
                        floors[i].revalidate();                 
                        floors[i].repaint();
                    }}
                firstOption=lastOption;                 
        }       
    }

//this is my CustomLabel class
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class CustomLabel extends JLabel{
    private int x=25, y=0, width=35, height=20; 
    public CustomLabel(int x1, int y1, int width1, int height1, int clr, int ind){
        if(ind==1){x1=150; y1=0;}
        if(clr==1)
            {setForeground(Color.red);}
        else
            {setForeground(Color.blue);}
        x=x1; y=y1;
        width=width1;
        height=height1;
    }   
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.fillRect(x, y, width, height);
        repaint();  
    }
}