Java,设置按钮的颜色而不是macOS上的边框?

Posted

技术标签:

【中文标题】Java,设置按钮的颜色而不是macOS上的边框?【英文标题】:Java, set colour of a button and not the border on a macOS? 【发布时间】:2020-12-21 01:52:38 【问题描述】:

我试图在鼠标点击时为我的按钮分配随机颜色。该动作本身似乎有效,但它没有为我的按钮着色 - 而是边框! :((仅供参考,我刚开始学习编码,所以我为我的“2+2=4”技能道歉)

它也不允许我在 If 语句或其他任何地方执行 setBorderPainted(false)。

这是我的代码:

import javax.swing.border.Border;
import javax.xml.transform.Source;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class buttonTest extends JFrame implements ActionListener 

    JPanel p = new JPanel();
    Random rand = new Random();
    Button one, two, three, four, five, six, seven, eight, nine;


    public static void main(String[] args) 

        new buttonTest();
    

    public buttonTest() 
        super("ColourButton(2.0)");
        setSize(500, 500);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        one = new Button("one");
        two = new Button("two");
        three = new Button("three");
        four = new Button("four");
        five = new Button("five");
        six = new Button("six");
        seven = new Button("seven");
        eight = new Button("eight");
        nine = new Button("nine");

        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();

        Color randColor = new Color(r, g, b);


        p.setLayout(new GridLayout(3, 3));
        p.add(one);
        p.add(two);
        p.add(three);
        p.add(four);
        p.add(five);
        p.add(six);
        p.add(seven);
        p.add(eight);
        p.add(nine);

        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        four.addActionListener(this);
        five.addActionListener(this);
        six.addActionListener(this);
        seven.addActionListener(this);
        eight.addActionListener(this);
        nine.addActionListener(this);


        add(p);
        setVisible(true);
    

    @Override
    public void actionPerformed(ActionEvent e) 
        String clickedbutton = e.getActionCommand();
        System.out.println(clickedbutton + " button clicked.");


        float r = rand.nextFloat();
        float g = rand.nextFloat();
        float b = rand.nextFloat();

        Color randColor = new Color(r, g, b);


        if (e.getSource() == one) 
            one.setBackground(new Color(r,g,b));

         else if (e.getSource() == two) 
            two.setBackground(new Color(r,g,b));

         else if (e.getSource() == three) 
            three.setBackground(new Color(r,g,b));

         else if (e.getSource() == four) 
            four.setBackground(new Color(r,g,b));

         else if (e.getSource() == five) 
            five.setBackground(new Color(r,g,b));

         else if (e.getSource() == six) 
            six.setBackground(new Color(r,g,b));

         else if (e.getSource() == seven) 
            seven.setBackground(new Color(r,g,b));

         else if (e.getSource() == eight) 
            eight.setBackground(new Color(r,g,b));

         else 
            nine.setBackground(new Color(r,g,b));
        


        

    



【问题讨论】:

【参考方案1】:

对所有 JButton 使用 button.setBorderPainted(false);,这应该会去掉边框。但是,如果那不是您在说的,那么我不明白您要做什么;您的 JButton 在单击时会被着色,而不是边框​​。

另外,为了减少您的代码,我强烈建议您创建一个包含所有 JButton 的 JButton 数组,这样您就无需编写代码来初始化和设置每个 JButton 的颜色。

这是我写的修改后的代码:

 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.Random;
 import javax.swing.*;

 public class ButtonTest extends JFrame implements ActionListener 

JPanel p = new JPanel();
Random rand = new Random();
JButton buttons[];


public static void main(String[] args) 

    new ButtonTest();


public ButtonTest() 
    super("ColourButton(2.0)");
    setSize(500, 500);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    buttons = new JButton[9];
    p.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < buttons.length; i++)
    
        buttons[i] = new JButton(Integer.toString(i));
        buttons[i].setBorderPainted(false);
        buttons[i].addActionListener(this);
        p.add(buttons[i]);
    
    
    add(p);
    setVisible(true);


@Override
public void actionPerformed(ActionEvent e) 
    String clickedbutton = e.getActionCommand();
    System.out.println(clickedbutton + " button clicked.");

    float r = rand.nextFloat();
    float g = rand.nextFloat();
    float b = rand.nextFloat();

    JButton button = (JButton)e.getSource();

    button.setBackground(new Color(r,g,b));
    button.setForeground(new Color(0, 0, 0, 250));


【讨论】:

否,否则它应该可以工作。您是否尝试在 Mac 上运行代码? 是的,我试过了,它对我不起作用......因此我的回应......欢迎来到“跨平台开发”? 好的,抱歉。另外,Mac 是最糟糕的,真正的程序员不会使用它们 不,真正的程序使用汇编:/【参考方案2】:

“冗长”的答案将涉及创建您自己的 UI 委托类,它允许您完全控制按钮的外观,但对于如此简单的事情来说,这似乎需要付出很多努力。

setBorderPaintedsetContentAreaFilled 开始。通常,这将允许您删除平台 UI 委托所做的所有自定义,但在我的测试中,我还需要使用 setOpaque(true)

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test 

    public static void main(String[] args) 
        new Test();
    

    public Test() 
        EventQueue.invokeLater(new Runnable() 
            @Override
            public void run() 
                JFrame frame = new JFrame();
                frame.add(new ButtonTest());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            
        );
    

    public class ButtonTest extends JPanel implements ActionListener 

        Random rand = new Random();
        JButton buttons[];

        public ButtonTest() 
            buttons = new JButton[9];
            setLayout(new GridLayout(3, 3));
            for (int i = 0; i < buttons.length; i++) 
                buttons[i] = new JButton(Integer.toString(i));

                buttons[i].setBorderPainted(false);
                // This may not be needed, but shouldn't hurt
                buttons[i].setContentAreaFilled(false);
                // This is what fixed the issue for me
                // But you might need to consider providing a "default"
                // background color OR change this in the `actionPerformed`
                // method
                buttons[i].setOpaque(true);

                buttons[i].addActionListener(this);
                add(buttons[i]);
            
        

        @Override
        public void actionPerformed(ActionEvent e) 
            if (!(e.getSource() instanceof JButton)) 
                return;
            
            String clickedbutton = e.getActionCommand();
            System.out.println("You clicked " + clickedbutton);

            float r = rand.nextFloat();
            float g = rand.nextFloat();
            float b = rand.nextFloat();

            JButton button = (JButton) e.getSource();

            button.setBackground(new Color(r, g, b));
        
    

【讨论】:

啊太棒了!这行得通!非常感谢您的帮助:)

以上是关于Java,设置按钮的颜色而不是macOS上的边框?的主要内容,如果未能解决你的问题,请参考以下文章

带有白色边框的警报控制器

c#中(winform)如何去掉button边框颜色?

C#button边框颜色设置

当我设置自定义边框或背景颜色时,为什么Firefox在选择箭头按钮上设置背景颜色?

怎么设置html的text文本框的边框颜色和粗细?怎么给button按钮加背景图片?

如何更改圆形按钮上的边框颜色(Swift)