import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class QuestionTwo {
/**
* JAVA 编写一个字符界面的程序,接受用户键盘输入的3个整数,并输出这3个整数的最大值和最小值。
*
* @throws IOException
*/
public static void main(String[] args) throws IOException {
MyFrame frame = new MyFrame();
frame.setVisible(true);
}
}
class MyFrame extends JFrame implements ActionListener {
/** */
private static final long serialVersionUID = 1L;
private JButton buttonShow;
private JTextField inputField;
public MyFrame() {
init();
}
private void init() {
// 设置窗体外观
setSize(500, 500);
// new出新组件
JLabel label = new JLabel("请在些输入数字,以空格分隔.");
inputField = new JTextField();
buttonShow = new JButton("显示最大值及最小值.");
// 布局
setLayout(new BorderLayout());
add(label, BorderLayout.EAST);
add(inputField, BorderLayout.CENTER);
add(buttonShow, BorderLayout.SOUTH);
// 增加事件监听
buttonShow.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals(buttonShow.getActionCommand())) {
String inputString = inputField.getText();
if (null == inputString || 0 == inputString.trim().length()) {
showMessage("请先按要求输入整数!");
}
String numStrs[] = inputString.split(" ");
Integer nums[] = new Integer[numStrs.length];
int realIndex = 0;
int index = 0;
for (String numStr : numStrs) {
try {
nums[realIndex++] = Integer.parseInt(numStr);
} catch (Exception e) {
showMessage("第" + (index + 1) + "个数输入的不是整数!跳过");
realIndex--;
}
index++;
}
Arrays.sort(nums, 0, realIndex);
showMessage("最小的数为:" + nums[0]);
showMessage("最大的数为:" + nums[realIndex - 1]);
}
}
/**
* 显示信息
*
* @param message
*/
private void showMessage(String message) {
JOptionPane.showMessageDialog(this, message);
}
}
Try this one:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GuiTest extends Applet implements ActionListener {
Label firstNum, secondNum, thirdNum, result;
TextField num1, num2, num3;
Button enter;
int number1, number2, number3;
public void init() {
firstNum = new Label("First Integer Number");
super.add(firstNum);
num1 = new TextField(6);
super.add(num1);
secondNum = new Label("Second Integer Number");
super.add(secondNum);
num2 = new TextField(6);
super.add(num2);
thirdNum = new Label("Third Integer Number");
super.add(thirdNum);
num3 = new TextField(6);
super.add(num3);
enter = new Button("Calculate");
super.add(enter);
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
number1 = Integer.parseInt(num1.getText());
number2 = Integer.parseInt(num2.getText());
number3 = Integer.parseInt(num3.getText());
int max = Math.max(Math.max(number1, number2), number3);
int min = Math.min(Math.min(number1, number2), number3);
result = new Label("Max is: " + max + ", min is " + min);
super.add(result);
}
}