![]() |
#2
grmmylbs2016-03-14 11:08
因为你的 String i = scanner.next();已经把空格前的读出来了,我 改了一下,还是有点小bug,暂时没想出更好的方法
class WindowInput extends JFrame implements ActionListener { JTextArea showResult; JButton openInput; String regex = "[\\d\\s]+"; WindowInput() { openInput = new JButton("弹出输入对话框"); showResult = new JTextArea(); add(openInput, BorderLayout.NORTH); add(new JScrollPane(showResult), BorderLayout.CENTER); openInput.addActionListener(this); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String str = JOptionPane.showInputDialog(this, "输入数字,用空格隔开", "输入对话框", JOptionPane.PLAIN_MESSAGE); if(str != null) { Scanner scanner = new Scanner(str); double sum = 0; int k = 0; //String i = scanner.nextLine(); //if(i.matches(regex)) { while(scanner.hasNext()) { if (!scanner.hasNext(regex)) { JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE); return; } try{ double number = scanner.nextDouble(); if(k == 0) showResult.append(""+number); else showResult.append("+"+number); sum += number; k++; } catch(InputMismatchException exp){ String t = scanner.next(); } } showResult.append("="+sum+"\n"); //} //else // JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE); } } } |

package 练习代码;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Example9_17 {
public static void main(String args[]) {
WindowInput win = new WindowInput();
win.setTitle("带输入对话框的窗口");
win.setBounds(80, 90, 280, 300);
}
}
class WindowInput extends JFrame implements ActionListener {
JTextArea showResult;
JButton openInput;
String regex = "[\\d\\s]+";
WindowInput() {
openInput = new JButton("弹出输入对话框");
showResult = new JTextArea();
add(openInput, BorderLayout.NORTH);
add(new JScrollPane(showResult), BorderLayout.CENTER);
openInput.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str = JOptionPane.showInputDialog(this, "输入数字,用空格隔开", "输入对话框", JOptionPane.PLAIN_MESSAGE);
if(str != null) {
Scanner scanner = new Scanner(str);
double sum = 0;
int k = 0;
String i = scanner.next();
if(i.matches(regex)) {
while(scanner.hasNext()) {
try{
double number = scanner.nextDouble();
if(k == 0)
showResult.append(""+number);
else
showResult.append("+"+number);
sum += number;
k++;
}
catch(InputMismatchException exp){
String t = scanner.next();
}
}
showResult.append("="+sum+"\n");
}
else
JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Example9_17 {
public static void main(String args[]) {
WindowInput win = new WindowInput();
win.setTitle("带输入对话框的窗口");
win.setBounds(80, 90, 280, 300);
}
}
class WindowInput extends JFrame implements ActionListener {
JTextArea showResult;
JButton openInput;
String regex = "[\\d\\s]+";
WindowInput() {
openInput = new JButton("弹出输入对话框");
showResult = new JTextArea();
add(openInput, BorderLayout.NORTH);
add(new JScrollPane(showResult), BorderLayout.CENTER);
openInput.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str = JOptionPane.showInputDialog(this, "输入数字,用空格隔开", "输入对话框", JOptionPane.PLAIN_MESSAGE);
if(str != null) {
Scanner scanner = new Scanner(str);
double sum = 0;
int k = 0;
String i = scanner.next();
if(i.matches(regex)) {
while(scanner.hasNext()) {
try{
double number = scanner.nextDouble();
if(k == 0)
showResult.append(""+number);
else
showResult.append("+"+number);
sum += number;
k++;
}
catch(InputMismatchException exp){
String t = scanner.next();
}
}
showResult.append("="+sum+"\n");
}
else
JOptionPane.showMessageDialog(this, "请输入数字好吗", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}