java—当我试图通过按钮从另一个类访问gui时,只会弹出一个标题栏

zzlelutf  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(239)

我是一个新手程序员和我有点坚持这个问题2小时了。所以我的问题是,每当我点击注册按钮,只有一个标题栏(没有任何内容)弹出在我的屏幕左上角。我怎样才能解决这个问题?先谢谢你

btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AccountCreationGUI window = new AccountCreationGUI();
window.setVisible(true);
frame.dispose();

            }
        });
btnRegister.setBounds(199, 170, 89, 23);
frame.getContentPane().add(btnRegister);

accountcreationgui(我没有添加其他方法来简化这个)

package main;
import java.sql.*;

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import java.awt.Window.Type;

public class AccountCreationGUI extends JFrame {

    private JFrame frmToolInventory;
    private JTextField textfield_firstname;
    private JTextField textfield_lastname;
    private JTextField textfield_age;
    private JTextField textfield_username;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;
    JFrame frame;
    Connection connection = null;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AccountCreationGUI window = new AccountCreationGUI();
                    window.frmToolInventory.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public AccountCreationGUI() {
        initialize();
    }

public void initialize() {
        frmToolInventory = new JFrame();
        frmToolInventory.setResizable(false);
        frmToolInventory.setTitle("Tool Inventory");
        frmToolInventory.getContentPane().setBackground(new Color(0, 51, 51));
        frmToolInventory.setBounds(100, 100, 534, 346);
        frmToolInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmToolInventory.getContentPane().setLayout(null);

        JLabel label_firstname = new JLabel("First Name");
        label_firstname.setForeground(new Color(255, 255, 255));
        label_firstname.setBounds(88, 67, 68, 14);
        frmToolInventory.getContentPane().add(label_firstname);

        JLabel label_lastname = new JLabel("Last Name");
        label_lastname.setForeground(Color.WHITE);
        label_lastname.setBounds(88, 92, 68, 14);
        frmToolInventory.getContentPane().add(label_lastname);

        JLabel label_username = new JLabel("Username");
        label_username.setForeground(Color.WHITE);
        label_username.setBounds(88, 142, 68, 14);
        frmToolInventory.getContentPane().add(label_username);

        JLabel label_password = new JLabel("Password");
        label_password.setForeground(Color.WHITE);
        label_password.setBounds(88, 167, 68, 14);
        frmToolInventory.getContentPane().add(label_password);

        JLabel label_reconfirmpassword = new JLabel("Re-confirm Password");
        label_reconfirmpassword.setForeground(Color.WHITE);
        label_reconfirmpassword.setBounds(88, 192, 101, 14);
        frmToolInventory.getContentPane().add(label_reconfirmpassword);

        JLabel label_age = new JLabel("Age");
        label_age.setForeground(Color.WHITE);
        label_age.setBounds(88, 117, 68, 14);
        frmToolInventory.getContentPane().add(label_age);

        textfield_firstname = new JTextField();
        textfield_firstname.setBounds(224, 64, 144, 20);
        frmToolInventory.getContentPane().add(textfield_firstname);
        textfield_firstname.setColumns(10);

        textfield_lastname = new JTextField();
        textfield_lastname.setColumns(10);
        textfield_lastname.setBounds(224, 89, 144, 20);
        frmToolInventory.getContentPane().add(textfield_lastname);

        textfield_age = new JTextField();
        textfield_age.setColumns(10);
        textfield_age.setBounds(224, 114, 144, 20);
        frmToolInventory.getContentPane().add(textfield_age);

        textfield_username = new JTextField();      
        textfield_username.setColumns(10);
        textfield_username.setBounds(224, 139, 144, 20);
        frmToolInventory.getContentPane().add(textfield_username);

        passwordField = new JPasswordField();
        passwordField.setBounds(224, 164, 144, 20);
        frmToolInventory.getContentPane().add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(224, 189, 144, 20);
        frmToolInventory.getContentPane().add(passwordField_1);

        JButton button_CreateAccount = new JButton("Create");
        button_CreateAccount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createAccount();    
            }
        });
        button_CreateAccount.setBounds(224, 220, 68, 23);
        frmToolInventory.getContentPane().add(button_CreateAccount);

        JButton btnClear = new JButton("Clear");
        btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clear();
            }
        });
        btnClear.setBounds(302, 220, 68, 23);
        frmToolInventory.getContentPane().add(btnClear);
    }
    }
r7knjye2

r7knjye21#

对于位置,需要指定位置;也可以使用系统默认值:

window.setLocationByPlatform(true);

https://stackoverflow.com/a/7778050/145976
至于大小,您需要设置一个大小,并且假设您使用的是布局管理器,请打包它。

window.setPreferredSize(new Dimension(400, 300));
window.pack();
qxsslcnc

qxsslcnc2#

我认为你创建的按钮是很好的 checkout 帐户创建用户界面可能有你的问题。

相关问题