如何在 Swing 应用程序中使用 FlatLaf 库?

Pio*_*otr 4 java swing netbeans

这是我的简单应用程序:

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);
}
}
Run Code Online (Sandbox Code Playgroud)

示例程序

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

tha*_*i57 7

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

Option 1: download files manually and install them

You can download and install the following files:

  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.

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 files are the main files you need to make things work. They are the files I mentioned above.
  2. pom file I think is a settings' file for Apache Maven build system.
  3. asc files are the PGP signatures.
  4. md5, sha1, sha256 and sha512 are the hashes. If you are familiar, you know what I am saying. So you should take a step back and verify the signatures and hashes of the main (jar) files you just downloaded.

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...

Option 2: sync with a build system

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.

Option 3: build from source, then install

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.

Installing

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. 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. 接下来是 JavaDoc 文件。在项目中创建一个类,并通过导入它并声明该导入类的示例的引用来使用库中的类。在类名上按CTRL+ SPACE...会弹出一个对话框,提示没有附加 JavaDoc,并为您提供了一个单击位置,其中显示“附加 JavaDoc...”或类似内容。单击它,然后单击“添加 ZIP/文件夹”按钮,选择 JavaDoc jar 文件并确认您的选择。现在已经准备好了。
  3. 最后是源文件。当您在代码中准备好类引用时,按住该键左键单击类名称CTRL。这将使您获得源代码。您将看到仅显示方法名称,并且每个方法主体都显示“编译代码”,如果您展开/展开它,您会在注释中看到字节码。这意味着未附加来源。您将看到已编译的 jar 文件的字节码。在编辑器的右上角,现在有一个按钮,上面写着Attach Sources...。单击它,然后弹出一个对话框。单击“Add Zip/Folder...”按钮,导航并选择flatlaf-1.1-sources.jar文件,您现在可以看到该库的源代码。

构建示例应用程序

您可以查阅GitHub页面,或指导您完成的项目主页。基本上你可以尝试以下方法:

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.setup();  //Must be called first of all Swing code as this sets the look and feel to FlatDark.
        
        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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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