简体   繁体   English

使用平滑外观更改 JButton 文本和背景颜色

[英]Change JButton Text and BackGround Color with Smooth Look

I am learning Swing from past week and I want desing some game like puzzle, 2048 for fun i have written puzzle sucessfully but when writting 2048 i encountered some problem, see below for sample code that is not orginal but it states the problem.我从上周开始学习 Swing,我想设计一些像拼图这样的游戏,为了好玩,我已经成功地编写了拼图,但是在编写 2048 时我遇到了一些问题,请参阅下面的示例代码,这些代码不是原始的,但它说明了问题。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

@SuppressWarnings("serial")
class Panel extends JPanel implements ActionListener
{
    JButton[] button = new JButton[4];

    boolean flag = true;

    public Panel()
    {
        this.setLayout(new GridLayout(1,4));

        for(int i = 0; i < 4; ++i)
        {
            button[i] = new JButton("");
            button[i].setBackground(Color.WHITE);
            button[i].addActionListener(this);
            button[i].setPreferredSize(new Dimension(100,100));
            add(button[i]);
        }

        button[0].setText("2");
        button[0].setBackground(Color.GREEN);
    }

    @Override
    public void actionPerformed(ActionEvent Ae)
    {
        if(flag)
        {
            flag = false;

            button[0].setText("");
            button[0].setBackground(Color.WHITE);
            //for(long i = 0; i < 100000000L; ++i);
            button[1].setText("2");
            button[1].setBackground(Color.GREEN);
            //for(long i = 0; i < 100000000L; ++i);
            button[1].setText("");
            button[1].setBackground(Color.WHITE);
            //for(long i = 0; i < 100000000L; ++i);
            button[2].setText("2");
            button[2].setBackground(Color.GREEN);
            //for(long i = 0; i < 100000000L; ++i);
            button[2].setText("");
            button[2].setBackground(Color.WHITE);
            //for(long i = 0; i < 100000000L; ++i);
            button[3].setText("2");
            button[3].setBackground(Color.GREEN);
        }
        else
        {
            flag = true;

            button[0].setText("2");
            button[0].setBackground(Color.GREEN);
            button[1].setText("");
            button[1].setBackground(Color.WHITE);
            button[2].setText("");
            button[2].setBackground(Color.WHITE);
            button[3].setText("");
            button[3].setBackground(Color.WHITE); 
        }
    }
}

public class Grid
{
    public Grid()
    {
        // I cannot ascess JFrame reference in real code too but it is not 
        // must, it would be better that i can do the thing without 
        // JFrame reference.
        JFrame jFrame = new JFrame("Grid");

        jFrame.add(new Panel());
        jFrame.pack();
        jFrame.setResizable(false);
        jFrame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> new Grid());
    }
}

In above code the change of button text and color is not smooth that is first button suddenly becomes white and last button suddnley becomes Green, so i have tested that second and third button is actullay changing or not by the delay loop but still second and third button is not changing but it waits some time to change first button and last button.在上面的代码中,按钮文本和颜色的变化不平滑,第一个按钮突然变成白色,最后一个按钮突然变成绿色,所以我测试了第二个和第三个按钮是否通过延迟循环改变了实际,但仍然是第二个和第三个按钮没有改变,但它等待一段时间来改变第一个按钮和最后一个按钮。 I Hope that the problem is understandable.我希望这个问题是可以理解的。

Note: In real i use keybord by keybinding not button click i used it here for state the problem.注意:实际上,我通过键绑定而不是按钮单击来使用键盘,我在这里使用它来说明问题。

How To switch text and color of button with smooth look like 2048 game ?如何像2048 游戏一样平滑切换按钮的文字和颜色?

Thanks.谢谢。

Hopefully I understood your problem.希望我理解你的问题。 What you want is animation.你想要的是动画。 To do this you need to use atimer .为此,您需要使用计时器

The following code demonstrates.下面的代码演示。 Note that I changed the class name to MyPanel so as not to clash with class java.awt.Panel .请注意,我将类名更改为MyPanel以免与类java.awt.Panel发生冲突。

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

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

public class MyPanel extends JPanel implements ActionListener {
    JButton[] button = new JButton[4];
    boolean flag = true;
    Timer timer;
    int currentIndex;
    int delta;

    public MyPanel() {
        this.setLayout(new GridLayout(1, 4));
        for (int i = 0; i < 4; ++i) {
            button[i] = new JButton("");
            button[i].setBackground(Color.WHITE);
            button[i].addActionListener(this);
            button[i].setPreferredSize(new Dimension(100, 100));
            add(button[i]);
        }
        button[0].setText("2");
        button[0].setBackground(Color.GREEN);
        timer = new Timer(500, event -> {
            button[currentIndex].setText("");
            button[currentIndex].setBackground(Color.WHITE);
            currentIndex += delta;
            button[currentIndex].setText("2");
            button[currentIndex].setBackground(Color.GREEN);
            if (currentIndex == 0  ||  currentIndex == 3) {
                timer.stop();
            }
        });
        timer.setInitialDelay(0);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (flag) {
            flag = false;
            delta = 1;
        }
        else {
            flag = true;
            delta = -1;
        }
        timer.start();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame jFrame = new JFrame("Grid");
            jFrame.add(new MyPanel());
            jFrame.pack();
            jFrame.setResizable(false);
            jFrame.setVisible(true);
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM