首页 >Java >我想使用从 jtextfield 输入的内容作为另一个类中的数组列表

我想使用从 jtextfield 输入的内容作为另一个类中的数组列表

WBOY
WBOY转载
2024-02-08 21:20:20856浏览

php小编百草在这里为您解答:如果您想使用从JTextField输入的内容作为另一个类中的数组列表,可以按照以下步骤进行操作。首先,获取JTextField中输入的内容并保存到一个变量中。然后,在另一个类中创建一个ArrayList对象,并将保存的内容添加到该列表中。这样,您就可以在另一个类中使用该数组列表了。记得要进行适当的异常处理和数据类型转换,确保输入的内容能够正确地添加到列表中。希望这个简短的回答能对您有所帮助!

问题内容

如果我在点击提交按钮后尝试将 jtextfield 添加到另一个类,它将无法工作,因为另一个类尝试在更新之前获取数组列表

import java.util.*;
import java.io.*;
import javax.swing.swingutilities;

public class blackjackmain { 
    public static void main(string [] args) {
          blackjackguiclass content = new blackjackguiclass();
     
          arraylist<string> hi = new arraylist<string>();
          content.outputgui();
          hi = content.newacc;
    }
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;



public class BlackjackGUIClass implements ActionListener {
    
    GridBagLayout ez;
    GridBagConstraints gg;
    private JFrame gui;
    private JPanel content;
    private JLabel title;
    private JTextField usern;
    private JLabel userent;
    private JButton login;
    private JTextField passw;
    private JLabel passent;  
    private JButton create;
    ArrayList<String> logacc = new ArrayList<String>();
    ArrayList<String> newacc = new ArrayList<String>();
    int check = 0;
    
    public BlackjackGUIClass() {
    }
    
            
    public void outputGUI () {
    
    
    gui = new JFrame("Blackjack Menu");
    content = new JPanel();
     gui.add(content);
     
     
     ez = new GridBagLayout();    
     gg = new GridBagConstraints();
     content.setLayout(ez);
   
     
    title = new JLabel ("Abel's Blackjack Casino");
    usern = new JTextField(30);
    usern.setPreferredSize(new Dimension(10, 60));
    userent = new JLabel ("Enter Username: ");
    login = new JButton("Login");
    login.setPreferredSize(new Dimension(300, 50));
    create = new JButton("Create Account");
    create.setPreferredSize(new Dimension(300, 50));
    
    passw = new JTextField(30);
    passw.setPreferredSize(new Dimension(10, 60));
    passent = new JLabel ("Enter Password: ");
    
    login.addActionListener(this);
    create.addActionListener(this);
    passw.addActionListener(this);
    usern.addActionListener(this);
     
    Font createSize = new Font("Times New Roman", Font.PLAIN, 50);
    Font second = new Font("", Font.PLAIN, 30);
    
    title.setFont(createSize);
    userent.setFont(second);
    passent.setFont(second);
     gg.insets = new Insets(5, 5, 5, 5);
   
    gg.gridx = 1;
    gg.gridwidth = 3;
    gg.gridy = 0;
    content.add(title, gg);
    
    
    gg.gridx = 2;
    gg.gridwidth = 1;
    gg.gridy = 1;
    content.add(usern, gg);
    
    
    gg.gridx = 1;
    content.add(userent, gg);
    
   
   
   gg.gridy = 2;
   gg.gridx = 1;
   content.add(passent, gg);
   
   gg.gridx = 2;
   content.add(passw, gg);
   
   gg.gridy = 3;
   gg.gridx = 1;
   gg.gridwidth = 1;
   content.add(login, gg);
   
   gg.gridy = 3;
   gg.gridx = 2;
   gg.gridwidth = 1;
   content.add(create, gg);
   
    
    
    gui.setExtendedState(JFrame.MAXIMIZED_BOTH);    
    gui.setLocationRelativeTo(null);
    gui.setVisible(true);
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void actionPerformed(ActionEvent e) {
        BlackjackMain wow = new BlackjackMain();
        
        if (e.getSource() == login) {
            logacc.add(usern.getText());
            logacc.add(passw.getText());
            
        } else if (e.getSource() == create) {
            newacc.add(usern.getText());
            newacc.add(passw.getText());
        }
        gui.dispose();
    }
    
}

我尝试在主类中设置一个数组列表,该数组列表等于正在更新的 gui 类中的数组列表,但它尝试在添加来自 jtextfield 的输入之前进行更新

解决方法

像大多数 gui 一样,swing 是一个事件驱动环境,也就是说,发生了一些事情并且您对其做出响应(查看 actionlistener,您不知道它何时可能被操作,只知道它可能在某个时刻被操作将来)。

当您在 jframe 上调用 setvisible 时,该调用将立即返回,并且在将来的某个时刻,该窗口将变得可见。

这意味着在您的代码中,您试图在用户有机会输入任何内容之前获取详细信息。

相反,请使用模式 jdialog (请参阅 如何使用对话框了解更多详情)和/或观察者模式来确定何时应对用户输入的内容采取行动。

import java.awt.Dimension;
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 static java.lang.ref.Cleaner.create;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 *
 * @author Mad
 */
public class Main {

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

