搜尋
首頁Java我想使用從 jtextfield 輸入的內容作為另一個類別中的陣列列表

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。如有侵權,請聯絡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脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具