我想用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