簡體   English   中英

JAVA JPanel滑出並同時滑入

[英]JAVA JPanel sliding out and sliding in at the same time

我在移動JPanels時遇到問題。

我有一個窗口400x600和一個JPanel(紅色)100x100

public class RulettModel {
    private int num;

    public void Slide() {
        num = num + 1;
        if(num > 600) {
            num = -100;
        }
    }

    public int getNum() {
        return num;
    }
}

class RulettListener implements ActionListener{
    Timer tm = new Timer(1, this);

   @Override
    public void actionPerformed(ActionEvent e) {
        tm.start();
        int y = 0;

        theModel.Slide();

        theView.setNewLocationOfPiros(theModel.getNum(), y);         
    }
}

void setNewLocationOfJpanel(int x, int y) {
    JPanel1.setLocation(x,y);
 }

此代碼是我的代碼的一部分,在這里需要使用,它可以正常工作,我的JPanel從窗口滑出到右側,當它超出窗口寬度時,它從-100開始從左側返回

我遇到的問題是:我希望JPanel從左側進入,然后在右側完全退出。

因此,如果小組的一半已經熄滅,那么那一半應該已經出現在左側,因此我的小組一次應該在兩個位置,一半在這里,另一半有可能嗎?

其他解決問題的技巧。

我猜您需要2個JPanels。 那我有

public class RulettModel {
    private int num1, num2;

    public void Slide() {
        num1 = num1 + 1;
        if(num1 > 600) {
            num2 = 500-num1;
        }
    }

    public int getNum1() {
        return num1;
    }

    public int getNum2() {
        return num2;
    }
}

並具有setNewLocOfJPanel1(int, int)setNewLocOfJPanel2(int, int)方法

在對下面的代碼進行編碼之前,我使用Microsoft Paint制作了7個不同顏色的矩形,分別為60像素寬和90像素高。 在您看到jf.setSize(186,120)我只能通過反復試驗得出這些值。 我剛剛猜到的timerDelay將是一個適當的值。 關於原因,所有其他值均應顯而易見。

所有這些的關鍵是,在TimerActionListener中,我只是簡單地獲取每個tile [i]的getLocation(),將x坐標加5,並在達到300時將其重置為-120,然后將setLocation(int,int)再次。

package SlidingTiles;

import ...;

public class Main 
{
    private static int timerDelay = 100;
    private static Timer t = new Timer(timerDelay, new TimerActionListener());

    private static JFrame jf = new JFrame();
    private static JPanel jp = new JPanel();
    private static String[] colors = { "blue", "brown", "green", "grey", "purple", "red", "yellow" };
    private static JLabel[] tiles = new JLabel[7];

    public static void main(String[] args) 
    {


        for(int i=0; i<7; ++i)
        {
            tiles[i] = new JLabel(new ImageIcon("tiles/"+colors[i]+".jpg"));
            tiles[i].setBounds(-120+60*i,0,60,90);
            jf.add(tiles[i]);
        }

        jf.add(jp);
        jf.setSize(186,120);
        jf.setResizable(false);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.start();
    }

    public static class TimerActionListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            for(int i=0; i<7; ++i)
            {
                Point p = tiles[i].getLocation();
                int newX = (int) p.getX()+5;
                if(newX == 300)
                    newX = -120;

                tiles[i].setLocation(newX, 0);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM