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()一个元素