netbeans编码_使用NetBeans和Swing编码简单的Java用户界面

这篇博客介绍了如何使用Java NetBeans平台构建GUI,包括设置NetBeans项目,使用JFrame创建窗口,添加JPanel,创建事件监听器,以及展示如何通过按钮切换JPanel的可见性。内容详细解释了GUI的容器层次结构,并提供了构建简单Java GUI应用的步骤和代码示例。
摘要由CSDN通过智能技术生成

netbeans编码

A graphical user interface (GUI) built using the Java NetBeans platform is made up of several layers of containers. The first layer is the window used to move the application around the screen of your computer. This is known as the top-level container, and its job is to give all other containers and graphical components a place to work in. Typically for a desktop application, this top-level container will be made using the 

使用Java NetBeans平台构建的图形用户界面(GUI)由多层容器组成。 第一层是用于在计算机屏幕上移动应用程序的窗口。 这被称为顶层容器,其工作是为所有其他容器和图形组件提供一个可以使用的位置。通常,对于桌面应用程序,此顶层容器将使用

class.

类。

You can add any number of layers to your GUI design, depending on its complexity. You can place graphical components (e.g., text boxes, labels, buttons) directly into the 

您可以根据其复杂性在GUI设计中添加任意数量的层。 您可以将图形组件(例如,文本框,标签,按钮)直接放入

, or you can group them in other containers.

,也可以将它们分组在其他容器中。

The layers of the GUI are known as the containment hierarchy and can be thought of as a family tree. If the 

GUI的各层称为包含层次结构,可以视为族谱。 如果

is the grandfather sitting at the top, then the next container can be thought of as the father and the components it holds as the children.

是祖父坐在顶部,那么下一个容器可以看作是父亲,而它所容纳的组件则是孩子。

For this example, we'll build a GUI with a 

在此示例中,我们将使用

containing two

包含两个

and a

和一个

. The first

。 首先

will hold a

将举行一个

and

. The second

。 第二

will hold a

将举行一个

and a

和一个

. Only one

。 只有一个

(and hence the graphical components it contains) will be visible at a time. The button will be used to switch the visibility of the two

(因此包含的图形组件)将一次可见。 该按钮将用于切换两者的可见性

.

There are two ways to build this GUI using NetBeans. The first is to manually type in the Java code that represents the GUI, which is discussed in this article. The second is to use the NetBeans GUI Builder tool for building Swing GUIs.

有两种使用NetBeans构建此GUI的方法。 第一种是手动键入代表GUI的Java代码,本文将对此进行讨论。 第二种是使用NetBeans GUI生成器工具来构建Swing GUI。

For information on using JavaFX rather than Swing to create a GUI, see What is JavaFX?

有关使用JavaFX而不是Swing创建GUI的信息,请参阅什么是JavaFX

Note: The complete code for this project is at Example Java Code for Building A Simple GUI Application.

注意 :此项目的完整代码在“用于构建简单GUI应用程序的Java示例代码”中

设置NetBeans项目 ( Setting Up the NetBeans Project )

Create a new Java Application project in NetBeans with a main class We'll call the project

在NetBeans中使用主类创建一个新的Java Application项目,我们将其称为

Check Point: In the Projects window of NetBeans should be a top-level GuiApp1 folder (if the name is not in bold, right-click the folder and choose

检查点:在NetBeans的“项目”窗口中,应该是顶层GuiApp1文件夹(如果名称不是粗体,请右键单击该文件夹,然后选择

). Beneath the

)。 在下面

folder should be a Source Packages folder with

文件夹应该是带有

called GuiApp1. This folder contains the main class called

名为GuiApp1。 该文件夹包含名为

.java.

.java。

Before we add any Java code, add the following imports to the top of the

在我们添加任何Java代码之前,请将以下导入添加到

class, between the

类之间

line and the

线和

:

These imports mean that all the classes we need to make this GUI application will be available for us to use.

这些导入意味着使我们能够使用此GUI应用程序所需的所有类。

Within the main method, add this line of code:

在main方法中,添加以下代码行:

