【问题标题】:Defining a background color and a foreground icon for a JButton?为 JButton 定义背景颜色和前景图标?
【发布时间】:2017-08-01 16:12:06
【问题描述】:

我正在使用 Swing 制作 Fire Emblem 模型(我刚刚掌握了窍门,我以前主要在控制台程序上工作)。对于那些不熟悉游戏的人来说,这是一个基于棋子的策略游戏,您可以在网格上移动单位(类似于国际象棋)。

我正在考虑将 JButtons 用于网格,以便玩家能够点击他想要移动的单位,然后点击他的目的地。正如您在图片中看到的,单位后面的图块颜色可以变化(红色表示该单位可以攻击该图块上的单位,蓝色表示所选单位可以移动的图块)。我不想为每个单元使用 15 种不同的瓷砖设计(蓝色背景的 UnitX、红色背景的 UnitX、绿色背景的 UnitX 等),那么有没有办法将“图层”与 JButton 一起使用?画一个蓝色瓷砖并在上面画正确的字符?

【问题讨论】:

标签: java swing computer-science


【解决方案1】:

画一个蓝色瓷砖并在上面画正确的字符?

  1. 使用setBackground(...)方法设置背景 颜色。

  2. 使用setIcon(...)方法设置字符。

【讨论】:

    【解决方案2】:

    camickr 的回答是正确的(假设我已经理解了你的问题)。

    以下代码演示了如何在JButton 上使用setBackgroundsetIcon。它显示一个带有背景颜色和图标的按钮。按钮在被点击时会改变其背景颜色:

    import javax.swing.*;
    import java.awt.*;
    import java.net.*;
    import java.util.*;
    import java.util.List;
    
    public class ButtonBackgroundAndIcon {
        private static final List<Color> BACKGROUND_COLORS = Arrays.asList(
                new Color(229, 119, 120),
                Color.BLUE,
                Color.CYAN,
                Color.GREEN,
                Color.YELLOW,
                Color.RED
        );
    
        private int backgroundIndex;
    
        public static void main(String[] arguments) {
            SwingUtilities.invokeLater(new ButtonBackgroundAndIcon()::run);
        }
    
        private void run() {
            JFrame frame = new JFrame("Stack Overflow");
            frame.setBounds(100, 100, 800, 600);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            JButton tileButton = new JButton();
            tileButton.setBackground(BACKGROUND_COLORS.get(0));
    
            Icon icon = getRoyIcon();
            if (icon != null) {
                tileButton.setIcon(icon);
            }
    
            tileButton.addActionListener(actionEvent -> {
                backgroundIndex = (backgroundIndex + 1) % BACKGROUND_COLORS.size();
                tileButton.setBackground(BACKGROUND_COLORS.get(backgroundIndex));
            });
    
            frame.getContentPane().add(tileButton);
    
            frame.setVisible(true);
        }
    
        private ImageIcon getRoyIcon() {
            ImageIcon imageIcon;
    
            try {
                String iconLocation = "http://orig06.deviantart.net/fd0e/f/2008"
                                      + "/060/d/1/roy_sprite_by_chstuba007.gif";
                imageIcon = new ImageIcon(new URL(iconLocation));
            } catch (MalformedURLException e) {
                imageIcon = null;
            }
    
            return imageIcon;
        }
    }
    

    【讨论】:

    • (1-) 正如您所说,答案已经给出。无需重复答案。无需编写代码。 OP 所要做的就是将这两行代码添加到他们当前的程序中以测试解决方案。
    【解决方案3】:

    假设如果你有一个透明的图像,你可以使用这个类来完成你想要的。

    公共类 ColorBackgroundIcon 实现图标 {

    private ImageIcon image;
    private Color background;
    
    public ColorBackgroundIcon(ImageIcon image, 
                               Color background) {
        this.image = image;
        this.background = background;
    }
    
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        Graphics2D g2d = (Graphics2D) g;
        g.setColor(background);
        g.fillRect(x, y, getIconWidth(), getIconHeight());
        g2d.drawImage(image.getImage(), x, y, null);
    
    }
    
    public void setColor(Color bgColor) {
        this.background = bgColor;
    }
    
    @Override
    public int getIconWidth() {
        return image.getIconWidth();
    }
    
    @Override
    public int getIconHeight() {
        return image.getIconHeight();
    }
    

    }

    然后只需使用上面创建的实例 setIcon()。对我很有用,因为我有四个状态,某个图标可以代表。我没有创建四个不同的图标,而是使用一个透明图标,并使用提供的代码更改背景。

    希望对任何人都有帮助, 丹麦

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 2010-10-03
      • 2016-03-03
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 2011-01-28
      • 2015-06-29
      相关资源
      最近更新 更多