celine_chen 2016-08-08 03:59 采纳率: 33.3%
浏览 2068

swing做登陆界面程序的问题,关于actionPerformed

我想用swing编写一个登陆界面的小程序,但是给重置按钮增加action的时候总是报错,

package com.hbasejava.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import bsh.This;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class hbaseframe extends JFrame {

private JPanel contentPane;
private JTextField userNameText;
private JPasswordField PasswordText;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                hbaseframe frame = new hbaseframe();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public hbaseframe() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 679, 516);
    contentPane = new JPanel();
    contentPane.setToolTipText("管理系统");
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JLabel label = new JLabel("管理");
    label.setFont(new Font("Dialog", Font.BOLD, 30));

    JLabel label_1 = new JLabel("用户名");
    label_1.setFont(new Font("Dialog", Font.BOLD, 20));

    JLabel label_2 = new JLabel("密码");
    label_2.setFont(new Font("Dialog", Font.BOLD, 20));

    userNameText = new JTextField();
    userNameText.setColumns(10);

    JButton js_logon = new JButton("登录");

    JButton js_reset = new JButton("重置");
    js_reset.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             this.userNameText.setText("");
        }
    });


    PasswordText = new JPasswordField();
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_contentPane.createSequentialGroup()
                        .addGap(147)
                        .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                            .addComponent(label_1)
                            .addComponent(label_2))
                        .addGap(56)
                        .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                            .addComponent(userNameText, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
                            .addComponent(PasswordText, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label, GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)))
                    .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(js_logon)
                        .addGap(63)
                        .addComponent(js_reset)))
                .addGap(246))
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_contentPane.createSequentialGroup()
                .addGap(110)
                .addComponent(label)
                .addGap(49)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                    .addComponent(label_1)
                    .addComponent(userNameText, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE))
                .addGap(43)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                    .addComponent(label_2)
                    .addComponent(PasswordText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(75)
                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                    .addComponent(js_reset)
                    .addComponent(js_logon))
                .addContainerGap(88, Short.MAX_VALUE))
    );
    contentPane.setLayout(gl_contentPane);
}

}
主要就是下面这一小段
js_reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
this.userNameText.setText("");
}
});
userNameText是用户名的变量,之前应该是定义过的,在这里就总说
userNameText cannot be resolved or is not a field

  • 写回答

1条回答

  • little_how 2016-08-08 05:53
    关注

    this指针域变成了你的ActionListener了,所以读取不到;
    解决办法就是去掉this.让作用域变成全局.

    希望能够帮到你...

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!