本文介绍了使用Nimbus时,只有在控制有焦点时才将控制背景颜色设为黄色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该是直截了当的,但我还没有找到一个好的方法来做到这一点......



使用Swing Nimbus L& F,我想让我的控件(JButtons,JTextField等......)在焦点时为黄色背景颜色。除了黄色背景颜色,我希望它们保留所有常见的Nimbus样式。



当没有聚焦时,我希望它们能够用正常的Nimbus样式绘制。 / p>

我发现这样做的唯一方法是为每一个控件重写控件 [Focused] .backgroundPainter(这相当于从头开始重写Nimbus的很大一部分。)



我错过了什么吗?谢谢!

解决方案

为Nimbus外观提供简单的矩阵,但需要覆盖所有相关的鼠标和焦点事件,而不是覆盖...可能只是,





代码

  import com.sun.java。 swing.Painter; 
import java.awt。*;
import javax.swing。*;

公共类NimbusJPanelBackGround {

public NimbusJPanelBackGround(){
JButton btn = new JButton(Whatever);
JButton btn1 =新JButton(随便);
JPanel p = new JPanel();
p.add(btn);
p.add(btn1);
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.add(p,BorderLayout.CENTER);
f.pack();
f.setLocation(150,150);
f.setVisible(true);
}

public static void main(String [] args){

try {
for(UIManager.LookAndFeelInfo laf:UIManager.getInstalledLookAndFeels() ){
if(Nimbus.equals(laf.getName())){
UIManager.setLookAndFeel(laf.getClassName());
UIManager.getLookAndFeelDefaults()。put(Panel.background,Color.white);
UIManager.getLookAndFeelDefaults()。put(nimbusFocus,Color.blue);
UIManager.getLookAndFeelDefaults()。put(Button [Focused + MouseOver] .backgroundPainter,
new FillPainter(new Color(127,255,191)));
UIManager.getLookAndFeelDefaults()。put(Button [MouseOver] .backgroundPainter,
new FillPainter(new Color(127,255,191)));
}
}
} catch(例外e){
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable(){

@Override
public void run(){
NimbusJPanelBackGround nimbusJPanelBackGround = new NimbusJPanelBackGround();
}
});
}
}

类FillPainter实现Painter< JComponent> {

私人最终颜色;

public FillPainter1(Color c){
color = c;
}

@Override
public void paint(Graphics2D g,JComponent对象,int width,int height){
g.setColor(color);
g.fillRect(0,0,width - 1,height - 1);
}
}


This seems like it should be straightforward, but I haven't found a "good" way to do it...

While using the Swing Nimbus L&F, I want to give my controls (JButtons, JTextField, etc...) a yellow background color when they have focus. Other than the yellow background color, I want them to keep all the usual Nimbus styling.

When not focused, I want them to be drawn with the normal Nimbus styling.

The only way I've found to do this is to rewrite the control[Focused].backgroundPainter for every single control (which would amount to rewriting a large part of Nimbus from scratch).

Am I missing something? Thanks!

解决方案

Nimbus Default provide simple matrix for Nimbus Look and Feel, but required to override all related Mouse and Focus events, without overriding ... could be only,

from code

import com.sun.java.swing.Painter;
import java.awt.*;
import javax.swing.*;

public class NimbusJPanelBackGround {

    public NimbusJPanelBackGround() {
        JButton btn = new JButton("  Whatever  ");
        JButton btn1 = new JButton("  Whatever  ");
        JPanel p = new JPanel();
        p.add(btn);
        p.add(btn1);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.add(p, BorderLayout.CENTER);
        f.pack();
        f.setLocation(150, 150);
        f.setVisible(true);
    }

    public static void main(String[] args) {

        try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                    UIManager.getLookAndFeelDefaults().put("Panel.background", Color.white);
                    UIManager.getLookAndFeelDefaults().put("nimbusFocus", Color.blue);
                    UIManager.getLookAndFeelDefaults().put("Button[Focused+MouseOver].backgroundPainter",
                            new FillPainter(new Color(127, 255, 191)));
                    UIManager.getLookAndFeelDefaults().put("Button[MouseOver].backgroundPainter",
                            new FillPainter(new Color(127, 255, 191)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                NimbusJPanelBackGround nimbusJPanelBackGround = new NimbusJPanelBackGround();
            }
        });
    }
}

class FillPainter implements Painter<JComponent> {

    private final Color color;

    public FillPainter1(Color c) {
        color = c;
    }

    @Override
    public void paint(Graphics2D g, JComponent object, int width, int height) {
        g.setColor(color);
        g.fillRect(0, 0, width - 1, height - 1);
    }
}

这篇关于使用Nimbus时,只有在控制有焦点时才将控制背景颜色设为黄色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 03:58