2015-11-21 72 views
4

我有一个JButton,我想将背景设置为一种颜色。在窗口上设置JButton背景颜色

JButton button = new JButton(); 
button.setVisible(true); 
button.setPreferredSize(new Dimension(student_scroll.getWidth(), 50)); 
button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1)); 
button.setBackground(Color.BLACK); 
button.setForeground(Color.WHITE); 
button.setOpaque(true); 

我用这个用于mac,它显示为我想要的。然而,在Windows上尝试它时,前景是白色的(应该是),但背景是空的。

Setting background color to JButton

说加button.setContentAreaFilled(false);我做到了,但都没有效果。大多数人说要添加button.setOpaque(true);,我也已经这样做了。

我还有什么要做的,它会显示黑色背景?

编辑

按照要求,SSCCE:

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class MainSwing extends JFrame { 
    private static final long serialVersionUID = -8231889836024827530L; 

    public static void main(String[] args) { 
     try { 
      System.setProperty("apple.laf.useScreenMenuBar", "true"); 
      System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test"); 
      UIManager.put("ScrollBarUI", "main.CustomScrollBarUI"); 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    } 
    catch(ClassNotFoundException e) { 
      System.out.println("ClassNotFoundException: " + e.getMessage()); 
    } 
    catch(InstantiationException e) { 
      System.out.println("InstantiationException: " + e.getMessage()); 
    } 
    catch(IllegalAccessException e) { 
      System.out.println("IllegalAccessException: " + e.getMessage()); 
    } 
    catch(UnsupportedLookAndFeelException e) { 
      System.out.println("UnsupportedLookAndFeelException: " + e.getMessage()); 
    } 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame frame = new JFrame() { 

        Container c = getContentPane(); 
        JButton button = new JButton("Hello"); 
        { 
         button.setText("Hello"); 
         button.setVisible(true); 
         button.setPreferredSize(new Dimension(100, 50)); 
         button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1)); 
         button.setBackground(Color.BLACK); 
         button.setForeground(Color.WHITE); 
         button.setOpaque(true); 
         c.add(button); 
        } 

       }; 
       frame.setSize(500, 500); 
       frame.setBackground(Color.BLACK); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

看来这个问题已经是与行:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());,当我将其删除,按钮是黑色的。

+0

工作正常,我。发布一个合适的[SSCCE](http://sscce.org/)来说明问题。所以你需要的是一个带有按钮的框架来做你的测试。整个程序将是10-15行代码。 – camickr

+1

'setContentAreaFilled(false)'和/或'setBorderPainted(false)'。 “填充”效果由外观和感觉委托提供,我不知道任何影响其使用颜色的方法 – MadProgrammer

+0

'button.setVisible(true);'不是必需的。只需将它添加到一个顶层窗口(框架,窗口,对话框等)中,它可以在添加组件后自行打包,然后设置为可见。 'button.setPreferredSize(new Dimension(student_scroll.getWidth(),50));'如果按钮没有文本,则可以使用(可能是透明的)图像轻松设置大小。如果按钮**具有文本**,则更好地根据字体大小和文本内容以及包含它的按钮的边距和边框来确定大小。在任何情况下,我们都不需要**设置**首选尺寸。 –

回答

4

我从这些UIManager键/值对猜测PLAF是基于OS X的Aqua PLAF。这似乎是问题的一部分。这里没有在Windows上填充内容区域。

enter image description here

import java.awt.*; 
import javax.swing.*; 

public class MainSwing extends JFrame { 

    public static void main(String[] args) { 
     try { 
      System.setProperty("apple.laf.useScreenMenuBar", "true"); 
      System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test"); 
      UIManager.put("ScrollBarUI", "main.CustomScrollBarUI"); 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception e) { 
      e.printStackTrace(); // more info for less LOC! 
     } 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame frame = new JFrame() { 

        Container c = getContentPane(); 
        JButton button = new JButton("Hello"); 

        { 
         c.setLayout(new GridLayout(0,1)); 
         c.add(new JButton("Hi")); 
         button.setText(UIManager.getSystemLookAndFeelClassName()); 
         button.setVisible(true); 
         button.setPreferredSize(new Dimension(400, 100)); 
         button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1)); 
         button.setContentAreaFilled(false); 
         button.setBackground(Color.BLACK); 
         button.setForeground(Color.WHITE); 
         button.setOpaque(true); 
         c.add(button); 
        } 
       }; 
       frame.pack(); 
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
+3

(1+),哇这也适用于我在Windows上。我发现'setOpaque(true)'语句很重要,它必须在'setContentAreaFilled(false)'语句之后被调用,因为这个语句显然调用了'setOpaque(false)',我不知道这一点。每天学习一些东西。我想我应该先阅读你的答案'设置背景颜色按钮'第一:) – camickr

+0

@camickr始终的鼠尾草分析。 :)我也看到有价值的方法来解决PLAF的整个“问题”,正如你的答案中所建议的那样。 –

+0

回到未来,这也解决了Jython JButton的相同问题。 –

3

看来这个问题已经是与行:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());当我将它删除时,按钮是黑色的。

因此,这是我们在创建SSCCE之前所没有的额外信息(这就是为什么您应始终发布SSCCE与您的问题)。

它也告诉你问题不在于你的代码,而是在LAF中。 Windows LAF忽略setBackground(...)方法并绘制其自己的背景。

一个选项是将图标添加到所需颜色的按钮。然后,您可以配置要在按钮中心绘制的文本:

import java.awt.*; 
import javax.swing.*; 

public class ColorIcon implements Icon 
{ 
    private Color color; 
    private int width; 
    private int height; 

    public ColorIcon(Color color, int width, int height) 
    { 
     this.color = color; 
     this.width = width; 
     this.height = height; 
    } 

    public int getIconWidth() 
    { 
     return width; 
    } 

    public int getIconHeight() 
    { 
     return height; 
    } 

    public void paintIcon(Component c, Graphics g, int x, int y) 
    { 
     g.setColor(color); 
     g.fillRect(x, y, width, height); 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    public static void createAndShowGUI() 
    { 
     JPanel panel = new JPanel(new GridLayout(2, 2)); 

     for (int i = 0; i < 4; i++) 
     { 
      Icon icon = new ColorIcon(Color.RED, 50, 50); 
      JButton label = new JButton(icon); 
      label.setText("" + i); 
      label.setHorizontalTextPosition(JButton.CENTER); 
      label.setVerticalTextPosition(JButton.CENTER); 
      panel.add(label); 
     } 

     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(panel); 
     f.setSize(200, 200); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 
} 

然后是应该在所有LAF上工作。