更改特定窗口的外观

更改特定窗口的外观,第1张

更改特定窗口的外观

一般来说,混合LAF并不是一个好主意。这个问题就是一个例子。

Nimbus LAF中有某些内容可能不允许您执行此 *** 作。按原样运行代码。它将LAF设置为

SystemLAF
,然后重置LAF。在我的情况下,该系统是Windows,并且运行正常。然后更改代码以使用Nimbus
LAF。它最初似乎可以正常工作,但是尝试调整框架的大小会出现错误。因此,似乎Nimbus框架无法完全独立于当前的LAF运作。

import java.awt.*;import java.awt.event.*;import java.awt.GridBagLayout;import javax.swing.*;import javax.swing.UIManager.LookAndFeelInfo;public class GUI2 extends Jframe {    private static LookAndFeel originalLookAndFeel = UIManager.getLookAndFeel();    private GridBagLayout gridBag;    private JTabbedPane tabs;    private JPanel selectionPanel;    private JPanel infoPanel;    private JPanel settingsPanel;    public GUI2() {        System.out.println("At start, look and feel is " + UIManager.getLookAndFeel().getName());        try {// setNimbusLookAndFeel();          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        } catch (Exception e) { e.printStackTrace();        }        System.out.println("Look and feel changed to " + UIManager.getLookAndFeel().getName()     + " before component creation");        gridBag = new GridBagLayout();        setLayout(gridBag);        tabs = new JTabbedPane();        selectionPanel = new JPanel(gridBag);        infoPanel = new JPanel(gridBag);        settingsPanel = new JPanel(gridBag);        setUpComponents();        addComponents();        setWindowProperties();        Action reset = new AbstractAction()        { public void actionPerformed(ActionEvent ae) {     try {         System.out.println("Setting to original, which is " + originalLookAndFeel.getName());         UIManager.setLookAndFeel(originalLookAndFeel);         System.out.println("Current look and feel is " + UIManager.getLookAndFeel().getName());     } catch (UnsupportedLookAndFeelException e) {         //e.printStackTrace();         System.out.println(e.getMessage());     } }        };        Timer timer = new Timer(500, reset);        timer.setRepeats(false);        timer.start();    }    private void setWindowProperties() {//        setLayout(gridBag);        setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        setTitle("fAmos Quester");//        setResizable(false);        pack();        setLocationRelativeTo(null);    }    private void setNimbusLookAndFeel() {        try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {     if ("Nimbus".equals(info.getName())) {         UIManager.setLookAndFeel(info.getClassName());     } }        } catch (Exception e) { try {     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e2) { }        }    }    public void setUpComponents() {        tabs.addTab("Quest selection", selectionPanel);        tabs.addTab("Quest info", infoPanel);        tabs.addTab("Settings", settingsPanel);        selectionPanel.setPreferredSize(new Dimension(650, 500));        infoPanel.setPreferredSize(new Dimension(650, 500));        settingsPanel.setPreferredSize(new Dimension(650, 500));    }    private void addComponents() {        add(tabs);    }    public static void main(String[] args) {        new GUI2().setVisible(true);    }}

也许一个解决方案是像上面那样使用Nimbus
LAF创建组件。但是,在停用框架之前,请勿重置LAF。然后,您可以尝试在每次激活框架时重置LAF。您将使用WindowListener来处理已激活/已禁用的事件。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5499722.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存