Java,Apple默认外观:如何设置按钮的颜色 [英] java, apple default look and feel: How to set color of a button
问题描述
我想知道如何通过苹果的外观设置按钮的颜色(例如,但是我将需要设置每种组件的颜色).
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.
我在stackoverflow中找到了一个答案,该答案建议更改为标准外观,这对我有用,但是我不愿更改,因为我喜欢苹果的.
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.
有什么解决办法吗? 我知道是因为我看到许多用Java编写的应用程序带有彩色按钮,并且还使用特定的样式或图像作为背景.
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?
推荐答案
扩展Jbutton类,并在其中重写repaint()方法并调用setBackground(COLOR.ORANGE)以更改按钮的颜色.
Extend the Jbutton class and in that override the repaint() method and call setBackground(COLOR.ORANGE), to change the button color.
现在使用此类创建您所有的按钮.如果要更改特定按钮的颜色,请在该特定按钮上调用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
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;
}
}
}
}
注释"b.setBackground(Color.ORANGE);"行并看到差异.
Comment the line "b.setBackground(Color.ORANGE);" and see the difference.
这篇关于Java,Apple默认外观:如何设置按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!