java 新手提问 鼠标事件
如图所示,文本框中有默认的文字,当鼠标点击文本框时候,文本框里面的文字会消失,填写完所有文本框信息,点击按钮注册。...
如图所示,文本框中有默认的文字,当鼠标点击文本框时候,文本框里面的文字会消失,填写完所有文本框信息,点击按钮注册。
展开
2个回答
展开全部
源代码如下:
public class Reg extends javax.swing.JFrame
{
public Reg()
{
initComponents();
setTitle("注册界面");
setLocationRelativeTo(null);
}
private void initComponents()
{
button1 = new java.awt.Button();
textField1 = new java.awt.TextField();
textField2 = new java.awt.TextField();
textField3 = new java.awt.TextField();
textField4 = new java.awt.TextField();
textField5 = new java.awt.TextField();
setDefaultCloseOperation
(javax.swing.WindowConstants.EXIT_ON_CLOSE);
button1.setActionCommand("\u6ce8\u518c");
button1.setLabel("\u6ce8\u518c");
//添加鼠标单击事件
button1.addActionListener(new
java.awt.event.ActionListener()
{
public void actionPerformed
(java.awt.event.ActionEvent evt)
{
button1ActionPerformed(evt);
}
});
textField1.setText("\u59d3");
//添加获得焦点的事件
textField1.addFocusListener(new
java.awt.event.FocusAdapter()
{
public void focusGained
(java.awt.event.FocusEvent evt)
{
textField1FocusGained(evt);
}
});
textField2.setText("\u540d");
//添加获得焦点的事件
textField2.addFocusListener(new
java.awt.event.FocusAdapter()
{
public void focusGained
(java.awt.event.FocusEvent evt)
{
textField2FocusGained(evt);
}
});
textField3.setText("\u90ae\u7bb1");
//添加获得焦点的事件
textField3.addFocusListener(new
java.awt.event.FocusAdapter()
{
public void focusGained
(java.awt.event.FocusEvent evt)
{
textField3FocusGained(evt);
}
});
textField4.setText("\u5bc6\u7801");
//添加获得焦点的事件
textField4.addFocusListener(new
java.awt.event.FocusAdapter()
{
public void focusGained
(java.awt.event.FocusEvent evt)
{
textField4FocusGained(evt);
}
});
textField5.setText("textField5");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup
(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup
().addContainerGap().addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false).addComponent(textField1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE).addComponent(textField3,
javax.swing.GroupLayout.DEFAULT_SIZE, 98,
Short.MAX_VALUE).addComponent(textField4,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)).addGap(54, 54, 54)
.addComponent
(textField2,
javax.swing.GroupLayout.PREFERRED_SIZE, 82,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(77,
Short.MAX_VALUE)).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup
().addContainerGap(313,
Short.MAX_VALUE).addComponent(textField5,
javax.swing.GroupLayout.PREFERRED_SIZE, 8,
javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
layout.createSequentialGroup
().addGap(114, 114, 114)
.addComponent
(button1,
javax.swing.GroupLayout.PREFERRED_SIZE, 92,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap
(115, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addComponent(
textField5,
javax.swing.GroupLayout.PREFERRED_SIZE,
9,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(48, 48, 48)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
textField2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(
layout
.createSequentialGroup()
.addComponent(
textField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(
53,
53,
53)
.addComponent(
textField3,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(
53,
53,
53)
.addComponent(
textField4,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(20, 20, 20)
.addComponent(
button1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)));
pack();
}// </editor-fold>
/**
* 密码框响应事件
*/
private void textField4FocusGained(java.awt.event.FocusEvent
evt)
{
// TODO add your handling code here:
textField4.setText("");
}
/**
* 邮箱框响应事件
*/
private void textField3FocusGained(java.awt.event.FocusEvent
evt)
{
// TODO add your handling code here:
textField3.setText("");
}
/**
* 名框响应事件
*/
private void textField2FocusGained(java.awt.event.FocusEvent
evt)
{
// TODO add your handling code here:
textField2.setText("");
}
/**
* 姓框响应事件
*/
private void textField1FocusGained(java.awt.event.FocusEvent
evt)
{
// TODO add your handling code here:
if (textField1.getText().trim() != null)
textField1.setText("");
}
/**
* 按钮响应事件
*/
private void button1ActionPerformed
(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
System.out.println("注册按钮被按下了");
System.out.println("姓:" + textField1.getText());
System.out.println("名:" + textField2.getText());
System.out.println("邮箱:" + textField3.getText());
System.out.println("密码:" + textField4.getText());
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Reg().setVisible(true);
}
});
}
private java.awt.Button button1;//注册按钮
private java.awt.TextField textField1;//姓
private java.awt.TextField textField2;//名
private java.awt.TextField textField3;//邮箱
private java.awt.TextField textField4;//密码
private java.awt.TextField textField5;
}
public class Reg extends javax.swing.JFrame
{
public Reg()
{
initComponents();
setTitle("注册界面");
setLocationRelativeTo(null);
}
private void initComponents()
{
button1 = new java.awt.Button();
textField1 = new java.awt.TextField();
textField2 = new java.awt.TextField();
textField3 = new java.awt.TextField();
textField4 = new java.awt.TextField();
textField5 = new java.awt.TextField();
setDefaultCloseOperation
(javax.swing.WindowConstants.EXIT_ON_CLOSE);
button1.setActionCommand("\u6ce8\u518c");
button1.setLabel("\u6ce8\u518c");
//添加鼠标单击事件
button1.addActionListener(new
java.awt.event.ActionListener()
{
public void actionPerformed
(java.awt.event.ActionEvent evt)
{
button1ActionPerformed(evt);
}
});
textField1.setText("\u59d3");
//添加获得焦点的事件
textField1.addFocusListener(new
java.awt.event.FocusAdapter()
{
public void focusGained
(java.awt.event.FocusEvent evt)
{
textField1FocusGained(evt);
}
});
textField2.setText("\u540d");
//添加获得焦点的事件
textField2.addFocusListener(new
java.awt.event.FocusAdapter()
{
public void focusGained
(java.awt.event.FocusEvent evt)
{
textField2FocusGained(evt);
}
});
textField3.setText("\u90ae\u7bb1");
//添加获得焦点的事件
textField3.addFocusListener(new
java.awt.event.FocusAdapter()
{
public void focusGained
(java.awt.event.FocusEvent evt)
{
textField3FocusGained(evt);
}
});
textField4.setText("\u5bc6\u7801");
//添加获得焦点的事件
textField4.addFocusListener(new
java.awt.event.FocusAdapter()
{
public void focusGained
(java.awt.event.FocusEvent evt)
{
textField4FocusGained(evt);
}
});
textField5.setText("textField5");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup
(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup
().addContainerGap().addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING,
false).addComponent(textField1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE).addComponent(textField3,
javax.swing.GroupLayout.DEFAULT_SIZE, 98,
Short.MAX_VALUE).addComponent(textField4,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)).addGap(54, 54, 54)
.addComponent
(textField2,
javax.swing.GroupLayout.PREFERRED_SIZE, 82,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(77,
Short.MAX_VALUE)).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup
().addContainerGap(313,
Short.MAX_VALUE).addComponent(textField5,
javax.swing.GroupLayout.PREFERRED_SIZE, 8,
javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
layout.createSequentialGroup
().addGap(114, 114, 114)
.addComponent
(button1,
javax.swing.GroupLayout.PREFERRED_SIZE, 92,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap
(115, Short.MAX_VALUE)));
layout
.setVerticalGroup(layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addComponent(
textField5,
javax.swing.GroupLayout.PREFERRED_SIZE,
9,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(48, 48, 48)
.addGroup(
layout
.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
textField2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(
layout
.createSequentialGroup()
.addComponent(
textField1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(
53,
53,
53)
.addComponent(
textField3,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(
53,
53,
53)
.addComponent(
textField4,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(20, 20, 20)
.addComponent(
button1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32)));
pack();
}// </editor-fold>
/**
* 密码框响应事件
*/
private void textField4FocusGained(java.awt.event.FocusEvent
evt)
{
// TODO add your handling code here:
textField4.setText("");
}
/**
* 邮箱框响应事件
*/
private void textField3FocusGained(java.awt.event.FocusEvent
evt)
{
// TODO add your handling code here:
textField3.setText("");
}
/**
* 名框响应事件
*/
private void textField2FocusGained(java.awt.event.FocusEvent
evt)
{
// TODO add your handling code here:
textField2.setText("");
}
/**
* 姓框响应事件
*/
private void textField1FocusGained(java.awt.event.FocusEvent
evt)
{
// TODO add your handling code here:
if (textField1.getText().trim() != null)
textField1.setText("");
}
/**
* 按钮响应事件
*/
private void button1ActionPerformed
(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
System.out.println("注册按钮被按下了");
System.out.println("姓:" + textField1.getText());
System.out.println("名:" + textField2.getText());
System.out.println("邮箱:" + textField3.getText());
System.out.println("密码:" + textField4.getText());
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Reg().setVisible(true);
}
});
}
private java.awt.Button button1;//注册按钮
private java.awt.TextField textField1;//姓
private java.awt.TextField textField2;//名
private java.awt.TextField textField3;//邮箱
private java.awt.TextField textField4;//密码
private java.awt.TextField textField5;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class FrameTest extends JFrame {
private JTextField tf1, tf2;
public FrameTest() {
super("测试");
tf1 = new JTextField("姓");
tf2 = new JTextField("名");
tf1.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
if (tf1.getText().trim().equals("")) {
tf1.setText("姓");
}
}
public void mouseEntered(MouseEvent e) {
if (tf1.getText().trim().equals("姓")) {
tf1.setText(null);
}
}
public void mouseClicked(MouseEvent e) {
}
});
tf2.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
if (tf2.getText().trim().equals("")) {
tf2.setText("名");
}
}
public void mouseEntered(MouseEvent e) {
if (tf2.getText().trim().equals("名")) {
tf2.setText(null);
}
}
public void mouseClicked(MouseEvent e) {
}
});
this.add(tf1, BorderLayout.NORTH);
this.add(tf2, BorderLayout.SOUTH);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new FrameTest();
}
}
你参考一下,还不会就回复我
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class FrameTest extends JFrame {
private JTextField tf1, tf2;
public FrameTest() {
super("测试");
tf1 = new JTextField("姓");
tf2 = new JTextField("名");
tf1.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
if (tf1.getText().trim().equals("")) {
tf1.setText("姓");
}
}
public void mouseEntered(MouseEvent e) {
if (tf1.getText().trim().equals("姓")) {
tf1.setText(null);
}
}
public void mouseClicked(MouseEvent e) {
}
});
tf2.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
if (tf2.getText().trim().equals("")) {
tf2.setText("名");
}
}
public void mouseEntered(MouseEvent e) {
if (tf2.getText().trim().equals("名")) {
tf2.setText(null);
}
}
public void mouseClicked(MouseEvent e) {
}
});
this.add(tf1, BorderLayout.NORTH);
this.add(tf2, BorderLayout.SOUTH);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new FrameTest();
}
}
你参考一下,还不会就回复我
追问
mousePressed(); mouseReleased();mouseClicked();mouseEntered();mouseExited(); 是不是总要一起用?比如你上面tf1鼠标事件中只有mouseEntered();mouseExited()有代码,其他mousePressed(); mouseReleased();mouseClicked()都是空方法.
追答
不是总要的,因为我在那里用的是接口,接口就一定要实现里面的抽象方法,你完全可以用自定义类,然后继承mouse适配器,再重写Enter和Exit方法,就不用出现Press等其他空方法
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询