目录
问题内容
解决方法
首页 Java 我想使用从 jtextfield 输入的内容作为另一个类中的数组列表

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

Feb 08, 2024 pm 09:20 PM

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)