java – 在Windows上设置JButton背景颜色
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在Windows上设置JButton背景颜色,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3654字,纯文字阅读大概需要6分钟。
内容图文
我有一个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());当我删除它时,按钮是黑色的.
解决方法:
我猜测那些UIManager键/值对,PLAF是基于OS X的Aqua PLAF.这似乎是问题的一部分.这里没有在Windows上填充的内容区域.
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);
}
});
}
}
内容总结
以上是互联网集市为您收集整理的java – 在Windows上设置JButton背景颜色全部内容,希望文章能够帮你解决java – 在Windows上设置JButton背景颜色所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。