
正文
文本框源代码java 文本框代码怎么写
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
想设计一个java小程序,在文本框输入一个数,然后每一秒+1,求给完整代码
package com.demo;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TimerTest {
private Timer timer;
private JTextField field;
private JButton button;
private boolean flag = true;
public TimerTest() {
timer = new Timer();
addview();
}
private void addview() {
JFrame frame = new JFrame("Timer test");
field = new JTextField();
field.setPreferredSize(new Dimension(0, 30));
button = new JButton("start");
button.setPreferredSize(new Dimension(100, 30));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (flag) {
auto();
flag = false;
button.setText("stop");
} else {
timer.cancel();
flag = true;
button.setText("start");
}
}
});
frame.add(field, BorderLayout.CENTER);
frame.add(button, BorderLayout.EAST);
frame.setBounds(200, 200, 300, 60);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TimerTest();
}
private void auto() {
timer.schedule(new TimerTask() {
@Override
public void run() {
int num = Integer.parseInt(field.getText().trim());
num += 1;
field.setText(num + "");
auto();
}
}, 1000);
}
}
相关问答
Q1: 编写JAVA程序,在其中有一个按钮和一个文本框。单击按钮时,文本框中显示按钮上显示的字。
1、首先,设置在网页中显示文本的格式,文本的高度为100像素,宽度为280像素。
2、定义显示文本的字体大小为22像素,离左侧和右侧的距离设置为自动,离顶部的距离设置为15像素。
3、用font标签在网页中插入要显示的文本。
4、由于显示的文本内容是动态控制的,所以设置一个标识符poemsi,用来动态表示显示的诗句。
5、然后,在网页中插入一个按钮。
6、按钮的高度为33像素,宽度为100像素,离左侧和右侧的距离为自动,离顶部的距离为20像素。
7、用type属性,定义按钮的类型为button。
8、定义按钮的标签为显示,单击后执行的函数为show。
9、按钮的背景色值为#aaeded,字体大小为20像素。
Q2: 用Java编写小程序(包含组合框下拉和文本框)变换背景颜色
代码如下:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class App extends JFrame {
private JComboBoxString cbxColors;
private JTextField txtName;
private MapString, Color colorMap;
public App() {
this.setSize(300, 100);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
colorMap = new HashMap();
colorMap.put("红", Color.RED);
colorMap.put("绿", Color.GREEN);
colorMap.put("蓝", Color.BLUE);
colorMap.put("黄", Color.YELLOW);
cbxColors = new JComboBox(colorMap.keySet().toArray());
cbxColors.setPreferredSize(new Dimension(50, 23));
cbxColors.addItemListener(e - {
Color color = colorMap.get(e.getItem());
txtName.setBackground(color);
});
this.add(cbxColors);
txtName = new JTextField();
txtName.setPreferredSize(new Dimension(100, 23));
txtName.setBackground(Color.RED);
this.add(txtName);
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
Q3: java语言,实现点击清空按钮,清空多个文本框内容(源代码)
script type="text/javascript"
function aa(){
var a = document.getElementsByTagName("input");
for(var i = 0;ia.length;i++){
if(a[i].type == "text"){
a[i].value = "";
}
}
}
/script
input type="button" value = "清空" onclick="aa();"/
Q4: java中点击一个按钮弹出两个输入文本框的源代码
写文本框源代码java了一个很简单文本框源代码java的案例,可以参考和修改
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FromeDemo extends JFrame{
JButton jbutton;
public FromeDemo() {
jbutton = new JButton("弹出2个文本框");
jbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JDialog jd = new JDialog();
jd.setBounds(320, 180, 260, 100);
jd.setTitle("弹出文本框");
jd.getContentPane().setLayout(new GridLayout(2, 2));
jd.add(new JLabel("文本框一"));
jd.add(new JTextField(80));
jd.add(new JLabel("文本框二"));
jd.add(new JTextField(80));
jd.setModal(true);//确保弹出文本框源代码java的窗口在其文本框源代码java他窗口前面
jd.setVisible(true);
}
});
add(jbutton,BorderLayout.SOUTH);
setBounds(300, 100, 320, 320);
setTitle("测试");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[]) {
new FromeDemo();
}
}
Q5: java文本框格式的代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
public class Ttex {
BufferedImage bi = new BufferedImage(300, 200, BufferedImage.TYPE_INT_RGB);
public Ttex(String text) {
Graphics gr = bi.createGraphics();
gr.setColor(Color.white);
gr.fillRect(0, 0, bi.getWidth(), bi.getHeight());
gr.setColor(Color.GRAY);
gr.drawString(text, 10, 10);
}
/**
* 根据地址来保存文件
*
* @param dir
* 文件地址
*/
private void Save(String dir) {
BufferedImage image = bi;
// 得到系统支持的图片写入器
Iterator it = ImageIO.getImageWritersBySuffix("jpg");
ImageWriter iw = null;
if (it.hasNext()) {
try {
ImageOutputStream imgOut = ImageIO
.createImageOutputStream(new File(dir));
iw = (ImageWriter) it.next();
iw.setOutput(imgOut);
IIOImage iioi = new IIOImage(image, null, null);
iw.write(iioi);
imgOut.close();
iw.dispose();
} catch (IOException e1) {
}
}
}
public static void main(String[] args) {
Ttex m = new Ttex("0123456789");
m.Save("D:/1.jpg");
}
}
关于文本框源代码java和文本框代码怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







