如何在 Swing 应用程序中使用 FlatLaf 库? [英] How to use FlatLaf library in swing application?

查看:28
本文介绍了如何在 Swing 应用程序中使用 FlatLaf 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的简单应用程序:

Here is my simple application:

public class MainFrame extends JFrame{
    private Dimension size;
    private JPanel buttonPanel;
    private JButton button;

public MainFrame() throws ClassNotFoundException{
    super("Applikacja1");
   
    try {
        UIManager.setLookAndFeel("com.formdev.flatlaf.FlatDarkLaf");
    } catch (Exception ex) {
       
    }
    JFrame.setDefaultLookAndFeelDecorated(true);
    size = new Dimension(350, 530);
    setPreferredSize(size);
    setMinimumSize(size);
    setLocationRelativeTo(null);
    setLayout(new BorderLayout());
    pack();
    
    //setting button 
    button = new JButton("Start programu");
    button.setVisible(true);
    this.add(button);


    
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setResizable(false);
}
}

我需要做什么才能将此程序的外观更改为 FlatLaf 并使用它们的库?我需要从网上下载什么?我尝试下载整个代码(从这里 https://www.formdev.com/flatlaf/)接下来将它作为库添加到 netbeans 中,但它不起作用.有人可以做一个完全新手的教程吗?

What I need to do in order to change look of this program to FlatLaf and use their libraries? What i need to download from internet? I tried download whole code(from here https://www.formdev.com/flatlaf/) next add it as libraries to netbeans and it didn't work. Can someone do a little totally-newbie tutorial how to do it?

推荐答案

我不是这方面的专家,但这里有一些说明:

I am not an expert on this, but here are some instructions:

您可以下载并安装以下文件:

You can download and install the following files:

  1. 二进制文件:这是 Java 虚拟机执行的字节码.在您的案例中,此文件称为 flatlaf-1.1.jar.这是迄今为止的最新版本.这些文件通常(在这种情况下)只包含库名称和版本.如果您选择此选项,这是能够使用库所需的绝对必需文件.
  2. JavaDoc 文件:该文件包含每个类、方法、字段的文档注释以及开发人员随其代码提供的任何文档.在您的案例中,此文件称为 flatlaf-1.1-javadoc.jar.通常,开发人员会提供一个在线文档,该文档甚至可以以 HTML 格式下载,但事先下载此文件,即使离线,您也可以随时在 IDE(即项目编辑器,例如 NetBeans 或 IntelliJ)中查看文档.所以这个文件是一个很好的文件,但不是绝对必须的.
  3. 源文件:该文件包含每个类、方法、字段的源代码以及一般库的代码,捆绑在一个 jar 文件中.在您的案例中,此文件称为 flatlaf-1.1-sources.jar.如果您将您的项目与此源文件连接起来,您将受益于看到实现库的实际代码(不是二进制文件的 Java 字节码,而是 Java 纯文本源代码).这个文件又不是绝对强制性的,但如果文档中的某些内容不够清楚(或缺失),最好咨询一下.
  1. Binary file: this is the bytecode executed by the Java Virtual Machine. This file in your case is called flatlaf-1.1.jar. This is the latest release so far. Those files usually (as in this case) consist of just the library name and the verison. This is the absolute mandatory file needed to be able to use the library, if you select this option.
  2. JavaDoc file: this is the file containing the documenting comments of each class, method, field and generally whatever documentation the developers provide with their code. This file in your case is called flatlaf-1.1-javadoc.jar. Usally the developers provide an online documentation which is even downloadable in HTML format, but downloading this file beforehand, will let you see the docs any time in your IDE (ie project editor, such as NetBeans or IntelliJ) even when offline. So this file is a very good one, but not an absolute mandatory one.
  3. Sources file: this is the file containing the source code of each class, method, field and generally the code of the library, bundled in a jar file. This file in your case is called flatlaf-1.1-sources.jar. If you connect your project with this source's file you get the benefit of seeing the actual code (not Java bytecode as is the case of the binary, but Java plain text source code) that the library is implemented in. This file then is again not an absolute mandatory one, but it is a good one to consult if something is not clear enough (or is missing) from the documentation.

目前为止我提到的所有文件都可以在站点指导您找到它们的地方找到,例如您想要的库的最新版本文件可以一起找到这里.您可以通过以下方式找到此链接:首先导航到 FlatLaf 的主页,然后向下滚动一点在此页面上找到下载部分,该部分为您提供了另一个链接,即 Maven 存储库中的链接(特别是 这个 一个).在那里您可以找到有关如何将库链接到您的项目的说明,当然,还可以下载必要的文件.在这种情况下,右上角有两个按钮,一个叫做Browse,另一个叫做Downloads.下载按钮只是下拉您可以下载的所有资产(即文件)的列表.Browse 按钮将您导航到服务器的目录,您可以在该目录中找到 Downloads 按钮下拉列表中显示的所有文件,所以让我们去那里以便查看所有可用文件...

All the files I mentioned so far can be found where the site guides you to find them, for example the latest release files of your desired library can be found all together here. You can find this link as follows: first navigate to the FlatLaf's home page, then scroll a bit down on this page to find the section Download which gives you another link, the one in the Maven repository (specifically this one). There you can find instructions on how to link the library to your project and, of course, download the necessary files. In this case, there are two buttons on the top right, the one called Browse and the other called Downloads. The Downloads button simply drops down a list of all assets (ie files) that you can download. The Browse button navigates you to the directory of the server where you can find all the files presented with the drop down list of the Downloads button, so let's go there in order to see all the available files...

如果你去那里,你可以看到更多的文件.它们中的大多数对安全性很重要,我还没有涵盖.验证您下载的文件的完整性始终很重要,因为如果不这样做,就会存在安全风险.例如,您不希望下载的库是由网络钓鱼站点提供的恶意副本.因此,您需要将它们与您正在访问的链接一起验证.诚然,我在下载库时并不经常这样做,但我觉得,我对这样做的疏忽,并没有使它成为我不应该提及的事情.所以我警告你:注意你正在下载的内容.

If you went there, you can see many more files. Most of them are imporant for security, which something I did not cover yet. It is always important to verify the integrity of the files you download, because there are security risks if you don't. You don't want the library you download to be a malicious copy provided by a phising site for example. So you need to verify them together with the link you are visiting. Admitedly, I am not doing this very often, when downloading libraries, but I feel that, my neglectness of doing it, does not make it something I should not mention. So I am warning you: be aware of what you are downloading.

让我们休息一下,看看所有这些文件(最有可能)是什么:

Let's have a break to see what all those files (most likely) are:

  1. jar 文件是使工作正常运行所需的主要文件.它们是我上面提到的文件.
  2. 我认为 pom 文件是 Apache Maven 构建系统的设置文件.
  3. asc 文件是 PGP 签名.
  4. md5、sha1、sha256 和 sha512 是哈希值.如果你熟悉,你就会知道我在说什么.因此,您应该退后一步,验证您刚刚下载的主(jar)文件的签名和哈希值.

我认为验证完整性的最佳方法是简单地下载源代码,自己验证并通过提供的资源验证,然后构建表单源(如果您信任您的编译器)并安装.但这不是那么简单,也不是保存时间.我提供的所有链接都可能包含恶意内容:我只是不知道,因为我还没有花必要的时间来验证.因此,正如他们通常所说的那样,请自行承担风险.另请注意,我在下载 jar 文件时收到警告,通常是因为它们不受信任".正如消息所说.继续……

Best way to verify the integrity in my opinion is to simply download the source code, verify it yourself and via the provided resources, then build form source (if you trust your compiler) and install. But that's not so simple, nor time preserving. All those links I provide may contain malicious content: I just cannot know yet because I haven't spent the necessary time to verify. So continue at your own risk, as they usually say. Also note I am getting a warning when downloading jar files generally because "they are not trusted" as the message says. Moving on...

如果您有一个系统来自动化项目的构建过程(而不是使用 javac 命令行实用程序编译项目的每个 Java 源代码文件),例如 Apache MavenGradlea>、Apache Ant 等,然后您可以选择在您的项目并让构建系统同步(即下载并在构建过程中包含库).这至少是我有点熟悉的 Gradle 发生的事情.至于其他人,我不是很熟悉,所以这就是我不再为他们说话的地方.请注意,有一些关于如何使用它们的说明、教程和文档,可以直接通过命令行,也可以通过您的 IDE.然后据我所知,库开发人员通常会让你知道如何在构建工具的设置文件中包含相应的依赖项.

If you have a system to automate the building process of your project (instead of compiling each and every Java source code file of your project with javac command line utility), such as Apache Maven, Gradle, Apache Ant and others, then you have the option to include a so called dependecy of the library in your project and let the build system synchronize (ie download and include the library in the build process). This is at least what happens with Gradle which I am a little familiar with. As for the others I am not really familiar, so this is where I stop talking for them. Just note that there are several instructions and tutorials and documentation about how to use them, either via command line directly, or via your IDE. Then the library developers as far as I know usually they let you know how to include the corresponding dependency in the build tool's settings file.

您可以从 GitHub、有时通过 FTP 站点或通常从提供相应内容的某种在线服务器下载(至少是该软件的)源代码,并构建它.您想要的库的源代码位于这里,从问题来看,您可能已经发现.

You can download the source code (of this software at least) from GitHub, or sometimes by FTP sites or generally from some online server of some sort which provides the corresponding content, and build it. The source code of your desired library is located here, as you have probably found out, judging from the question.

现在您下载(并验证)或构建了您需要的东西,实际安装如下...我将演示 NetBeans 8.2 IDE with Java 8(FlatLaf 支持的最低版本),但我猜每个 IDE 和版本的步骤通常是相同的.如果不是,您可以更具体地说明您的环境,然后我们可以再次讨论.

So now that you downloaded (and verified), or built what you needed, the actual installation is as follows... I am going to demonstrate for the NetBeans 8.2 IDE with Java 8 (the minimum supported version for FlatLaf), but the steps I guess are generally the same for each IDE and version. If they are not, you can be more specific about your environment and then we can talk again.

  1. 在您的项目中包含已编译的库 (flatlaf-1.1.jar),如下所示:右键单击 NetBeans 的 Projects 选项卡中的项目(通常在左侧)IDE 的)并转到名为 Properties 的最底部按钮并单击它.在弹出的对话框中,从对话框左侧的 Categories 部分,选择名为 Libraries 的类别.从中间选择选项卡Compile,然后单击右侧的Add JAR/Folder.然后选择您的库文件并单击打开以包含它.就是这样.您现在基本上已准备好开始将 FlatLaf 的类包含到您的项目中,因为正如我上面所说,这个文件是绝对必需的.
  2. 接下来是 JavaDoc 文件.在项目中创建一个类,并通过导入类并声明该导入类的引用来使用库中的类.在类名上按 CTRL+SPACE...弹出一个对话框,说没有附加 JavaDoc,并为您提供一个单击位置,上面写着附加 JavaDoc..."或类似的东西.单击它,然后单击添加 ZIP/文件夹 按钮,选择 JavaDoc jar 文件并确认您的选择.现在准备好了.
  3. 最后是源文件.准备好代码中的类引用后,按住 CTRL 键左键单击类名.这将使您获得源代码.你会看到只显示了方法名称,每个方法主体都说Compiled Code,如果你展开/展开它,你会在注释中看到字节码.这意味着没有附加来源.您正在看到编译后的 jar 文件的字节码.在编辑器的右上角,现在有一个按钮,上面写着Attach Sources....单击它,然后弹出一个对话框.单击按钮添加 Zip/文件夹...,导航并选择 flatlaf-1.1-sources.jar 文件,您现在可以看到库的源代码.
  1. Include the compiled library (flatlaf-1.1.jar) in your project, like so: right click the project from the Projects tab of NetBeans (usually to the left of the IDE) and go to the bottom most button named Properties and click it. In the dialog that pop's up, from the Categories section, to the left of the dialog, select the category named Libraries. Select the tab Compile from the middle, and click on Add JAR/Folder to the right. Then go select your library file and click Open to include it. That's it. You are basically now ready to start including the FlatLaf's classes to your project, because this file was the absolute mandatory one, as I said above.
  2. Next, the JavaDoc file. Create a class in the project and use a class from the library by importing it and declaring a reference for example of that imported class. Press CTRL+SPACE on the class name... A dialog pops up saying there is no JavaDoc attached, and gives you a place to click which says "Attach JavaDoc..." or something similar. Click it, and then click on the Add ZIP/Folder button, select the JavaDoc jar file and confirm your choice. This is now ready.
  3. Finally the Sources' file. As you have ready the class reference in your code, left click the class name with holding the CTRL key. This will get you to the source code. You will see that only the methods names are shown and every method body says Compiled Code and if you unfold/expand it, then you see the bytecode in comments. That means the sources are not attached. You are seeing the bytecode of the compiled jar file. On the top right of the editor, there is now a button which reads Attach Sources.... Click it, and then a dialog pops up. Click the button Add Zip/Folder..., navigate and select the flatlaf-1.1-sources.jar file and you can now see the source code of the library.

构建示例应用

您可以参考 GitHub 页面,或指导项目的主页你通过.基本上您可以尝试以下操作:

Building a sample application

You can consult the GitHub page, or the the project's main page which guides you through. Basically you can try the following:

import com.formdev.flatlaf.FlatDarkLaf;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Main {
    
    private static void createAndShowGUI() {
        FlatDarkLaf.install(); //Must be called first of all Swing code.
        
        final JPanel panel = new JPanel(); //FlowLayout.
        panel.add(new JButton("FlatDarkLaf button!"));
        panel.add(new JTextField("FlatDarkLaf text field!"));
        
        final JFrame frame = new JFrame("FlatDarkLaf test.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(Main::createAndShowGUI);
    }
}

最后一点:您必须在创建任何 Swing 组件之前应用 LAF.在您问题中的代码中,您似乎不是,因为您首先创建了 JFrame(通过构造函数的 super 调用),然后以同样的方式安装 LAF不推荐.

Final note: you must apply your LAF before any Swing component creation. In the code in your question seems like you are not, because you first create the JFrame (via constructor's super call) and then you install the LAF in a manner which is also not recommended.

这篇关于如何在 Swing 应用程序中使用 FlatLaf 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