Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用actionlisterns从文本字段获取文本_Java_Actionlistener_Gridbaglayout - Fatal编程技术网

Java 使用actionlisterns从文本字段获取文本

Java 使用actionlisterns从文本字段获取文本,java,actionlistener,gridbaglayout,Java,Actionlistener,Gridbaglayout,这可能是一个相当愚蠢的问题,但我正在玩弄gridbag布局,并试图构建一个基本的登录屏幕,将用户信息发送到另一个使用mysql数据库进行检查的类。但在我这么做之前,我必须找到一种方法来监听我的登录按钮被按下,然后从文本字段中获取文本(最后一部分就是我遇到问题的地方)。我曾尝试在addComponentsToPane方法之外声明JTextField,但没有效果 这是到目前为止我的代码 此外,我对任何整洁表示歉意,我是这个网站的新手,还没有足够的知识来正确格式化我的代码 /* * To chang

这可能是一个相当愚蠢的问题,但我正在玩弄gridbag布局,并试图构建一个基本的登录屏幕,将用户信息发送到另一个使用mysql数据库进行检查的类。但在我这么做之前,我必须找到一种方法来监听我的登录按钮被按下,然后从文本字段中获取文本(最后一部分就是我遇到问题的地方)。我曾尝试在addComponentsToPane方法之外声明JTextField,但没有效果

这是到目前为止我的代码

此外,我对任何整洁表示歉意,我是这个网站的新手,还没有足够的知识来正确格式化我的代码

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


/**
 *
 * @author TeslaSolari
 */
public class login {

    public void login() {
        createAndShowGUI();
    }

    private static void createAndShowGUI() {
        //Create and setup the windows
        JFrame frame = new JFrame("Login");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Setup the content pane
        addComponentsToPane(frame.getContentPane());

        //Display the window
        frame.pack();
        frame.setVisible(true);
    }

    public static void addComponentsToPane(Container pane) {
        pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

        //parts
        JButton login;
        JTextField user;
        JLabel[] label = new JLabel[10];
        JPasswordField pass;
        ActionListener loginB = null;

        pane.setLayout(new GridBagLayout());
        //Cpnstraints
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.CENTER;

        // Buttons
        login = new JButton("Login");
        c.weightx = 0.5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        pane.add(login, c);
        login.addActionListener(new Action());

        // textfields / labels
        label[0] = new JLabel("Username");
        user = new JTextField();
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(label[0], c);
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 1;
        pane.add(user, c);

        label[1] = new JLabel("Password");
        pass = new JPasswordField();
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(label[1], c);
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 3;
        pane.add(pass, c);


    }

    //Actions

    static class Action implements ActionListener{
        public void actionPerformed (ActionEvent e){
            System.out.println(label[]);
        }
    }

}

看起来您的解决方案是正确的(将声明JTextField声明移到方法之外),但是您可能没有将它们声明为静态(根据类的其余部分)


您可以使用JOptionPane找到这方面的示例。
private static JTextField user;
private static JPasswordField pass;