更改 Swing 进度条的外观

发布于 2024-11-05 13:15:02 字数 190 浏览 8 评论 0原文

我一直在尝试更改 UIManager 以使这些看起来愚蠢的绿色方块消失。我如何改变用户的外观和感觉?是否依赖于系统?其他正在编译我的代码的人有一个恒定的梯度。理想情况下,它只是一个实心正方形,而不是较小的块。

谢谢

在此处输入图像描述

I keep trying to chang the UIManager to make these stupid looking green squares go away. how do i change the look and feel of this to the user? Is it system dependent? Someone else who was compiling my code had a constant gradient. Ideally, it would just be a solid square, as opposed to smaller blocks.

Thanks

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

偏爱你一生 2024-11-12 13:15:02

您是否尝试过将其设置为系统(用户)的外观和感觉?

设置外观的最简单方法是在调用后启动 GUI:

try
{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
    // ...
}

话虽如此,上面似乎是 Windows XP 主题,并且确实可能​​是系统(用户)主题。我通常会远离 GUI 中的自定义主题,除非有非常的充分理由(例如,客户/用户要求)。

也就是说,上面的代码使其依赖于系统,这是,因为它符合用户的期望。

Have you tried setting it to the system's (user's) look and feel?

The easiest way to set the look and feel is by launching the GUI after calling:

try
{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
    // ...
}

With that said, the above appears to be the Windows XP theme and may indeed be the system (user) theme. I generally stay away from custom themes in GUIs unless there is a very good reason (e.g., customer/user requirement).

That is to say, the above code makes it system dependent, which is good because it matches the user's expecations.

俯瞰星空 2024-11-12 13:15:02

“这些看起来愚蠢的绿色方块”是 Windows XP 的外观和感觉的元素。如果您希望它们看起来不同,您可以更改此特定组件的外观和感觉。

只需使用下面的解决方法:

try {
    javax.swing.UIManager.setLookAndFeel(/* Look and Feel for your JProgressBar*/);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}

MyProgressBar = new javax.swing.JProgressBar();

try {
    javax.swing.UIManager.setLookAndFeel(/* Previous, main Look and Feel */);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}

我使用此代码为 JFileChooser 提供另一种外观,并且它工作得很好。

您只需在创建组件之前更改外观和感觉,并在创建之后恢复前一个组件。

"These stupid looking green squares" are element of Windows XP's Look nad Feel. If you want them to look different, you can change Look and Feel for this particular component.

Just use below workaround:

try {
    javax.swing.UIManager.setLookAndFeel(/* Look and Feel for your JProgressBar*/);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}

MyProgressBar = new javax.swing.JProgressBar();

try {
    javax.swing.UIManager.setLookAndFeel(/* Previous, main Look and Feel */);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}

I'm using this code for giving JFileChooser another look and it works perfectly.

You just change Look and Feel before creating component, and restoring previous one, just after that creation.

︶ ̄淡然 2024-11-12 13:15:02

编辑
下面的代码更改了整个 JFrame 的 L&F。

static Main m;//Reference to JFrame to be updated
static String maxOSLookAndFeel = "ch.randelshofer.quaqua.QuaquaLookAndFeel";//Package of particular L&F    
private void MacOSLFjMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MacOSLFjMenuItemActionPerformed
                    // TODO add your handling code here:
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            try {
                                UIManager.setLookAndFeel(maxOSLookAndFeel);
                                SwingUtilities.updateComponentTreeUI(m);
                                m.validate();
                            } catch (ClassNotFoundException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (InstantiationException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (IllegalAccessException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (UnsupportedLookAndFeelException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });

                }//GEN-LAST:event_MacOSLFjMenuItemActionPerformed

第二:
在我看来(通过谷歌搜索并根据经验),在附加新的 L&F 时不可能只影响一个 JComponent。您可以在整个 JFrame 中更改 L&F,或者您可以 编写您自己的 Swing 组件。

实现目标的另一种方法是学习 java 源代码 并找到 JProgressBar 图像被添加到组件的位置,并通过扩展 JProgressBar 覆盖此方法。

Edit
Code below change the L&F of entire JFrame.

static Main m;//Reference to JFrame to be updated
static String maxOSLookAndFeel = "ch.randelshofer.quaqua.QuaquaLookAndFeel";//Package of particular L&F    
private void MacOSLFjMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MacOSLFjMenuItemActionPerformed
                    // TODO add your handling code here:
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            try {
                                UIManager.setLookAndFeel(maxOSLookAndFeel);
                                SwingUtilities.updateComponentTreeUI(m);
                                m.validate();
                            } catch (ClassNotFoundException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (InstantiationException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (IllegalAccessException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (UnsupportedLookAndFeelException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });

                }//GEN-LAST:event_MacOSLFjMenuItemActionPerformed

Secundo:
In my opinion (googled a lot and by expirience) it's impossible to affect only one JComponent while you attach a new L&F. You change your L&F in entire JFrame or you can Write your own Swing Component.

Another way to achive your goal is studying java source code and find place where JProgressBar image is beeing added to component and override this method by extending JProgressBar.

烟沫凡尘 2024-11-12 13:15:02

Java 外观和感觉的全面描述可以在 Sun Java 文档中找到,此处:Java、外观和感觉设计指南pdf 中的

A comprehensive description of Java look and feel could be found by Sun Java document, here: Java, Look and Feel Design Guidelines in pdf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文