java 我正在尝试做一个用户注册页面.我的代码运行顺利,但我的最终框架和组件是不是被显示

dkqlctbz  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(76)

我目前正试图使注册页面为我的alevel计算机科学nea使用java swing和Gui.我的代码到目前为止似乎是正确的,没有错误,但我似乎不能得到最后的窗口显示所有的组件.当我运行我的代码在intellij它显示“进程完成退出代码0”这意味着没有错误,但我的窗口和组件不被dispalyed.我已经尝试搞乱与不同的东西,虽然我没有设置帧可见性为真,但所有这一切已经做了,所以我在一个损失什么是错的.我已经把我的代码,所以如果任何人有任何想法,我错过了什么,可以帮助我真的很感激它

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class MainRegistrationPage {

    private JTextField Firstname;
    private JTextField Lastname;
    private JTextField Email;
    private JTextField Password;
    private JFrame Frame;
    private JButton Submit;


    public MainRegistrationPage() {

        //create user interface
        Frame= new JFrame("Registration Page");
        Frame.setSize(500,500);
        Frame.setVisible(true);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //add to frame
        //Frame.getContentPane().add(BorderLayout.CENTER, mainPanel );
        Frame.setSize(400,300);
        Frame.setVisible(true);



        //create Jpanel to hold everythoing
        JPanel mainPanel = new JPanel();
        mainPanel.setVisible(true);
        mainPanel.add(Firstname);
        mainPanel.add(Lastname);
        mainPanel.add(Email);
        mainPanel.add(Password);
        mainPanel.add(Submit);

        //create font
        Font greatFont = new Font("Georgia", Font.PLAIN, 14);
        Firstname = new JTextField(5);
        Firstname.setBounds(50,100,200,30);
        Firstname.setFont(greatFont);
        Firstname.setVisible(true);



        Font greatFont2 = new Font("Georgia",Font.PLAIN, 14);
        Lastname = new JTextField(5);
        Lastname.setBounds(50,150,200,30);
        Lastname.setFont(greatFont2);
        Lastname.setVisible(true);

        Font greatFont3 = new Font ("Georgia", Font.PLAIN,14);
        Email = new JTextField(5);
        Email.setBounds(50,200,200,30);
        Email.setFont(greatFont3);
        Email.setVisible(true);

        Font greatFont4 = new Font ("Georgia", Font.PLAIN, 14);
        Password= new JTextField(5);
        Password.setBounds(50,250,200,30);
        Password.setFont(greatFont4);
        Password.setVisible(true);

//button
        JButton Submit = new JButton("Submit");
        Submit.setBounds(50,300,200,30);
        Submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        //create labels
       // JLabel fJLabel = new JLabel("Firstname");
      // JLabel lJLabel = new JLabel("Lastname");
       // JLabel eJLabel = new JLabel("Email");
        //JLabel pJLabel = new JLabel("Password");

        //add to the frame
        Frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
        Frame.setVisible(true);
        Frame.setSize(300,350);
fsi0uk1n

fsi0uk1n1#

我把你的代码复制到我的IDE中,但是它没有编译。在添加了两个丢失的右括号和一个静态main方法之后,我遇到了一个运行时错误。
在修复了运行时错误之后,我终于让您的GUI显示组件。
Java变量名以小写字母开头,这有助于在视觉上将它们与以大写字母开头的类名区分开来。
Oracle有一个有用的教程Creating a GUI With Swing。跳过使用NetBeans IDE学习Swing部分。特别注意Laying Out Components Within a Container部分。
我已经修复了你的代码。这是图形用户界面。

我将JFrameJPanel的创建分开,这样我就可以一次专注于GUI的一个部分,并将关注点分开。
我使用GridBagLayout创建注册表单,并按照从左到右、从上到下的顺序创建Swing组件,以使代码更易于阅读和理解。
下面是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MainRegistrationPage {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainRegistrationPage();
            }
        });
    }

    private JTextField firstname;
    private JTextField lastname;
    private JTextField email;
    private JPasswordField password;

    private JFrame frame;

    public MainRegistrationPage() {

        // create user interface
        frame = new JFrame("Registration Page");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createRegistrationPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createRegistrationPanel() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
        Font font = new Font("Georgia", Font.PLAIN, 14);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(5, 5, 5, 5);

        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel label = new JLabel("First Name:");
        label.setFont(font);
        mainPanel.add(label, gbc);

        gbc.gridx++;
        firstname = new JTextField(30);
        firstname.setFont(font);
        mainPanel.add(firstname, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        label = new JLabel("Last Name:");
        label.setFont(font);
        mainPanel.add(label, gbc);

        gbc.gridx++;
        lastname = new JTextField(30);
        lastname.setFont(font);
        mainPanel.add(lastname, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        label = new JLabel("Email:");
        label.setFont(font);
        mainPanel.add(label, gbc);

        gbc.gridx++;
        email = new JTextField(30);
        email.setFont(font);
        mainPanel.add(email, gbc);

        gbc.gridx = 0;
        gbc.gridy++;
        label = new JLabel("Password:");
        label.setFont(font);
        mainPanel.add(label, gbc);

        gbc.gridx++;
        password = new JPasswordField(30);
        password.setFont(font);
        mainPanel.add(password, gbc);

        gbc.gridwidth = 2;
        gbc.gridx = 0;
        gbc.gridy++;
        JButton submit = new JButton("Submit");
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        mainPanel.add(submit, gbc);

        return mainPanel;
    }

}

相关问题