简体   繁体   English

如何在 Netbeans 中运行 Java Swing?

[英]How to run Java Swing in Netbeans?

I've been trying to run gui in netbeans using java swing but it won't run at all.我一直在尝试使用 java swing 在 netbeans 中运行 gui,但它根本不会运行。 All the projects that I execcute that uses jframe does not run.我执行的所有使用 jframe 的项目都没有运行。 Any tips on how to fix this thing?有关如何解决此问题的任何提示? This is how it looks like:这是它的样子: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

I got this to run with no problems using NetBeans IDE 8.2.我使用 NetBeans IDE 8.2 让它运行没有问题。 Let's start from the beginning.让我们从头开始。

Delete your current OfficeManagementSystem project from your IDE and local drive system.从 IDE 和本地驱动系统中删除当前的OfficeManagementSystem项目。 Select the OfficeManagementSystem project located within your IDE Project pane located on the left side of the IDE. Select OfficeManagementSystem项目位于 IDE 项目窗格中,位于 IDE 的左侧。 Right-click on it and select the Delete popup menu item.右键单击它并 select删除弹出菜单项。 A Delete Project dialog is displayed.将显示删除项目对话框。 Check-mark the Also delete sources under..... checkbox then select the Yes button.勾选Also delete sources under.....复选框,然后 select按钮。

Close NetBeans!关闭 NetBeans!

To make sure all is gone, open your file browser (in Windows10 this is named File Explorer [ win key + E ]) and navigate to your NetBeans Projects folder.为确保一切就绪,请打开文件浏览器(在 Windows10 中,这称为文件资源管理器[ win key + E ])并导航到 NetBeans 项目文件夹。 Default location in Windows is C:\Users\{yourAccountName}\Documents\NetBeansProjects\ . Windows 中的默认位置是C:\Users\{yourAccountName}\Documents\NetBeansProjects\ Make sure that OfficeManagementSystem project folder is deleted.确保删除OfficeManagementSystem项目文件夹。

With your web browser, go to the Source Code Web Page and select the Code button:使用您的 web 浏览器,go 到源代码 Web 页面Z99938282F04071852ZEFE按钮:

在此处输入图像描述

Then from the dropdown menu select Download ZIP :然后从下拉菜单 select下载 ZIP

在此处输入图像描述

A Save File dialog will appear possibly similar to this:可能会出现一个保存文件对话框,类似于:

在此处输入图像描述

Select the Save File option then select the OK button. Select保存文件选项然后 select确定按钮。 Choose a location to save the ZIP file and remember it.选择一个位置来保存 Z4348F938BDDDD8475E967CCB47ECB234Z 文件并记住它。

Navigate to you downloaded file named HospitalManagementSystemusingJAVASwing-master.zip and Un-Zip it with your favorite decompression software like WinRar or 7Zip .导航到您下载的名为HospitalManagementSystemusingJAVASwing-master.zip 的文件,然后使用您喜欢的解压缩软件如WinRar7Zip解压缩它。 Have it unzip into its own folder .将其解压缩到自己的文件夹中。

Navigate to and open the the folder created by your decompression software, it should have been named HospitalManagementSystemusingJAVASwing-master .导航到并打开你的解压软件创建的文件夹,它应该被命名为HospitalManagementSystemusingJAVASwing-master If done properly you should have yet another folder (a sub-folder) also named HospitalManagementSystemusingJAVASwing-master .如果操作正确,您应该还有另一个文件夹(子文件夹),也名为HospitalManagementSystemusingJAVASwing-master Rename this folder to HospitalManagementSystem by right-clicking on the folder and selecting Rename from the popup menu.通过右键单击文件夹并从弹出菜单中选择命名,将此文件夹重命名为HospitalManagementSystem Delete the unnecessary text within the name then hit your ENTER key.删除名称中不必要的文本,然后按ENTER键。

