- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望能够即时更新我的 Swing GUI 的 LookAndFeel 属性。在这种情况下,我有一个简单的 Swing/Awt 游戏,它运行最初作为 Nimbus LookAndFeel
的游戏。在启动后的不同时间点,我只想更改(比方说)一个细节:我的应用程序的背景颜色。
我可以通过这样做来改变背景颜色:
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
UIManager.getLookAndFeelDefaults().put("Panel.background", Color.RED);
SwingUtilities.updateComponentTreeUI(SomeGame.this);
break;
}
}
这个“有效”是因为应用程序的背景颜色会正确更改并且程序不会崩溃。但是在命令行上我得到错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthLookAndFeel.paintRegion(SynthLookAndFeel.java:371)
at javax.swing.plaf.synth.SynthLookAndFeel.update(SynthLookAndFeel.java:335)
显然,有些东西是空的,但我不知道它是什么或如何修复它。一定有什么我不明白的地方。我查看了有关 setting background colors in Nimbus 的其他 StackOverflow 问题和 overriding the LookAndFeel information after startup .
getLookAndFeelDefaults()
时,我是否还需要指定其余的默认值?最佳答案
Java6 的所有代码,必须更改 Java7 的 Nimbus 导入
Nimbus
有一些惊喜,其中之一是可以(没有肮脏的 hacks,我正在为那些 hacks 留下评论)改变 Background
JPanel
仅一次
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.AlphaComposite;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EmptyBorder;
public class ButtonTest {
private JFrame frame;
private JPanel panel;
private JButton opaqueButton1;
private JButton opaqueButton2;
private SoftJButton softButton1;
private SoftJButton softButton2;
private Timer alphaChanger;
public void createAndShowGUI() {
opaqueButton1 = new JButton("Opaque Button");
opaqueButton2 = new JButton("Opaque Button");
softButton1 = new SoftJButton("Transparent Button");
softButton2 = new SoftJButton("Transparent Button");
opaqueButton1.setBackground(Color.GREEN);
softButton1.setBackground(Color.GREEN);
panel = new JPanel();
panel.setLayout(new java.awt.GridLayout(2, 2, 10, 10));
panel.add(opaqueButton1);
panel.add(softButton1);
panel.add(opaqueButton2);
panel.add(softButton2);
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
frame = new JFrame();
frame.add(panel);
frame.setLocation(150, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
alphaChanger = new Timer(50, new ActionListener() {
private float incrementer = -.03f;
@Override
public void actionPerformed(ActionEvent e) {
float newAlpha = softButton1.getAlpha() + incrementer;
if (newAlpha < 0) {
newAlpha = 0;
incrementer = -incrementer;
} else if (newAlpha > 1f) {
newAlpha = 1f;
incrementer = -incrementer;
}
softButton1.setAlpha(newAlpha);
softButton2.setAlpha(newAlpha);
}
});
alphaChanger.start();
Timer uiChanger = new Timer(3500, new ActionListener() {
private final LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
private int index = 1;
@Override
public void actionPerformed(ActionEvent e) {
try {
UIManager.setLookAndFeel(laf[index].getClassName());
if (laf[index].getClassName().equals("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel")) {
UIManager.getLookAndFeelDefaults().put("Panel.background", Color.orange);
SwingUtilities.updateComponentTreeUI(frame);
} else {
panel.setBackground(Color.yellow);
SwingUtilities.updateComponentTreeUI(frame);
}
opaqueButton1.setText(laf[index].getClassName());
softButton1.setText(laf[index].getClassName());
frame.pack();
} catch (Exception exc) {
exc.printStackTrace();
}
index = (index + 1) % laf.length;
}
});
uiChanger.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ButtonTest().createAndShowGUI();
}
});
}
private static class SoftJButton extends JButton {
private static final JButton lafDeterminer = new JButton();
private static final long serialVersionUID = 1L;
private boolean rectangularLAF;
private float alpha = 1f;
SoftJButton() {
this(null, null);
}
SoftJButton(String text) {
this(text, null);
}
SoftJButton(String text, Icon icon) {
super(text, icon);
setOpaque(false);
setFocusPainted(false);
}
public float getAlpha() {
return alpha;
}
public void setAlpha(float alpha) {
this.alpha = alpha;
repaint();
}
@Override
public void paintComponent(java.awt.Graphics g) {
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
if (rectangularLAF && isBackgroundSet()) {
Color c = getBackground();
g2.setColor(c);
g.fillRect(0, 0, getWidth(), getHeight());
}
super.paintComponent(g2);
}
@Override
public void updateUI() {
super.updateUI();
lafDeterminer.updateUI();
rectangularLAF = lafDeterminer.isOpaque();
}
}
}
JComponent
可以改变它的 Color
, Font
, etc by calling UIManager.getLookAndFeel().uninitialize();
,但这不适用于容器
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
public class NimbusTestButtonsBackground extends JFrame {
private static final long serialVersionUID = 1L;
private javax.swing.JButton button;
public NimbusTestButtonsBackground() {
button = new javax.swing.JButton();
button.setText("Text");
add(button);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.pack();
Timer t = new Timer(1000, new ActionListener() {
private Random r = new Random();
@Override
public void actionPerformed(ActionEvent e) {
Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
try {
LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
UIDefaults uiDefaults = lnf.getDefaults();
uiDefaults.put("nimbusBase", c);
UIManager.getLookAndFeel().uninitialize();
UIManager.setLookAndFeel(lnf);
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
UIDefaults defaults = UIManager.getDefaults();
defaults.put("Button.background", c);
SwingUtilities.updateComponentTreeUI(button);
}
});
t.start();
}
public static void main(String args[]) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
return;
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NimbusTestButtonsBackground().setVisible(true);
}
});
}
}
JPanel
的 paintComponent
是最好的方式
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.RepaintManager;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class ChangePanelBackgroundNimbus {
private JFrame frame = new JFrame();
public ChangePanelBackgroundNimbus() {
GradientPane pane = new GradientPane();
pane.setLayout(new GridLayout(6, 4, 10, 10));
for (int i = 1; i <= 24; i++) {
pane.add(createButton(i));
}
pane.setOpaque(false);
frame.add(pane);
RepaintManager.setCurrentManager(new RepaintManager() {
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
Container con = c.getParent();
while (con instanceof JComponent) {
if (!con.isVisible()) {
return;
}
if (con instanceof GradientPane) {
c = (JComponent) con;
x = 0;
y = 0;
w = con.getWidth();
h = con.getHeight();
}
con = con.getParent();
}
super.addDirtyRegion(c, x, y, w, h);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
//frame.setSize(400, 300);
frame.pack();
frame.setVisible(true);
}
private JButton createButton(final int text) {
JButton button = new JButton(Integer.toString(text));
return button;
}
class GradientPane extends JPanel {
private static final long serialVersionUID = 1L;
private final int h = 150;
private BufferedImage img = null;
private BufferedImage shadow = new BufferedImage(1, h, BufferedImage.TYPE_INT_ARGB);
public GradientPane() {
paintBackGround(new Color(150, 250, 150));
Timer t = new Timer(500, new ActionListener() {
private Random r = new Random();
@Override
public void actionPerformed(ActionEvent e) {
paintBackGround(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
}
});
t.start();
}
public void paintBackGround(Color g) {
Graphics2D g2 = shadow.createGraphics();
g2.setPaint(g);
g2.fillRect(0, 0, 1, h);
g2.setComposite(AlphaComposite.DstIn);
g2.setPaint(new GradientPaint(0, 0, new Color(0, 0, 0, 0f), 0, h, new Color(0.4f, 0.8f, 0.8f, 0.5f)));
g2.fillRect(0, 0, 1, h);
g2.dispose();
}
@Override
public void paintComponent(Graphics g) {
if (img == null || img.getWidth() != getWidth() || img.getHeight() != getHeight()) {
img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
}
Graphics2D g2 = img.createGraphics();
super.paintComponent(g2);
Rectangle bounds = this.getVisibleRect();
g2.scale(bounds.getWidth(), -1);
g2.drawImage(shadow, bounds.x, -bounds.y - h, null);
g2.scale(1, -1);
g2.drawImage(shadow, bounds.x, bounds.y + bounds.height - h, null);
g2.dispose();
g.drawImage(img, 0, 0, null);
repaint();
}
}
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ChangePanelBackgroundNimbus ml = new ChangePanelBackgroundNimbus();
}
});
}
}
关于java - 即时更新 LookAndFeel 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14722306/
我已经四处寻找我的问题的解决方案,但它们似乎都涉及看似复杂的方法,如数据透视表、交叉表等。似乎必须有一个更简单的解决方案来解决那些没有解决的问题在我看来是一个特别复杂的问题。我正在使用 MS SQL
我在共享服务器上安装了 MySQL,并且可以通过 phpMyAdmin 进行访问。我想将该数据库连续实时克隆到云 mySQL 数据库(我们专门为此数据库创建了一个支持 Nginx 的 MySQL 服务
我目前正在围绕一个相当复杂的数据模型编写一个 Django 应用程序。对于许多用例,我需要构建相似但略有不同的模板(包括 graphviz 等)。 现在我想知道是否有一种方法可以遵循 DRY 并“即时
我选择了图片并在提交表单之前进行了预览。但是我想在选择图像并预览并提交文件后即时编辑文件。 js代码: var img = null; var canvas1 = document.g
目前,我们的网站存储 2/3 的固定图像尺寸。这些在上传时生成并通过我们的 CDN 分发。然而,我们需要实现更灵活的解决方案,我们正在开发需要多种不同尺寸的移动和平板电脑应用程序。我们建议的解决方案是
在 Google Wave 的介绍视频中,他们谈到了网络应用程序中的聊天问题。在许多 Web 应用程序中,您会看到如下消息: is typing.. (消息提交前) Google 想出了一个想法“在键
这个问题在这里已经有了答案: Formatting a number with leading zeros in PHP [duplicate] (11 个回答) 关闭3年前. PHP - 是否有一种
如何在VBA的“即时”窗口中打印二维数组?是否存在执行此操作的通用方法?一种在“即时”窗口中为每行绘制一排数组的方法可以解决此问题,因为唯一要做的就是为数组的每一行循环此代码。 最佳答案 我做了一个简
与非 JIT 编译器相比,JIT 编译器具体做什么?谁能给出一个简洁易懂的描述? 最佳答案 JIT 编译器在程序启动后运行,并将代码(通常是字节码或某种 VM 指令)动态(或称为即时)编译为通常更快的
我已经在我的 Windows 2003 服务器上安装了 VisualSVN,并将其配置为提供匿名读取访问。据我了解,VisualSVN 仅使用 apache 和下面的官方 SVN 存储库服务器。 现在
我正在开发一个使用 Twig 的 PHP 应用程序(但这并不重要)作为 View 层。这个 View 层有一个自定义扩展,允许我注册远程样式和脚本 Assets 以及样式和脚本内联 block 。系统
如今在许多网页上,您会经常看到带有指向目标的箭头的即时工具提示,类似于: https://www.w3schools.com/css/tryit.asp?filename=trycss_tooltip
我正在用 C++ 编写并在 Windows 中使用 OpenGL。 我创建了一个立方体,我希望它通过按“4”或“6”小键盘键围绕 y 轴旋转(使用 glRotate3f(),而不是 gluLookat
与非 JIT 编译器相比,JIT 编译器具体做什么?谁能给出一个简洁易懂的描述? 最佳答案 JIT 编译器在程序启动后运行,并将代码(通常是字节码或某种 VM 指令)动态(或称为即时)编译为通常更快的
这个问题在这里已经有了答案: 关闭 10 年前。
与非 JIT 编译器相比,JIT 编译器具体做什么?谁能给个简洁易懂的描述? 最佳答案 JIT 编译器在程序启动后运行,并将代码(通常是字节码或某种 VM 指令)即时(或所谓的即时)编译成通常速度更快
与非 JIT 编译器相比,JIT 编译器具体做什么?谁能给个简洁易懂的描述? 最佳答案 JIT 编译器在程序启动后运行,并将代码(通常是字节码或某种 VM 指令)即时(或所谓的即时)编译成通常速度更快
我希望能够即时将音频文件转换为 MP3 以供用户浏览器使用。我正在使用的软件是:ubuntu 系统上的 Apache、PHP 和 FFMPEG。这是我到目前为止的代码: 使用此代码,仅转换音频的前几
我正在使用 IntervalObservable 连续调用我的应用程序的服务器端。我可以订阅和取消订阅 Oberservable,一切正常,但有一个异常(exception): 对服务器的第一次调用被
从服务器上的文件夹压缩(比如 2 个文件)并强制下载的最简单方法是什么?不将“zip”保存到服务器。 $zip = new ZipArchive(); //the string "fil
我是一名优秀的程序员,十分优秀!