这是运行效果
上代码:
package com.sh.lw;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TestFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = -3792557927147777060L;
private javax.swing.JTextField field0 = new javax.swing.JTextField(20);
private javax.swing.JTextField field1 = new javax.swing.JTextField(20);
private javax.swing.JTextField field2 = new javax.swing.JTextField(20);
private javax.swing.JTextField field3 = new javax.swing.JTextField(20);
private javax.swing.JButton max = new javax.swing.JButton("最大值");
private javax.swing.JButton min = new javax.swing.JButton("最小值");
int num1,num2,num3;
public TestFrame(){
java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
java.awt.GridBagConstraints gbc = new java.awt.GridBagConstraints();
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setLayout(layout);
javax.swing.JLabel lable0 = new javax.swing.JLabel("第一个数:");
layout.setConstraints(lable0, gbc);
panel.add(lable0);
this.add(panel);
gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
layout.setConstraints(field0, gbc);
panel.add(field0);
javax.swing.JLabel label1 = new javax.swing.JLabel("第二个数:");
唤掘
gbc.gridwidth 毁链州= java.awt.GridBagConstraints.RELATIVE;
layout.setConstraints(label1, gbc);
panel.add(label1);
gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
layout.setConstraints(field1, gbc);
panel.add(field1);
javax.swing.JLabel label2 = new javax.swing.JLabel("第三个数:");
gbc.gridwidth = java.awt.GridBagConstraints.RELATIVE;
layout.setConstraints(label2, gbc);
panel.add(label2);
gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
layout.setConstraints(field2, gbc);
panel.add(field2);
纤蔽
javax.swing.JLabel lable3 = new javax.swing.JLabel("结果:");
gbc.gridwidth = java.awt.GridBagConstraints.RELATIVE;
layout.setConstraints(lable3, gbc);
panel.add(lable3);
gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
layout.setConstraints(field3, gbc);
panel.add(field3);
gbc.gridwidth = java.awt.GridBagConstraints.RELATIVE;
layout.setConstraints(max, gbc);
panel.add(max);
gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
layout.setConstraints(min, gbc);
panel.add(min);
this.setVisible(true);
this.pack();
this.setResizable(false);
max.addActionListener(new ButtonListener());
min.addActionListener(new ButtonListener());
field3.setEditable(false);
this.addWindowListener(new MyWindowClosing());
}
class MyWindowClosing extends java.awt.event.WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
java.lang.System.exit(0);
}
}
class ButtonListener implements java.awt.event.ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String text0 = field0.getText();
String text1 = field1.getText();
String text2 = field2.getText();
if(null == text0 || "".equals(text0.trim())){
JOptionPane.showMessageDialog(null, "请输入第一个数!");
return;
}else{
try{
num1 = Integer.parseInt(text0.trim());
}catch(java.lang.NumberFormatException ne){
JOptionPane.showMessageDialog(null, "第一个数必须为数字!");
return;
}
}
if(null == text1 || "".equals(text1.trim())){
JOptionPane.showMessageDialog(null, "请输入第二个数!");
return;
}else{
try{
num2 = Integer.parseInt(text1.trim());
}catch(java.lang.NumberFormatException ne){
JOptionPane.showMessageDialog(null, "第二个数必须为数字!");
return;
}
}
if(null == text2 || "".equals(text2.trim())){
JOptionPane.showMessageDialog(null, "请输入第三个数!");
return;
}else{
try{
num3 = Integer.parseInt(text2.trim());
}catch(java.lang.NumberFormatException ne){
JOptionPane.showMessageDialog(null, "第三个数必须为数字!");
return;
}
}
Object source = e.getSource();
int arr[] = new int[]{num1,num2,num3};
int result = arr[0];
if(source == max){
for(int i=1; i<arr.length; i++){
if(arr[i] > result){
result = arr[i];
}
}
}else if(source == min){
for(int i=1; i<arr.length; i++){
if(arr[i] < result){
result = arr[i];
}
}
}
field3.setText(new Integer(result).toString());
}
}
public static void main(String[] args) {
new TestFrame();
}
}
2013-02-04 · 知道合伙人数码行家
你这个要用到Swing编程吧。这个网上是有例子的。