当前位置: 首页 > 面试题库 >

更改特定窗口的外观

应嘉容
2023-03-14
问题内容

我正在为更大的GUI应用程序编写脚本。主应用程序窗口使用系统的窗口LookAndFeel,但我希望脚本的GUI使用Nimbus
LookAndFeel。创建GUI后,我想将其设置LookAndFeel回原始。我觉得下面的SSCCE应该可以工作,但是我NullPointerException在使用Component对象时遇到了麻烦。

import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class GUI extends JFrame {
    private static LookAndFeel originalLookAndFeel = UIManager.getLookAndFeel();
    static {
        System.out.println("At start, look and feel is " + UIManager.getLookAndFeel().getName());
        try {
            setNimbusLookAndFeel();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.println("Look and feel changed to " + UIManager.getLookAndFeel().getName()
                + " before component creation");
    }
    private GridBagLayout gridBag = new GridBagLayout();
    private JTabbedPane tabs = new JTabbedPane();
    private JPanel selectionPanel = new JPanel(gridBag);
    private JPanel infoPanel = new JPanel(gridBag);
    private JPanel settingsPanel = new JPanel(gridBag);

    public GUI() {
        setWindowProperties();
        setUpComponents();
        addComponents();

        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();
        }
    }

    private void setWindowProperties() {
        setLayout(gridBag);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(700, 600));
        setTitle("fAmos Quester");
        setResizable(false);
        setLocationRelativeTo(null);
    }

    private static 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 GUI().setVisible(true);
    }
}

问题答案:

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

Nimbus LAF中有某些内容可能不允许您执行此操作。按原样运行代码。它将LAF设置为System LAF,然后重置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 = new GridBagLayout();
    private JTabbedPane tabs = new JTabbedPane();
    private JPanel selectionPanel = new JPanel(gridBag);
    private JPanel infoPanel = new JPanel(gridBag);
    private JPanel settingsPanel = new JPanel(gridBag);
*/
    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来处理已激活/已禁用的事件。



 类似资料:
  • 我写了一个小GUI程序与python在tkinter的窗口。我的窗口必须在全屏游戏窗口的前面。 此刻,我用这句话: 它适用于普通窗口(浏览器,浏览器,...),但如果我启动游戏到全屏模式,我的窗口隐藏在游戏后面。 为什么会发生这种情况?调用游戏可能类似于覆盖我的属性的? 我的问题还有别的解决办法吗?也许可以告诉windows,我的窗口应该在特定窗口(游戏窗口)的前面?

  • 问题内容: 我有一个带有一些外键的表,我需要获取这些键何时更改的报告。 SELECT from,to,FIRST(timestamp)FROM表GROUP BY from,to; 我可以使用“分组依据”来获取前两个过渡,但是它将第三个与第一个过渡组合在一起,当它返回时我看不到它。 我想进行以下查询: 是否可以? 问题答案: 在PostgreSQL 8.4中,您可以使用窗口函数LAG访问上一行并进行

  • 问题内容: 我正在使用Bootstrap 3设计页面。我试图在输入元素上使用带有popover的页面。新的Bootstrap可确保您在使用时基本上具有全角输入元素。 HTML代码如下所示: 我认为popover的宽度太小,因为它们在div中没有​​剩余的任何宽度。我想在左侧输入表单,在右侧显示宽弹出窗口。 通常,我正在寻找无需覆盖Bootstrap的解决方案。 附加的JsFiddle。第二个输入选

  • 问题内容: 基本上是弹出用于JComboBox中显示其衍生的JTextField下面,如何从怒吼取向为JcomboBox的弹出改变方向并在顶/显示JcomboBox的弹出超过该 编辑:基本JComboBox的代码示例 编辑第二。MacOX的代码 问题答案: 尝试在“ 组合框弹出”中找到的setPopupAbove()方法。

  • 我有两台显示器;创建 LWJGL 窗口时: 它总是出现在我的左侧屏幕上。是否有一个参数可以设置以更改它出现在哪个屏幕上,例如:

  • 大多数情况下,我在寻找一个不用重写引导的解决方案。 附加的jsfiddle。第二个输入选项。没有经常使用jsfiddle,所以不知道,但尝试增加输出框的大小来查看结果,在更小的屏幕上甚至看不到。http://jsfiddle.net/rqx8t/