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

huangapple go评论69阅读模式
英文:

Java, set colour of a button and not the border on a macOS?

问题

import javax.swing.*;
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));
        }
    }
}
英文:

I'm trying to assign random colors to my buttons on mouseclick. The action itself seems to work but it is not coloring my button - but the border instead! Java,设置按钮在 macOS 上的颜色,而不是边框? (FYI, I'm just starting to learn to code so I apologize for my '2+2=4' skills)

It also won't let me do setBorderPainted(false) within the If statements or anywhere else.

Here's my code:

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));
}
}
}
</details>
# 答案1
**得分**: 1
```java
// "long" answer would involve creating your own UI delegate class which allowed you to take full control over the buttons appearance, but that seems like a lot of effort for something so simple.
// A place to start is looking at `setBorderPainted` and `setContentAreaFilled`.  This will allow you to, generally, remove all the customisation done by the platforms UI delegate, but in my testing, I also need to use `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));
}
}
}
英文:

The "long" answer would involve creating your own UI delegate class which allowed you to take full control over the buttons appearance, but that seems like a lot of effort for something so simple.

A place to start is looking at setBorderPainted and setContentAreaFilled. This will allow you to, generally, remove all the customisation done by the platforms UI delegate, but in my testing, I also need to use setOpaque(true)

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

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 &lt; buttons.length; i++) {
buttons[i] = new JButton(Integer.toString(i));
buttons[i].setBorderPainted(false);
// This may not be needed, but shouldn&#39;t hurt
buttons[i].setContentAreaFilled(false);
// This is what fixed the issue for me
// But you might need to consider providing a &quot;default&quot;
// 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(&quot;You clicked &quot; + 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));
}
}
}

答案2

得分: 0

使用 button.setBorderPainted(false); 对所有的 JButtons 进行设置,这将去除边框。但如果这不是你所讨论的内容,那么我不明白你试图做什么;你的 JButtons 在被点击时会改变颜色,而不是边框。

此外,为了减少你的代码量,我强烈建议创建一个 JButton 数组,其中包含了所有的 JButtons,这样你就不需要为每个单独的 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));
    }
}
英文:

Use button.setBorderPainted(false); for all the JButtons, which should take away the border. But if that's not what you're talking about then I don't understand what you're trying to do; your JButtons are being colored when they are clicked, not the border.

Also, in order to reduce your code, I highly recommend creating a JButton array that holds all of your JButtons so you don't need to write code to initialize and set the color of every inividual JButton.

Here is the revised code that I wrote:

 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(&quot;ColourButton(2.0)&quot;);
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
buttons = new JButton[9];
p.setLayout(new GridLayout(3, 3));
for (int i = 0; i &lt; 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 + &quot; button clicked.&quot;);
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));
}
}

huangapple
  • 本文由 发表于 2020年9月2日 06:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63696304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定