如何更改 Netbeans 中的 Swing 外观

发布于 2024-08-26 22:49:09 字数 144 浏览 8 评论 0 原文

我是 netbeans ide 的新手,我有一个 swing 桌面应用程序,我想将默认的 java 外观和感觉(为其)更改为物质外观和感觉(或其他),那么如何将物质 jar 文件添加到我的项目中 (我想将项目部署到 jar 文件),并从中设置外观。

谢谢

i am new to netbeans ide , i have a swing desktop application and i want to change the default java look and feel (for it) to substance look and feel ( or other) , so how to add substance jar file to my project
(i want to deploy the project to jar file) , and set a look and feel from it .

thanks

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

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

发布评论

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

评论(4

天荒地未老 2024-09-02 22:49:09

首先尝试此代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame {
public Test() {
    initComponents();
}

private void initComponents() {
    // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    menuBar1 = new JMenuBar();
    menu1 = new JMenu();
    menuItem5 = new JMenuItem();
    menuItem4 = new JMenuItem();
    checkBoxMenuItem1 = new JCheckBoxMenuItem();
    menuItem3 = new JMenuItem();
    menu2 = new JMenu();
    menuItem6 = new JMenuItem();
    tabbedPane1 = new JTabbedPane();
    panel1 = new JPanel();
    scrollPane1 = new JScrollPane();
    textPane1 = new JTextPane();
    button1 = new JButton();
    button2 = new JButton();
    button3 = new JButton();
    scrollPane2 = new JScrollPane();
    textArea1 = new JTextArea();
    scrollPane3 = new JScrollPane();
    tree1 = new JTree();
    progressBar1 = new JProgressBar();
    radioButton1 = new JRadioButton();
    checkBox1 = new JCheckBox();
    panel2 = new JPanel();
    panel3 = new JPanel();

    Container contentPane = getContentPane();
    contentPane.setLayout(null);
    {
        {
            menu1.setText("text");
            menuItem5.setText("text");
            menu1.add(menuItem5);
            menuItem4.setText("text");
            menu1.add(menuItem4);
            menu1.addSeparator();
            checkBoxMenuItem1.setText("text");
            menu1.add(checkBoxMenuItem1);
            menuItem3.setText("text");
            menu1.add(menuItem3);
        }
        menuBar1.add(menu1);
        {
            menu2.setText("text");
            menuItem6.setText("text");
            menu2.add(menuItem6);
        }
        menuBar1.add(menu2);
    }
    setJMenuBar(menuBar1);
    {

        {
            panel1.setLayout(null);

            {
                scrollPane1.setViewportView(textPane1);
            }
            panel1.add(scrollPane1);
            scrollPane1.setBounds(15, 15, 665, scrollPane1.getPreferredSize().height);
            button1.setText("text");
            panel1.add(button1);
            button1.setBounds(15, 45, 300, button1.getPreferredSize().height);
            button2.setText("text");
            panel1.add(button2);
            button2.setBounds(325, 45, 140, 23);
            button3.setText("text");
            panel1.add(button3);
            button3.setBounds(470, 45, 210, 23);
            {
                scrollPane2.setViewportView(textArea1);
            }
            panel1.add(scrollPane2);
            scrollPane2.setBounds(15, 75, 665, 175);
            {
                scrollPane3.setViewportView(tree1);
            }
            panel1.add(scrollPane3);
            scrollPane3.setBounds(15, 260, 140, 150);
            progressBar1.setValue(40);
            panel1.add(progressBar1);
            progressBar1.setBounds(160, 260, 520, 20);
            radioButton1.setText("text");
            panel1.add(radioButton1);
            radioButton1.setBounds(160, 290, 100, radioButton1.getPreferredSize().height);
            checkBox1.setText("text");
            panel1.add(checkBox1);
            checkBox1.setBounds(265, 295, 165, checkBox1.getPreferredSize().height);

            {
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        tabbedPane1.addTab("text", panel1);
        {
            panel2.setLayout(null);

            {
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        tabbedPane1.addTab("text", panel2);

        {
            panel3.setLayout(null);

            {
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        tabbedPane1.addTab("text", panel3);

    }
    contentPane.add(tabbedPane1);
    tabbedPane1.setBounds(10, 10, 700, 450);

    {
        Dimension preferredSize = new Dimension();
        for(int i = 0; i < contentPane.getComponentCount(); i++) {
            Rectangle bounds = contentPane.getComponent(i).getBounds();
            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
        }
        Insets insets = contentPane.getInsets();
        preferredSize.width += insets.right;
        preferredSize.height += insets.bottom;
        contentPane.setMinimumSize(preferredSize);
        contentPane.setPreferredSize(preferredSize);
    }
    pack();
    setLocationRelativeTo(getOwner());

}

private JMenuBar menuBar1;
private JMenu menu1;
private JMenuItem menuItem5;
private JMenuItem menuItem4;
private JCheckBoxMenuItem checkBoxMenuItem1;
private JMenuItem menuItem3;
private JMenu menu2;
private JMenuItem menuItem6;
private JTabbedPane tabbedPane1;
private JPanel panel1;
private JScrollPane scrollPane1;
private JTextPane textPane1;
private JButton button1;
private JButton button2;
private JButton button3;
private JScrollPane scrollPane2;
private JTextArea textArea1;
private JScrollPane scrollPane3;
private JTree tree1;
private JProgressBar progressBar1;
private JRadioButton radioButton1;
private JCheckBox checkBox1;
private JPanel panel2;
private JPanel panel3;

public static void main(String args[]){

    new Test().setVisible(true);


  }
}

您还可以使用 Nimbus 主题,修改为此代码:

try{
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

    }
    catch(Exception e){
        System.out.println("Nimbus isn't available");
    }

并为此编辑主项目:

public static void main(String args[]){

    try{
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

    }
    catch(Exception e){
        System.out.println("Nimbus isn't available");
    }
    new Test().setVisible(true);


}

参考:
forum.codecall.net

First try this code :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame {
public Test() {
    initComponents();
}

private void initComponents() {
    // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    menuBar1 = new JMenuBar();
    menu1 = new JMenu();
    menuItem5 = new JMenuItem();
    menuItem4 = new JMenuItem();
    checkBoxMenuItem1 = new JCheckBoxMenuItem();
    menuItem3 = new JMenuItem();
    menu2 = new JMenu();
    menuItem6 = new JMenuItem();
    tabbedPane1 = new JTabbedPane();
    panel1 = new JPanel();
    scrollPane1 = new JScrollPane();
    textPane1 = new JTextPane();
    button1 = new JButton();
    button2 = new JButton();
    button3 = new JButton();
    scrollPane2 = new JScrollPane();
    textArea1 = new JTextArea();
    scrollPane3 = new JScrollPane();
    tree1 = new JTree();
    progressBar1 = new JProgressBar();
    radioButton1 = new JRadioButton();
    checkBox1 = new JCheckBox();
    panel2 = new JPanel();
    panel3 = new JPanel();

    Container contentPane = getContentPane();
    contentPane.setLayout(null);
    {
        {
            menu1.setText("text");
            menuItem5.setText("text");
            menu1.add(menuItem5);
            menuItem4.setText("text");
            menu1.add(menuItem4);
            menu1.addSeparator();
            checkBoxMenuItem1.setText("text");
            menu1.add(checkBoxMenuItem1);
            menuItem3.setText("text");
            menu1.add(menuItem3);
        }
        menuBar1.add(menu1);
        {
            menu2.setText("text");
            menuItem6.setText("text");
            menu2.add(menuItem6);
        }
        menuBar1.add(menu2);
    }
    setJMenuBar(menuBar1);
    {

        {
            panel1.setLayout(null);

            {
                scrollPane1.setViewportView(textPane1);
            }
            panel1.add(scrollPane1);
            scrollPane1.setBounds(15, 15, 665, scrollPane1.getPreferredSize().height);
            button1.setText("text");
            panel1.add(button1);
            button1.setBounds(15, 45, 300, button1.getPreferredSize().height);
            button2.setText("text");
            panel1.add(button2);
            button2.setBounds(325, 45, 140, 23);
            button3.setText("text");
            panel1.add(button3);
            button3.setBounds(470, 45, 210, 23);
            {
                scrollPane2.setViewportView(textArea1);
            }
            panel1.add(scrollPane2);
            scrollPane2.setBounds(15, 75, 665, 175);
            {
                scrollPane3.setViewportView(tree1);
            }
            panel1.add(scrollPane3);
            scrollPane3.setBounds(15, 260, 140, 150);
            progressBar1.setValue(40);
            panel1.add(progressBar1);
            progressBar1.setBounds(160, 260, 520, 20);
            radioButton1.setText("text");
            panel1.add(radioButton1);
            radioButton1.setBounds(160, 290, 100, radioButton1.getPreferredSize().height);
            checkBox1.setText("text");
            panel1.add(checkBox1);
            checkBox1.setBounds(265, 295, 165, checkBox1.getPreferredSize().height);

            {
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        tabbedPane1.addTab("text", panel1);
        {
            panel2.setLayout(null);

            {
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        tabbedPane1.addTab("text", panel2);

        {
            panel3.setLayout(null);

            {
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        tabbedPane1.addTab("text", panel3);

    }
    contentPane.add(tabbedPane1);
    tabbedPane1.setBounds(10, 10, 700, 450);

    {
        Dimension preferredSize = new Dimension();
        for(int i = 0; i < contentPane.getComponentCount(); i++) {
            Rectangle bounds = contentPane.getComponent(i).getBounds();
            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
        }
        Insets insets = contentPane.getInsets();
        preferredSize.width += insets.right;
        preferredSize.height += insets.bottom;
        contentPane.setMinimumSize(preferredSize);
        contentPane.setPreferredSize(preferredSize);
    }
    pack();
    setLocationRelativeTo(getOwner());

}

private JMenuBar menuBar1;
private JMenu menu1;
private JMenuItem menuItem5;
private JMenuItem menuItem4;
private JCheckBoxMenuItem checkBoxMenuItem1;
private JMenuItem menuItem3;
private JMenu menu2;
private JMenuItem menuItem6;
private JTabbedPane tabbedPane1;
private JPanel panel1;
private JScrollPane scrollPane1;
private JTextPane textPane1;
private JButton button1;
private JButton button2;
private JButton button3;
private JScrollPane scrollPane2;
private JTextArea textArea1;
private JScrollPane scrollPane3;
private JTree tree1;
private JProgressBar progressBar1;
private JRadioButton radioButton1;
private JCheckBox checkBox1;
private JPanel panel2;
private JPanel panel3;

public static void main(String args[]){

    new Test().setVisible(true);


  }
}

You can also use Nimbus theme, modified to this code :

try{
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

    }
    catch(Exception e){
        System.out.println("Nimbus isn't available");
    }

And edit the main project for this :

public static void main(String args[]){

    try{
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

    }
    catch(Exception e){
        System.out.println("Nimbus isn't available");
    }
    new Test().setVisible(true);


}

Reference :
forum.codecall.net

孤单情人 2024-09-02 22:49:09

您可以将 .jar 放置在模块包装器中,就像将任何 .jar 文件放置在 NetBeans 平台应用程序中一样。至于设置外观,您可能需要扩展 ModuleInstall,具体来说,用如下内容重写 returned() 方法:

    try {
      UIManager.setLookAndFeel(new SubstanceRavenGraphiteLookAndFeel());
    } catch (Exception e) {
      System.out.println("Substance Raven Graphite failed to initialize");
    }

You place the .jar in a module wrapper, as you would any .jar file in a NetBeans Platform application. As for setting the look and feel, you might want to extend ModuleInstall, specifically, override the restored() method with something like this:

    try {
      UIManager.setLookAndFeel(new SubstanceRavenGraphiteLookAndFeel());
    } catch (Exception e) {
      System.out.println("Substance Raven Graphite failed to initialize");
    }
青萝楚歌 2024-09-02 22:49:09

抱歉,如果是旧的,但在 Netbeans 上,您可以单击项目上的辅助按钮(右键单击),然后转到“属性”。

接下来转到应用程序>桌面应用程序,您可以在那里更改外观:)。

Sorry if is old but on Netbeans you can click with the secondary button on your project (right click), and go to Properties.

Next go to Appliation>Desktop App and there you can change the look and feel :).

逆蝶 2024-09-02 22:49:09

如果您想在应用程序中使用 Substance LookAndFeel,简单的步骤是将 SubstanceLookAndFeel jar 添加到库类路径中(在 NetBeans IDE 上,只需右键单击库节点,然后添加 Substance.jar)。添加 Substance.jar 后,在主应用程序中只需添加以下代码(在启动主框架之前):

    public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(new SubstanceLookAndFeel());
            } 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);
            }
            new MainFrame();
        }
    });
}

如果您也希望 NetBeans IDE 使用 Substance Look And Feel,只需将 SubstanceLookAndFeel.jar 放在特定目录中(例如,在 /home/ 中) your-username/LAF/SubstanceLookAndFeel.jar) 并使用以下命令启动 NetBeans IDE:

$ netbeans --cp:p /home/your-username/LAF/SubstanceLookAndFeel.jar --laf org.jvnet.substance.SubstanceLookAndFeel 

If you want use Substance LookAndFeel in your application, the easy step is add SubstanceLookAndFeel jar into your library classpath (On NetBeans IDE just right click on library node, then add Substance.jar). After you add substance.jar, in your main application just add this code (before you launch main frame) :

    public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(new SubstanceLookAndFeel());
            } 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);
            }
            new MainFrame();
        }
    });
}

If you want NetBeans IDE using Substance Look And Feel too, just put SubstanceLookAndFeel.jar in specific directory (ex. in /home/your-username/LAF/SubstanceLookAndFeel.jar) and launch NetBeans IDE with this command :

$ netbeans --cp:p /home/your-username/LAF/SubstanceLookAndFeel.jar --laf org.jvnet.substance.SubstanceLookAndFeel 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文