JAVA题目
1、编写一个小应用程序,小应用程序窗口中有一个按钮,点击这个按钮时,点击按钮的次数会显示在按钮上。2、设计一个窗口,窗口中有一个文本框。程序对键盘输入进行监视,当输入一个...
1、 编写一个小应用程序,小应用程序窗口中有一个按钮,点击这个按钮时,点击按钮的次数会显示在按钮上。
2、 设计一个窗口,窗口中有一个文本框。程序对键盘输入进行监视,当输入一个整数和回车符时,在文本框输出1至该整数的数列和;如果中间输入非数字的其他字符,在本文框中显示“输入错误,重新输入”的字样。
(用JAVA) 展开
2、 设计一个窗口,窗口中有一个文本框。程序对键盘输入进行监视,当输入一个整数和回车符时,在文本框输出1至该整数的数列和;如果中间输入非数字的其他字符,在本文框中显示“输入错误,重新输入”的字样。
(用JAVA) 展开
2个回答
展开全部
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.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.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);
}
}
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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询