1.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Count extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
try {
Count frame = new Count();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Count() {
super();
setTitle("Count");
setBounds(100, 100, 200, 200);
getContentPane().setLayout(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton btn = new JButton();
btn.setText("0");
btn.addActionListener(new ActionListener() {
int i = 1;
public void actionPerformed(ActionEvent arg0) {
btn.setText(String.valueOf(i));
i++;
}
});
btn.setBounds(50, 71, 99, 23);
getContentPane().add(btn);
}
}
---------------------
2.
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Input extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField txt;
public static void main(String args[]) {
try {
Input frame = new Input();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Input() {
super();
setTitle("Input");
setResizable(false);
getContentPane().setLayout(null);
setBounds(100, 100, 200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txt = new JTextField();
txt.setBounds(48, 61, 90, 21);
getContentPane().add(txt);
txt.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
if(txt.getText().matches("\\d+")){
int sum = 0;
int end = Integer.parseInt(txt.getText());
for(int i = 1;i<=end;i++){
sum = sum + i;
}
txt.setText(String.valueOf(sum));
}else{
txt.setText("输入错误,重新输入");
}
}else{
return;
}
}
});
}
}
第一题没技术含量,第二题用个正则表达式吧
很简单,都测试过了,完全可以运行
第一个
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
final JButton b = new JButton("0");
frame.add(b);
b.addActionListener(new ActionListener() {
int n = 0;
public void actionPerformed(ActionEvent e) {
b.setText(""+(++n));
}});
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
第二个
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TextTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
final JTextField t = new JTextField(10);
frame.add(t);
t.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = t.getText();
for(int i=0; i
if(s.charAt(i)>'9' || s.charAt(i)<'0') {
t.setText("输入错误,重新输入");
return;
}
}
int n = Integer.parseInt(s);
int sum = 0;
for(int i=1; i<=n; i++) sum += i;
t.setText(""+sum);
}});
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
不喜欢这样子的提问。。。。
【第一题】
运行出来后增加了清零按钮,可以给另外一个按钮清零,程序如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonTest extends JFrame implements ActionListener
{
int i=0;
JButton jb1 = new JButton("准备开始");
JButton jb2 = new JButton("按钮清零");
FlowLayout fl = new FlowLayout();
ButtonTest()
{
setTitle("按钮计数器");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(fl);
this.getContentPane().add(jb1);
this.getContentPane().add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
show();
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == jb1)
{
int count = ++i;
String str = Integer.toString(count);
jb1.setText(str);
}
if(source == jb2)
{
jb1.setText("0");
}
}
public static void main(String[] args)
{
ButtonTest bt = new ButtonTest();
}
}
【第二题】
也增加了一个清零按钮,可以清零进行多次运算,代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TextFieldTest extends JFrame implements ActionListener
{
JLabel jl = new JLabel("请输入一个数字:");
JTextField jtf = new JTextField(10);
JButton jbt1 = new JButton("计算结果");
JButton jbt2 = new JButton("清零按钮");
FlowLayout fl = new FlowLayout();
TextFieldTest()
{
setTitle("输入框测试");
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(fl);
this.getContentPane().add(jl);
this.getContentPane().add(jtf);
this.getContentPane().add(jbt1);
this.getContentPane().add(jbt2);
jbt1.addActionListener(this);
jbt2.addActionListener(this);
show();
}
public void actionPerformed(ActionEvent e)
{
int sum = 0;
Object source = e.getSource();
if(source == jbt1)
{
String str = jtf.getText();
for(int i=0; i
if(str.charAt(i)>'9' || str.charAt(i)<'0') {
jtf.setText("请输入一个数字");
return;
}
int in = Integer.parseInt(str);
for(int j = 0;j<=in;j++)
{
sum += j;
}
String s = Integer.toString(sum);
jtf.setText(s);
}
}
if(source == jbt2)
{
jtf.setText("0");
}
}
public static void main(String[] args)
{
TextFieldTest tft = new TextFieldTest();
}
}
两段代码经测试OK,希望能对楼主有所帮助,有问题请给我纸条。
思路:
1 使用监听器 , 每点击一次就+1
2 使用监听器 , 判断当输入回车时就开始计算 , 还要有一个计算整数之和的方法