    public Main() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new BlackjackGUIClass(new BlackjackGUIClass.Observer() {
                    @Override
                    public void createUser(String name, char[] password) {
                        frame.dispose();
                        JOptionPane.showMessageDialog(null, "A new user will be created", "Make it so", JOptionPane.PLAIN_MESSAGE);
                    }

                    @Override
                    public void authenticateUser(String name, char[] password) {
                        frame.dispose();
                        JOptionPane.showMessageDialog(null, "You will be authenticated", "Make it so", JOptionPane.PLAIN_MESSAGE);
                    }
                }));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BlackjackGUIClass extends JPanel {

        public interface Observer {
            public void createUser(String name, char[] password);
            public void authenticateUser(String name, char[] password);
        }

        private Observer observer;

        public BlackjackGUIClass(Observer observer) {
            this.observer = observer;
            GridBagLayout ez = new GridBagLayout();
            GridBagConstraints gg = new GridBagConstraints();
            setLayout(ez);

            JLabel title = new JLabel("Abel's Blackjack Casino");
            JTextField usern = new JTextField(30);
            JLabel userent = new JLabel("Enter Username: ");
            JButton login = new JButton("Login");
            JButton create = new JButton("Create Account");

            JPasswordField passw = new JPasswordField(30);
            JLabel passent = new JLabel("Enter Password: ");

            login.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // This is a repeating pattern and you should consider taking
                    // the time make a re-usable solution
                    String userName = usern.getText();
                    char[] password = passw.getPassword();                    
                    // Could make use of a delegate to validate the password
                    if (userName.isBlank()) {
                        JOptionPane.showMessageDialog(BlackjackGUIClass.this, "User name can't be empty", "Error", JOptionPane.PLAIN_MESSAGE);
                        return;
                    }
                    if (password.length == 0) {
                        JOptionPane.showMessageDialog(BlackjackGUIClass.this, "User name can't be empty", "Error", JOptionPane.PLAIN_MESSAGE);
                        return;
                    }
                    observer.authenticateUser(userName, password);
                }
            });
            create.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // This is a repeating pattern and you should consider taking
                    // the time make a re-usable solution
                    String userName = usern.getText();
                    char[] password = passw.getPassword();                    
                    // Could make use of a delegate to validate the password
                    if (userName.isBlank()) {
                        JOptionPane.showMessageDialog(BlackjackGUIClass.this, "User name can't be empty", "Error", JOptionPane.PLAIN_MESSAGE);
                        return;
                    }
                    if (password.length == 0) {
                        JOptionPane.showMessageDialog(BlackjackGUIClass.this, "User name can't be empty", "Error", JOptionPane.PLAIN_MESSAGE);
                        return;
                    }
                    observer.createUser(userName, password);
                }
            });

            Font createSize = new Font("Times New Roman", Font.PLAIN, 50);
            Font second = new Font("", Font.PLAIN, 30);

            title.setFont(createSize);
            userent.setFont(second);
            passent.setFont(second);
            gg.insets = new Insets(5, 5, 5, 5);

            gg.gridx = 1;
            gg.gridwidth = 3;
            gg.gridy = 0;
            add(title, gg);

            gg.gridx = 2;
            gg.gridwidth = 1;
            gg.gridy = 1;
            add(usern, gg);

            gg.gridx = 1;
            add(userent, gg);

            gg.gridy = 2;
            gg.gridx = 1;
            add(passent, gg);

            gg.gridx = 2;
            add(passw, gg);

            gg.gridy = 3;
            gg.gridx = 1;
            gg.gridwidth = 1;
            add(login, gg);

            gg.gridy = 3;
            gg.gridx = 2;
            gg.gridwidth = 1;
            add(create, gg);
        }
    }
}

我也不鼓励您使用 setpreferredsize,这会对不同平台产生不利影响。

以上是我想使用从 jtextfield 输入的内容作为另一个类中的数组列表的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除