java,apple默认外观:如何设置按钮的颜色

时间:2023-01-19 16:16:47

i want to know how to set the color of a button (in example, but i will need to set every component color) with the apple look and feel.

我想知道如何设置按钮的颜色(例如,但我需要设置每个组件颜色)与苹果的外观和感觉。

I found an answer in * that suggest to change to the standard look and feel, that works for me, but i prefer not to change because I like apple's one.

我在*中找到了一个建议改为标准外观的答案,这对我有用,但我不想改变,因为我喜欢苹果的。

Is there any solution? I know there is because I saw many apps written in java that have colored buttons and also that use particular styles or images as background.

有什么解决方案吗?我知道这是因为我看到很多用java编写的应用程序都有彩色按钮,并且使用特定样式或图像作为背景。

Can you tell me a solution?

你能告诉我一个解决方案吗?

1 个解决方案

#1


0  

Extend the Jbutton class and in that override the repaint() method and call setBackground(COLOR.ORANGE), to change the button color.

扩展Jbutton类,并重写repaint()方法并调用setBackground(COLOR.ORANGE),以更改按钮颜色。

Now use this class to create all your buttons. If you wish to change color of a specific button, call the setBackground(COLOR.ORANGE) method on that specific button. Hope this helps. Have a look at the code below

现在使用此类创建所有按钮。如果要更改特定按钮的颜色,请在该特定按钮上调用setBackground(COLOR.ORANGE)方法。希望这可以帮助。看看下面的代码

package solutions;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class VerifierTest extends JFrame {

    private static final long serialVersionUID = 1L;

    public VerifierTest() {
        final JTextField tf = new JTextField("TextField1");

        getContentPane().add(tf, BorderLayout.NORTH);
        tf.setInputVerifier(new PassVerifier());

        final JTextField tf2 = new JTextField("TextField2");

        getContentPane().add(tf2, BorderLayout.SOUTH);
        tf2.setInputVerifier(new PassVerifier());

        final JButton b = new JButton("Button");
        b.setBackground(Color.ORANGE);
        b.setVerifyInputWhenFocusTarget(true);
        getContentPane().add(b, BorderLayout.EAST);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!tf.getInputVerifier().verify(tf)) {
                    JOptionPane.showMessageDialog(tf.getParent(), "illegal value: " + tf.getText(), "Illegal Value",
                            JOptionPane.ERROR_MESSAGE);
                }
                if (b.isFocusOwner()) {
                    System.out.println("Button clicked");
                }
            }
        });
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Frame frame = new VerifierTest();
        frame.setSize(400, 200);
        frame.setVisible(true);
    }

    class PassVerifier extends InputVerifier {

        @Override
        public boolean verify(JComponent input) {
            final JTextField tf = (JTextField) input;
            String pass = tf.getText();
            if (pass.equals("Manish")) {
                return true;
            } else {
                return false;
            }
        }
    }
}

Comment the line "b.setBackground(Color.ORANGE);" and see the difference.

注释“b.setBackground(Color.ORANGE);”这一行并看到差异。

#1


0  

Extend the Jbutton class and in that override the repaint() method and call setBackground(COLOR.ORANGE), to change the button color.

扩展Jbutton类,并重写repaint()方法并调用setBackground(COLOR.ORANGE),以更改按钮颜色。

Now use this class to create all your buttons. If you wish to change color of a specific button, call the setBackground(COLOR.ORANGE) method on that specific button. Hope this helps. Have a look at the code below

现在使用此类创建所有按钮。如果要更改特定按钮的颜色,请在该特定按钮上调用setBackground(COLOR.ORANGE)方法。希望这可以帮助。看看下面的代码

package solutions;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class VerifierTest extends JFrame {

    private static final long serialVersionUID = 1L;

    public VerifierTest() {
        final JTextField tf = new JTextField("TextField1");

        getContentPane().add(tf, BorderLayout.NORTH);
        tf.setInputVerifier(new PassVerifier());

        final JTextField tf2 = new JTextField("TextField2");

        getContentPane().add(tf2, BorderLayout.SOUTH);
        tf2.setInputVerifier(new PassVerifier());

        final JButton b = new JButton("Button");
        b.setBackground(Color.ORANGE);
        b.setVerifyInputWhenFocusTarget(true);
        getContentPane().add(b, BorderLayout.EAST);
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!tf.getInputVerifier().verify(tf)) {
                    JOptionPane.showMessageDialog(tf.getParent(), "illegal value: " + tf.getText(), "Illegal Value",
                            JOptionPane.ERROR_MESSAGE);
                }
                if (b.isFocusOwner()) {
                    System.out.println("Button clicked");
                }
            }
        });
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Frame frame = new VerifierTest();
        frame.setSize(400, 200);
        frame.setVisible(true);
    }

    class PassVerifier extends InputVerifier {

        @Override
        public boolean verify(JComponent input) {
            final JTextField tf = (JTextField) input;
            String pass = tf.getText();
            if (pass.equals("Manish")) {
                return true;
            } else {
                return false;
            }
        }
    }
}

Comment the line "b.setBackground(Color.ORANGE);" and see the difference.

注释“b.setBackground(Color.ORANGE);”这一行并看到差异。