今天做了一个需要创建到界面、获取输入最小值的例子,涉及到很多新的知识点。还在慢慢熟悉它的用法。
主要用到"java.awt.*"和"javax.swing.*"这两个软件包里面的类方法等
比如:EventQueue,JButton,JFrame,JPanel,JLabel,JOptionPane,JTextField,border等
先放运行结果界面:
代码:
package com.lixiyu;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class ArrayMinValue extends JFrame {
/**
*
*/
private static final long serialVersionUID = -8388043412533827271L;
private JPanel contentPane;
private JTextField textField;
private JLabel label;
private JLabel label_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
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, 149);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setTitle("获取一维数组的最小值");
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(6, 36, 414, 30);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("\u8BA1\u7B97");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
button.setBounds(16, 76, 90, 30);
contentPane.add(button);
label = new JLabel("最小值:");
label.setBounds(116, 82, 304, 18);
contentPane.add(label);
label_1 = new JLabel(
"请在文本框中输入多个整数,以空格为分隔符。例如:3 5 2 562 125");
label_1.setBounds(6, 6, 422, 18);
contentPane.add(label_1);
}
protected void do_button_actionPerformed(ActionEvent e) {
String arrayStr = textField.getText().trim(); //去除左右空格
if(arrayStr.equals("")){
JOptionPane.showMessageDialog(null, "请输入数字内容");
return;
}
for (int i = 0; i < arrayStr.length(); i++) { // 过滤非法输入
char charAt = arrayStr.charAt(i);
if (!Character.isDigit(charAt) && charAt != ' ') {
JOptionPane.showMessageDialog(null, "输入包含非数字内容");
textField.setText("");
return;
}
}
String[] numStrs = arrayStr.split(" {1,}"); // 分割字符串
int[] numArray = new int[numStrs.length]; // 创建整型数组
// 转换输入为整型数组
for (int i = 0; i < numArray.length; i++) {
numArray[i] = Integer.valueOf(numStrs[i]);
}
int min = numArray[0]; // 创建最小数变量
for (int j = 0; j < numArray.length; j++) {
if (min > numArray[j]) { // 提取最小整数
min = numArray[j];
}
}
label.setText("数组中最小的数是:" + min); //显示最小值到指定的标签中
}
}
详细分析:
1.serialVersionUID
private static final long serialVersionUID = -8388043412533827271L;//序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
2.UIManager
UIManager .setLookAndFeel
("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//Nimbus风格外观
3.打印异常(要打印所有异常和错误, 应该catch Throwable而不是catch Exception.)
try {
...
}throw(Exception e){
e.printStackTrace();
}
4.EventQueue
/*导致 runnable 的 run
方法在 the system EventQueue 的指派线程中被调用。
参数:runnable - Runnable 对象,其 run 方法应该在 EventQueue 上同步
执行*/
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ArrayMinValue frame = new ArrayMinValue();//创建窗
体
frame.setVisible(true);//可见性
} catch (Exception e) {
e.printStackTrace();
}
}
});
5.EXIT_ON_CLOSE窗口关闭
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出应用程序后的默认窗口关闭操作。
6.setBounds用法
setBounds(100, 100, 450, 149); /*setBounds(int x, int y, int width, int height) 前两个是组件左上角在容器中的坐标 后两个是组件的宽度和高度*/
7.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
/*设置面板的边界,Border描述了面板四周的边界(属于面板内部),
EmptyBorder是一个空白的边界;
语句的意思是让contentPane内部边框为空,并且有5个像素的厚度,如果直接在
contentPane上面添加一个按钮(设置为充满),那么按钮将铺满除了边框之外
的内部矩形*/
8.setContentPane(contentPane);
setContentPane(contentPane);
/* JpanelcontentPane=newJpanel();
……//把其它组件添加到Jpanel中;
frame.setContentPane(contentPane);
//把contentPane对象设置成为frame的内容面板
*/
9.contentPane.setLayout(null);
contentPane.setLayout(null);//设置布局管理器,null表示什么布局器名称都不选
10.actionPerformed
public void actionPerformed(ActionEvent e) {//发生操作时调用
转载于:https://blog.51cto.com/lixiyu/1303209