简单窗口加文本框按钮实现选择数据最小值

/*
 * 窗口创建,简单文本框用于输入数据,按钮用于事件监听,实现选择数据最小值功能
 */
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;

public class ArrayMinValue extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
            try {
             ArrayMinValue frame = new ArrayMinValue();
             frame.setVisible(true);//设置界面可见
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });
    }
    /**
     * Create the frame.
     */
    public ArrayMinValue() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//界面的关闭
        setBounds(100, 100, 450, 300);//创建窗体大小
        contentPane = new JPanel();//添加容器窗体
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));//定义布局,这里为绝对布局,也就是空
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();//定义文本框
        textField.setBounds(36, 63, 375, 50);//文本框大小
        contentPane.add(textField);
        textField.setColumns(100);//文本框的字体多少

        JButton button = new JButton("计算");//添加按钮
        button.setBounds(171, 173, 104, 23);//定义按钮位置与大小
        button.addActionListener(new ActionListener() {//按钮事件监听

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根
                String str = new String();
                str = textField.getText();
                String[] s1 = new  String[1010];
                s1 = str.split(" ");
                int min,x;
                min = Integer.MAX_VALUE;
                for(String s:s1) {
                  x = Integer.parseInt(s);
                  if( min>x )
                    min = x;
                }
                textField.setText(Integer.toString(min));//在文本框中输出结果
            }
        });
        contentPane.add(button);//将按钮添加到界面
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39259536/article/details/81667874