Ⅰ java界面开发怎样实现文本框不能为空
TextField tf = new TextField ();
//对该文本框进行监听
tf.addFocusListener (new FocusListener(){
// 失去焦点的时候
@Override
public void focusLost(FocusEvent e) {
if(tf.getText.length ==0 || tf.getText.equals("")){
JOptionPane.showMessageDialog(tf, "请输入文本", "提示",
JOptionPane.YES_OPTION);
}
}
@Override
public void focusGained(FocusEvent e) {
}
});
Ⅱ 点击按钮,怎样把文本框字清空,用java语言编写
1、首先打开电脑中java的ide,再创建java项目,如下图所示。
Ⅲ JAVA 怎么监听文本框
方法一)内部类:
txt1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("这你都不懂");
}
});
方法二)前提:当前类要继承ActionListener类
txt1.addActionListener(this);
public void actionPerformed(ActionEvent e) {
System.out.println("这你都不懂");
}
Ⅳ javaSwing中在文本框TextField中输入值,然后点击按钮获取文本框中的值并进行相应的计算,怎么实现啊
1、监听按钮
EventAction ea;ea.getSource().equals("你的按钮对象名称");//--即表示按钮被点击了
2、获取输入框值
TextField tf;tf.getText();//--即可获得输入框文本值
Ⅳ JAVA文本框监听
importjavax.swing.*;
importjavax.swing.event.*;
importjavax.swing.text.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjava.util.regex.*;
{
=newDimension(800,600);
publicStringgetTitle(){return"TextValidateDemo";}
(){returnSIZE;}
publicDimensiongetMinimumSize(){returnSIZE;}
publicDimensiongetMaximumSize(){returnSIZE;}
publicDimensiongetSize(){returnSIZE;}
privateJTextFieldtextField;
=Pattern.compile("([0-9])*");
=newObservable(){
publicvoidnotifyObservers(){
setChanged();
super.notifyObservers();
}
publicvoidnotifyObservers(Objectarg){
setChanged();
super.notifyObservers(arg);
}
};
TextValidateDemo()throwsHeadlessException{
init();
attachListeners();
doLay();
setVisible(true);
}
privatevoidinit(){
textField=newJTextField(50);
validateObservable.addObserver(newTextValidateObserer(textField));
}
privatevoidattachListeners(){
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(1);
}
});
textField.getDocument().addDocumentListener(newDocumentListener(){
publicvoidinsertUpdate(DocumentEvente){
validateObservable.notifyObservers(null);
}
publicvoidremoveUpdate(DocumentEvente){
validateObservable.notifyObservers(null);
}
publicvoidchangedUpdate(DocumentEvente){
validateObservable.notifyObservers(null);
}
});
}
privatevoiddoLay(){
Containercontainer=getContentPane();
container.add(textField,BorderLayout.NORTH);
pack();
}
(Strings){
returnpattern.matcher(s).matches();
}
{
privateJTextComponentt;
publicTextValidateObserer(JTextComponentt){
this.t=t;
}
publicvoipdate(Observableo,Objectarg){
if(t!=null&&arg==null){
t.setForeground(validateInput(t.getText())?
Color.BLACK:Color.RED
);
}
}
}
publicstaticvoidmain(String...args){
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(TextValidateDemo::new);
}
}
Ⅵ java写一个简单的程序,给文本框加监听,按下回车键的时候可以执行“确定”按钮
JTextAreamessage=newJTextArea();
message.addKeyListener(newKeyListener(){
@Override
publicvoidkeyPressed(KeyEventarg0){
intkey=arg0.getKeyCode();
if(key==' '){
JOptionPane.showMessageDialog(null,"您按下了回车");
}
}
@Override
publicvoidkeyReleased(KeyEvente){
}
@Override
publicvoidkeyTyped(KeyEvente){
}
});
Ⅶ java编程如何使文本框不允许输入和粘贴除数字以外
首先你要给你的文本框加入监听.就是获得焦点时和失去焦点时.分别在里面调用一个对数字进行判断的方法.判断方法简单的就是loveweizaiwan了.思路就是先不管用户输入的是什么.当要提交前或者是要插库前对这个文本框内容进行验证.
这个是传统的验证思路.
若你需要实现一个实时验证的机制.那么就是需要向我开始时所说的.在文本框获得焦点时候进行验证.
可以给你的TextField 进行FocusListener的监听.两个方法分别调用一下方法
代码如下:
package com.text;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class InputNumber extends JFrame implements KeyListener
{
private static final long serialVersionUID = 1L;
private JTextField jtf;
private void initComponents()
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setForeground(Color.black);
setFont(new Font("Dialog", Font.PLAIN, 12));
setResizable(false);
setLayout(null);
add(getJTextField0());
setSize(300, 200);
setVisible(true);
}
private JTextField getJTextField0()
{
if (jtf == null)
{
jtf = new JTextField();
jtf.setBounds(40, 40, 155, 22);
jtf.addKeyListener(this);
}
return jtf;
}
public InputNumber()
{
initComponents();
}
private boolean isNumber(String num)
{
try
{
new Float("0" + num);
return true;
}
catch (Exception e)
{
return false;
}
}
public void keyPressed(KeyEvent e)
{
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e)
{
JTextField j = (JTextField) e.getComponent();
if(!isNumber(j.getText()))
{
JOptionPane.showMessageDialog(jtf, "请输入数字");
j.setText("");
}
}
public void keyTyped(KeyEvent e)
{
JTextField j = (JTextField) e.getComponent();
if(!isNumber(j.getText()))
{
JOptionPane.showMessageDialog(jtf, "请输入数字");
j.setText("");
}
}
public static void main(String aa[])
{
new InputNumber();
}
}
Ⅷ JAVA一个窗体添加三个文本框具体怎么操作
只要文本框添加监听即可,用ActionListener即会在文本框中按下回车键时触发事件。
下面是监听的代码:
public void actionPerformed(ActionEvent e)
{
Object o=e.getSource();
if(o==text1) //文本框一号
{
text2.requestFocus();//文本框2号,请求焦点
}
else if(o==text2)
{
text3.requestFocus();
}
}
程序如下:
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyFrame extends JFrame implements ActionListener
{
private JButton button;
private JTextField textField;
private JLabel label;
public MyFrame()
{
button = new JButton("Hello world");
textField = new JTextField(15);
label = new JLabel("输入内容:");
setTitle("Simple-Frame");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 300)/2
, 500, 300);
setLayout(new FlowLayout());
add(label);
add(textField);
add(button);
setResizable(false);
setVisible(true);
this.button.addActionListener(this);
}
public static void main(String[] args)
{
new MyFrame();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
this.textField.setText("Hello World");
}
}
}
Ⅸ Java 怎么点击一个按钮 让文本框里显示输出的结果
思路:定义一个按钮,然后对按钮进行监听,监听后,在里面将结果放置在需要显示结果的文本框中即可。
Ⅹ java 文本框监听
//简单的帮你写了基本功能 如下:
importjava.awt.event.FocusEvent;
importjava.awt.event.FocusListener;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
{
publicTestJT(){
setLayout(null);
JLabellabel=newJLabel("请输入省份证:");
label.setOpaque(true);
label.setBounds(50,30,100,25);
add(label);
finalJTextFieldtf1=newJTextField();
tf1.setOpaque(true);
tf1.setBounds(160,30,80,30);
add(tf1);
tf1.addFocusListener(newFocusListener(){
@Override
publicvoidfocusLost(FocusEvente){
if(tf1.getText().trim().length()!=18){
JOptionPane.showMessageDialog(null,"输入的身份证号码不是18位");
return;
}
}
@Override
publicvoidfocusGained(FocusEvente){
//TODOAuto-generatedmethodstub
}
});
JLabellabe2=newJLabel("请输入名字:");
labe2.setOpaque(true);
labe2.setBounds(50,70,100,25);
add(labe2);
finalJTextFieldtf2=newJTextField();
tf2.setOpaque(true);
tf2.setBounds(160,70,80,30);
add(tf2);
JLabellabe3=newJLabel("请输入性别:");
labe3.setOpaque(true);
labe3.setBounds(50,110,100,25);
add(labe3);
finalJTextFieldtf3=newJTextField();
tf3.setOpaque(true);
tf3.setBounds(160,110,80,30);
add(tf3);
}
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
JPanelp=newTestJT();
frame.add(p);
frame.setVisible(true);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}