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

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

WBOY
發布: 2024-02-08 21:20:20
轉載
873 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板