Ⅰ 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);
}
}