This means that the first thing to do is to create a new 

这意味着要做的第一件事是创建一个新的

object. It's a nice short-cut for example programs, as we only need one class. For this to work, we need a constructor for the

目的。 对于示例程序来说,这是一个很好的捷径,因为我们只需要一个类。 为此,我们需要一个构造函数

class, so add a new method:

类,因此添加一个新方法:

In this method, we'll put all the Java code needed to create the GUI, meaning that every line from now on will be inside the

在这种方法中,我们将放置创建GUI所需的所有Java代码,这意味着从现在开始的每一行都将位于

method.

方法。

使用JFrame构建应用程序窗口 ( Building the Application Window Using a JFrame )

Design Note: You might have seen Java code published that shows the class (i.e.,

设计说明:您可能已经看到发布了显示该类的Java代码(即,

) extended from a

)从

. This class is then used as the main GUI window for an application. There really isn't any need to do this for a normal GUI application. The only time you would want to extend the

。 然后,该类用作应用程序的主GUI窗口。 对于普通的GUI应用程序,实际上并不需要这样做。 您唯一想扩展的

class is if you need to make a more specific type of

类是如果您需要制作更具体的类型

(take a look at

(看一眼

for more information on making a subclass).

有关创建子类的更多信息)。

As mentioned earlier, the first layer of the GUI is an application window made from a

如前所述, GUI的第一层是由

. To create a

。 创建一个

object, call the

对象,调用

constructor:

构造函数:

Next, we'll set the behavior of our GUI application window, using these four steps:

接下来,我们将使用以下四个步骤来设置GUI应用程序窗口的行为:

1. Ensure that the application closes when the user closes the window so that it does not continue to run unknown in the background:

1.确保在用户关闭窗口时关闭应用程序,以使其不会继续在后台运行:

2. Set a title for the window so the window doesn't have a blank title bar. Add this line:

2.为窗口设置标题,以便窗口没有空白标题栏。 添加此行:

3. Set the window size, so that the window is sized to accommodate the graphical components you place into it.

3.设置窗口大小,以便调整窗口大小以适应放置在其中的图形组件。

Design Note: An alternative option for setting the size of the window is to call the

设计说明:设置窗口大小的另一种方法是调用

method of the

的方法

class. This method calculates the size of the window based on the graphical components it contains. Because this sample application doesn't need to change its window size, we'll just use ​the

类。 此方法根据窗口包含的图形组件来计算窗口的大小。 由于此示例应用程序不需要更改其窗口大小,因此我们仅使用

method.

方法。

4. Center the window to appear in the middle of the computer screen so that it does not appear in the top left-hand corner of the screen:

4.使窗口居中以出现在计算机屏幕的中间,这样它就不会出现在屏幕的左上角:

添加两个JPanel ( Adding the Two JPanels )

The two lines here create values for the

这两条线在这里创造价值

and

objects we'll be creating shortly, using two

我们将很快创建的对象,使用两个

arrays. This makes it easier to populate some example entries for those components:

数组。 这使得填充这些组件的一些示例条目变得更加容易:

创建第一个JPanel对象 ( Create the first JPanel Object )

Now, let's create the first

现在,让我们创建第一个

object. It will contain a

目的。 它将包含一个

and a

和一个

. All three are created via their constructor methods:

。 这三个都是通过其构造函数方法创建的:

Notes on the above three lines:

关于以上三行的注意事项:

  • The

    JPanel variable is declared 
    final. This means that the variable can only hold the final 。 这意味着该变量只能容纳
    JPanel that's created in this line. The result is that we can use the variable in an inner class. It will become apparent why we want to later on in the code.
  • The

    JLabel and 
    JComboBox have values passed to them to set their graphical properties. The label will appear as "Fruits:" and the combobox will now have the values contained within the 
    fruitOptions array declared earlier.
  • The

    add() method of the 
    JPanel places graphical components into it. A 
    JPanel uses the FlowLayout as its default 
    layout manager. This is fine for this application as we want the label to sit next to the combobox. As long as we add the 布局管理器 。 这对于此应用程序来说很好,因为我们希望标签位于组合框旁边。 只要我们添加
    JLabel first, it will look fine:

创建第二个JPanel对象 ( Create the Second JPanel Object )

The second

第二

follows the same pattern. We'll add a

遵循相同的模式。 我们将添加一个

and a

和一个

and set the values of those components to be "Vegetables:" and the second

并将这些组件的值设置为“ Vegetables:”,第二个

array

数组

. The only other difference is the use of the

。 唯一的不同是使用

method to hide the

隐藏方法

. Don't forget there will be a

。 别忘了会有一个

controlling the visibility of the two

控制两者的可见性

. For this to work, one needs to be invisible at the start. Add these lines to set up the second

。 为此,一开始必须是不可见的。 添加这些行以设置第二行

:

One line worth noting in the above code is the use of the

在上面的代码中值得注意的一行是使用

method of the

的方法

. The

。 的

value makes the list display the items it contains in two columns. This is called a "newspaper style" and is a nice way to display a list of items rather than a more traditional vertical column.

value使列表在两列中显示它包含的项目。 这被称为“报纸样式”,是一种显示项目列表的方法,而不是传统的垂直列。

添加画龙点睛 ( Adding Finishing Touches )

The last component needed is the

最后需要的组件是

to control the visibility of the

控制广告的可见度

s. The value passed in the

s。 传入的值

constructor sets the label of the button:

构造函数设置按钮的标签:

This is the only component that will have an event listener defined. An "event" occurs when a user interacts with a graphical component. For example, if a user clicks on a button or writes text into a textbox, then an event occurs.

这是将定义事件侦听器的唯一组件。 当用户与图形组件进行交互时,将发生“事件”。 例如,如果用户单击按钮或将文本写入文本框,则会发生事件。

An event listener tells the application what to do when the event happens. 

事件侦听器告诉应用程序事件发生时该怎么办。

uses the ActionListener class to "listen" for a button click by the user.

使用ActionListener类“监听”用户单击的按钮。

创建事件监听器 ( Create the Event Listener )

Because this application performs a simple task when the button is clicked, we can use an anonymous inner class to define the event listener:

因为单击按钮时此应用程序执行简单的任务,所以我们可以使用匿名内部类来定义事件侦听器:

This may look like scary code, but you just have to break it down to see what is happening:

这可能看起来像可怕的代码,但是您只需将其分解以查看发生了什么:

  • First, we call the

    首先,我们称

    addActionListener method of the 
    JButton. This method expects an instance of the 
    ActionListener class, which is the class that listens for the event.
  • Next, we create the instance of the 

    接下来,我们创建

    ActionListener class by declaring a new object using 
    new ActionListener() and then providing an anonymous inner class — which is all the code inside the curly brackets.
  • Inside the anonymous inner class, add a method called

    在匿名内部类中,添加一个名为

    actionPerformed(). This is the method that is called when the button is clicked. All that's needed in this method is to use 
    setVisible() to change the visibility of the 
    JPanels.

将JPanels添加到JFrame ( Add the JPanels to the JFrame )

Finally, we need to add the two

最后,我们需要添加两个

s and

to the

. By default, a

。 默认情况下,

uses the BorderLayout layout manager. This means there are five areas (across three rows) of the

使用BorderLayout布局管理器。 这意味着该区域有五个区域(跨三行)

that can contain a graphical component (NORTH, {WEST, CENTER, EAST}, SOUTH). Specify this area using the

可以包含图形组件(NORTH,{WEST,CENTER,EAST},SOUTH)。 使用

method:

方法:

将JFrame设置为可见 ( Set the JFrame to Be Visible )

Finally, all of the above code will have been for nothing if we don't set the 

最后,如果我们不设置

to be visible:

可见:

Now we're ready to run the NetBeans project to display the application window. Clicking on the button will switch between showing the combobox or list.

现在,我们准备运行NetBeans项目以显示应用程序窗口。 单击该按钮将在显示组合框或列表之间切换。

翻译自: https://www.thoughtco.com/coding-a-simple-graphical-user-interface-2034064

netbeans编码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值