java, apple default look and feel: How to set color of a button

爷,独闯天下 提交于 2019-12-12 02:15:00

问题


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 stackoverflow 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.

Can you tell me a solution?


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/12563322/java-apple-default-look-and-feel-how-to-set-color-of-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!