After Renaming the folder, once again right-click on it and select Copy from the popup menu.重命名文件夹后,再次右键单击它并从弹出菜单中复制select。

As described earlier, navigate to you NetBeansProjects directory (folder) and double-click on it.如前所述,导航到您的NetBeansProjects目录(文件夹)并双击它。 Paste the new HospitalManagementSystem folder into the NetBeansProjects folder.将新的HospitalManagementSystem文件夹粘贴到NetBeansProjects文件夹中。 This project will now be added to the NetBeans Projects List.该项目现在将添加到 NetBeans 项目列表中。

Start NetBeans!启动 NetBeans!

Once NetBeans has completely started, from the menu bar select the File ➞ Open Project... menu item.一旦 NetBeans 完全启动,从菜单栏 select文件 ➞ 打开项目...菜单项。 The Open Project dialog will be displayed.将显示打开项目对话框。 Scroll down the left pane of this dialog until you see HospitalManagementSystem and select it.向下滚动此对话框的左窗格,直到看到HospitalManagementSystem和 select。 Now hit the Open Project button.现在点击打开项目按钮。 This project will load into the NetBeans IDE Projects pane located on the left side of your IDE under the name of: OfficeManagementSystem .此项目将加载到 NetBeans IDE 项目窗格中,该窗格位于 IDE 左侧,名称为: OfficeManagementSystem Why this person changed the project name at this point is beyond me but to each their own.为什么这个人在这一点上更改了项目名称,这超出了我的理解,而是每个人自己的问题。

Expand the project and its active package (officemanagementsystem) so as to expose all the classes involved.展开项目及其活动的 package(officemanagementsystem),以便公开所有涉及的类。 You should see the following listed:您应该会看到以下列表:

LoginPage.java               // The first Form we want displayed.
MenuPage.java                // The Menu page (once login is achieved).
Module1.java                 // Should really be named PatientPage
Module2.java                 // Should really be named DoctorAvailabilityPage.
OfficeManagementSystem.java  // The application start-up class.

This application still will not run since it contains nothing within the main() method of the start-up class ( OfficeManagementSystem ).此应用程序仍然不会运行,因为它在启动 class ( OfficeManagementSystem ) 的main()方法中不包含任何内容。 Open this class into the IDE Editor by double-clicking on it.双击此 class 到 IDE 编辑器中打开它。

Expand the main() method code block and add this code line into it:展开main()方法代码块并将以下代码行添加到其中:

new LoginPage().setVisible(true);

Run the application!运行应用程序! The first Window displayed will be the Login Page.显示的第一个 Window 将是登录页面。 Username is: admin and the password is: admin .用户名是: admin ,密码是: admin (change in code if you like). (如果您愿意,可以更改代码)。 And away you go.离开你 go。 :) :)

All java applications need a static main method as a bootstrap.所有 java 应用程序都需要一个 static 主方法作为引导程序。 It should look something like the following:它应该如下所示:

public static void main(String[] args) { YourJFrameClass frame = new YourJFrameClass(); }

That should get you started.那应该让你开始。

The easiest method is to press Shift+F11 (Clean and Build Project), then hit F6 to run it.最简单的方法是按 Shift+F11(清理并构建项目),然后按 F6 运行它。

It refreshes Netbeans appropriately and finds your main without all the manual labor;它会适当地刷新 Netbeans 并找到您的主要内容,而无需所有手动操作; and if you have multiple mains, it will give you the option to select the correct one.如果您有多个电源,它将为您提供正确的 select 选项。

Check if your JFrame Class has this method:检查您的 JFrame Class 是否有这个方法:

public static void main(String args[]) {
   java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new YOURFORMNAME().setVisible(true);
            }
   });
}

The setVisibile(true) allows the form to be visible to the user. setVisibile(true)允许表单对用户可见。 The main method is used to define the entry point for the application therefore this method is needed to run the JFrame main方法用于定义应用程序的入口点,因此需要此方法来运行JFrame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM