如何创建具有多个页面的 Swing 应用程序 [英] How to create a swing application with multiple pages

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

问题描述

之前,我使用 servlet 创建了一个小型应用程序,其中第一页显示了一个用户注册页面,该页面通过提交按钮捕获用户的名字和姓氏等基本信息.

Earlier I have created a small application using servlets where the first page shows a user registration page that captures basic information like user's first name and last name etc. with a Submit button.

假设在第一页中输入的用户名字是Scott",然后一旦用户提交表单,然后在第二页中,我创建了一条欢迎消息Welcome Scott".然后我提供了一个选项,通过连接到数据库来查看之前注册到应用程序的用户列表.

Let's say the user first name entered in first page is "Scott" then once the user submits the form then in second page I have created a welcome message as "Welcome Scott". Then I provided an option to see list of users who have registered to the application earlier by connecting to database.

现在我想在 Swing 应用程序中实现相同的功能.我是 Swings 的新手,所以我尝试从各种来源学习它,但我找不到可以在多个框架/面板之间导航的示例.

Now I wanted to implement the same in Swing application. I am new to Swings so I tried to learn it from various sources but I was not able to find an example where I can navigate between multiple frames/panels.

对于第一页,我在下面的程序中创建了显示文本字段和提交按钮的程序:

For the first page I have created below program that displays the text fields and a submit button:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class UserRegistration extends JFrame {

    JButton button;

    public UserRegistration() {

        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        final JLabel label = new JLabel("Please enter details: ");
        JLabel firstName = new JLabel("First Name");
        JLabel lastName = new JLabel("Last Name");
        final JTextField firstNameTxt = new JTextField(20);
        final JTextField lastNameTxt = new JTextField(20);

        button = new JButton("Submit");
        JButton button1 = new JButton("Cancel");

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
                UserDetails user = new UserDetails();
                user.showDetails();
                user.setVisible(true);
            }
        });

        add(label);
        add(firstName);
        add(firstNameTxt);
        add(lastName);
        add(lastNameTxt);
        add(button);
        add(button1);

        setVisible(true);

    }
    public static void main(String args[]) {
        new UserRegistration();
    }

}

要从第 1 帧到第 2 帧,我使用了以下几行:

To go from 1st frame to 2nd frame I am using these lines:

                dispose();
                UserDetails user = new UserDetails();
                user.showDetails();
                user.setVisible(true);

现在对于第二页,我无法找出如何获取用户输入的参数,这是我被困在的代码:

Now for second page I am not able to find out how to get the parameters the user has entered, this is the code that I am stuck at:

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class UserDetails extends JFrame {

    //private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    public UserDetails() {
        prepareGUI();
    }


    private void prepareGUI() {
        //mainFrame = new JFrame("Java Swing Examples");
        setSize(800, 800);
        setLayout(new GridLayout(3, 5));
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        headerLabel = new JLabel("This is Header", JLabel.CENTER);
        statusLabel = new JLabel("This is Status", JLabel.CENTER);

        statusLabel.setSize(350, 100);

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        System.out.println(controlPanel);
        add(headerLabel);
        add(controlPanel);
        add(statusLabel);
        //setVisible(true);
    }

    public void showDetails() {

        // Here I want display the welcome message and also to add all the registered user details.
    }
}

请帮助我如何创建包含多个页面的应用程序?

Please help me how to create an application that contains multiple pages?

推荐答案

Swing 提供 CardLayout 类来提供您所需要的.它允许您加载两个或多个组件(JPanels)并允许共享相同的显示.可以像向导一样在卡片之间导航.请查看本教程:CardLayout

Swing provides CardLayout class to provide what you need. It allows you to load two or more components(JPanels) and allowing to share the same display. Its possible to navigate between the cards just like a wizard. Please look into this tutorial:CardLayout

这篇关于如何创建具有多个页面的 Swing 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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