将按钮设置为“背景".灵气按钮的 [英] Set the button "background" of a Nimbus button

查看:66
本文介绍了将按钮设置为“背景".灵气按钮的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Nimbus外观设计应用程序.有一个表,一个列包含按钮(使用罗伯·卡米克(Rob Camick)).那确实有效,但是结果却不是我所期望的.我已尝试修复外观,但无济于事.

I'm working on an app using the Nimbus Look and Feel. There's a table and one column contains buttons (using the Table Button Column from Rob Camick). That does work, but the result isn't what I had expected. I have tried to fix the look, but to no avail.

所以问题是:如何更改Nimbus按钮的背景"(圆角矩形外部的区域)?最好采用非骇客的方式:-)

So the question is: how do I change the "background" (the area outside the rounded rectangle) of a Nimbus button? Preferably in a non-hacky way :-)

使用默认的表格列"按钮,结果如下所示:

Using the default Table Column Button, the result looks like this:

如您所见,对于奇数行(白色),背景(因此,我的意思是按钮的圆角矩形之外的区域)是错误的.产生此输出的代码是:

As you can see, the background (and by this I mean the area outside the button's rounded rectangle) is wrong for the odd (white) rows. The code that produces this output is:

public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
    if (isSelected) {
        renderButton.setForeground(table.getSelectionForeground());
        renderButton.setBackground(table.getSelectionBackground());
    } else {
        renderButton.setForeground(table.getForeground());
        renderButton.setBackground(table.getBackground());
    }

    if (hasFocus) {
        renderButton.setBorder( focusBorder );
    } else {
        renderButton.setBorder( originalBorder );
    }

    // <snip some code>

    renderButton.setOpaque(true);

    return renderButton;
}

renderButton是默认JButton的实例.我尝试弄乱按钮的背景颜色,但是效果不像我最初期望的那样:

The renderButton is an instance of a default JButton. I've tried messing with the background color of the button, but that didn't work out like I expected at first:

        Color alternate = (Color)LookAndFeel.getDesktopPropertyValue("Table.alternateRowColor", Color.lightGray);
        Color normal = (Color)LookAndFeel.getDesktopPropertyValue("Table.background", Color.white);
        if (row % 2 == 0) {
            renderButton.setBackground(normal);
        } else {
            renderButton.setBackground(alternate);
        }

这将产生:

因此这一次在第一张图片中看起来不错的按钮现在是坏的,反之亦然.按钮的内部背景(圆角矩形内的区域)根据背景颜色属性(确实是通过setBackground()调用真正修改的)似乎具有正确的颜色.但是外面的区域都是错的.好吧,让我们结合两个:

So this time the buttons that look alright in the first image are now bad and vice versa. The button's inner backgrounds (the areas inside the rounded rectangles) do seem to have the correct color according to the background color property (which is what's really modified with the setBackground() call). But the area outside is all wrong. Alright, let's combine the two :

        Color alternate = table.getBackground();
        Color normal = (Color)LookAndFeel.getDesktopPropertyValue("Table.background", Color.white);
        if (row % 2 == 0) {
            renderButton.setBackground(normal);
        } else {
            renderButton.setBackground(alternate);
        }

结果:

因此,现在的背景"看起来确实正确,但是按钮看起来不再像Nimbus按钮.如何使背景"具有正确的颜色,同时仍然看起来像雨云形按钮?

So now the "background" does look correct, but the buttons don't look like Nimbus buttons any more. How do I make the "background" have the correct color while still looking like Nimbus buttons?

推荐答案

请勿将背景设置为JButton.使用JPanel包装JButton并将背景设置为JPanel.如果您在一个JTable列中使用了更多按钮,这可能会很明显.

Do not set background to JButton. Use JPanel to wrap JButton and set background to JPanel. This would be probably obvious if you used more buttons in one JTable column.

要设置我所做的JPanel的正确背景颜色(您应该这样做):

To set correct background color of JPanel i did (you should):

  1. 保留对原始渲染器的引用
  2. 让原始渲染器渲染自己的组件(每次渲染)!
  3. 使用渲染组件的背景设置JPanel的背景(每次渲染)!

这样,您不必自己选择正确的颜色

This way you don't have to choose correct color yourself

此外,您还必须重写paintComponent才能正确绘制JPanel的白色背景:

Also you have to override paintComponent to correctly paint white background of JPanel:

@Override
protected void paintComponent(Graphics g) {
  Color background = getBackground();
  setBackground(new Color(background.getRGB()));
  super.paintComponent(g);
}

按照@kleopatra的建议,您不必覆盖paintComponent,只需将背景颜色设置为not-uiresource(在完整示例中显示)

as @kleopatra suggests you don't have to override paintComponent, only set background color as not-uiresource (shown in complete example)

以下是完整的示例:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.table.TableCellRenderer;

public class Test {
public static void main(String[] args) throws Throwable {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }

    String[] columnNames = new String[]{"c1"};
    Object[][] data = new Object[4][1];
    data[0][0] = "First";
    data[1][0] = "Second";
    data[2][0] = "Third";
    data[3][0] = "Fourth";

    JTable table  = new JTable(data, columnNames){
        @Override
        public javax.swing.table.TableCellRenderer getCellRenderer(int row, int column) {
            final TableCellRenderer ori = super.getCellRenderer(row, column);
            final TableCellRenderer mine = new TableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int column) {
                    Component c = ori.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                    JPanel p = new JPanel();
                    if(value == null){
                        value = "";
                    }
                    p.add(new JButton(value.toString()));
                    p.setBackground(new Color(c.getBackground().getRGB()));
                    return p;
                }
            };
            return mine;
        };
    };
    table.setRowHeight(50);
    JFrame f = new JFrame();
    f.add(table);
    f.setVisible(true);
    f.pack();
}
}

结果:

这篇关于将按钮设置为“背景".灵气按钮的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