java文本框輸入
⑴ 怎樣讓java文本框只輸入數字問題
java swing中利用JFormattedTextField的控制項加keyReleased能實現,以下實現的只能輸入數字的文本框代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Administrator
*/
public class JFormattedText extends JFrame {
private JFormattedTextField text;
public JFormattedText() {
JPanel panel = new JPanel();
text = new JFormattedTextField(new java.text.DecimalFormat("#0"));
// text = new JFormattedTextField();
// text.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
text.setFont(new java.awt.Font("宋體", 0, 12)); // NOI18N
text.setMaximumSize(new java.awt.Dimension(50, 21));
text.setMinimumSize(new java.awt.Dimension(50, 21));
text.setPreferredSize(new java.awt.Dimension(50, 21));
//過濾輸入的字元
text.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
String old = text.getText();
JFormattedTextField.AbstractFormatter formatter = text.getFormatter();
if (!old.equals("")) {
if (formatter != null) {
String str = text.getText();
try {
long page = (Long) formatter.stringToValue(str);
text.setText(page + "");
} catch (ParseException pe) {
text.setText("1");//解析異常直接將文本框中值設置為1
}
}
}
}
});
panel.add(text);
this.add(panel);
this.setBounds(200, 100, 100, 100);
}
public static void main(String[] arg) {
new JFormattedText().setVisible(true);
}
}
⑵ 如何用java文本框里自動輸入文字
importjava.awt.BorderLayout;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
{
=1L;
publicSpider()
{
setTitle("文本區示例");
setSize(500,190);
setResizable(false);
setLayout(newBorderLayout());
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
privateSpideraddComponents()
{
finalJTextAreaarea=newJTextArea();
JScrollPanepane=newJScrollPane(area);
add(pane,BorderLayout.CENTER);
JPanelbottom=newJPanel(newFlowLayout(FlowLayout.LEFT));
JButtonbtn1=newJButton("自動換行");
btn1.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
area.setLineWrap(true);
area.setWrapStyleWord(true);
}
});
JButtonbtn2=newJButton("不換行");
btn2.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
area.setWrapStyleWord(false);
area.setLineWrap(false);
}
});
bottom.add(btn1);
bottom.add(btn2);
add(bottom,BorderLayout.SOUTH);
returnthis;
}
publicstaticvoidmain(String[]args)
{
newSpider().addComponents().setVisible(true);
}
}
⑶ Java文本框如何輸入一個數據,就把數據存入到集合中去
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class DemoSwing extends JFrame implements KeyListener
{
JTextArea area;
ArrayList array;
public DemoSwing(){
area = new JTextArea();
array = new ArrayList();
area.addKeyListener(this);
this.add(area);
this.setBounds(100, 100, 300, 300);
this.setVisible(true);
}
public static void main(String [] args){
DemoSwing ds = new DemoSwing();
}
@Override
public void keyTyped(KeyEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e)
{
// TODO Auto-generated method stub
if(e.getKeyChar() == KeyEvent.VK_ENTER){
String content = area.getText();
String[] arraysString = content.split(" ");
for(int i = 0; i < arraysString.length; i++){
array.add(Integer.parseInt(arraysString[i]));
}
int sum = 0;
for(int i = 0; i < array.size() ; i++){
sum += (Integer)array.get(i);
}
System.out.println("數值的總和是 " + sum);
}
}
@Override
public void keyReleased(KeyEvent e)
{
// TODO Auto-generated method stub
}
}
好了自己手工寫的請採納
⑷ java 要求在文本框中輸入字元串,當按下回車鍵或單擊按鈕時,將輸入的文字顯示在文本區中怎麼操作
給text對象和button對象加上事件響應方法即可,直接代碼:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class SwingTest extends JFrame {
// 確認按鈕
private JButton b1 = new JButton("顯示一把");
// 入力文本框
private JTextField txt = new JTextField(10);
// 顯示文本域
private JTextArea area = new JTextArea(2, 20);
private JPanel jp = new JPanel();
// 事件類
class SwingTestListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
area.setText(txt.getText());
}
}
public SwingTest(String frameName) {
super(frameName);
// 按下按鈕動作響應
b1.addActionListener(new SwingTestListener());
// 按下ENTER鍵響應
txt.addActionListener(new SwingTestListener());
setLayout(new FlowLayout());
add(txt);
add(b1);
jp.setBorder(new TitledBorder("TextArea"));
jp.add(area);
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300 , 150);
setLocation(500, 500);
setVisible(true);
}
public static void main(String[] args) {
new SwingTest("SwingTest");
}
}
⑸ java文本框輸入
JTextArea text = new JTextArea();
//…………………………省略圖形界面代碼
//下面是button事件中的代碼
String data = text.getText();
new PrintStream("out.bat").println(data); //寫到out.bat文件上,如需指定路徑就"C:/out.bat"
//不明白我可以給你寫一個完整的
⑹ JAVA輸入和輸出文本框怎麼寫,求解答!
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JTextField;
{
privateJTextFieldname=newJTextField("someone");
privateJTextFieldtext=newJTextField();
privateJButtonbutton=newJButton("輸入");
publicXXFrame()
{
super("一個測試框框");
name.setBounds(40,40,200,20);
button.setBounds(260,40,100,20);
text.setBounds(40,110,200,20);
this.setLayout(null);
this.setBounds(200,200,400,400);
this.add(name);
this.add(button);
this.add(text);
this.addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(-1);
}
});
button.addActionListener(this);
this.setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==button)
{
text.setText(name.getText());
}
}
publicstaticvoidmain(String[]args)
{
newXXFrame();
}
}
⑺ Java如何實現:在文本框輸入一個數據,就把數據存入到集合中去
1 ,先判斷定義的集合是否是空,如果為空,則:
2 .文本框裡面輸入的值,做一個失焦事件,通過ajax傳到後台
3.將傳進來的值add到你的集合中,
4,同時頁面也可通過jquery.append()一個元素