有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

//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();  
    }
}

共 (0) 个答案