📄 bsdialog.java
字号:
package datastructure;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//类dataDialog用于建立演示数据
class bsDialog
extends JDialog
implements ActionListener {
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
private JButton jButton3 = new JButton();
private JButton jButton4 = new JButton();
private JTextField jTextField1 = new JTextField();
private JLabel jLabel1 = new JLabel();
//用于保存演示数据
static int a[] = {
4, 153, 19, 199, 61, 158, 28, 97, 144, 75, 11, 88, 180};
public bsDialog(JFrame frame, String title, boolean mode) {
super(frame, title, mode);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
setSize(400, 300);
setLocation(100, 150);
setVisible(false);
jButton1.setBounds(new Rectangle(16, 206, 98, 25));
jButton1.setToolTipText("随机生成演示数据");
jButton1.setVerifyInputWhenFocusTarget(true);
jButton1.setText("随机生成");
jButton1.addActionListener(this);
this.getContentPane().setLayout(null);
jButton2.setBounds(new Rectangle(122, 206, 71, 25));
jButton2.setToolTipText("演示数据为递增");
jButton2.setText("升序");
jButton2.addActionListener(this);
this.getContentPane().setBackground(SystemColor.activeCaptionBorder);
jButton3.setBounds(new Rectangle(203, 207, 73, 25));
jButton3.setToolTipText("演示数据为递减");
jButton3.setText("降序");
jButton3.addActionListener(this);
jButton4.setBounds(new Rectangle(284, 208, 72, 25));
jButton4.setText("确定");
jButton4.addActionListener(this);
jTextField1.setToolTipText("请输入合法数据");
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(19, 104, 332, 39));
jTextField1.addActionListener(this);
jLabel1.setText("输入1到200之间的整形数据,用逗号分开. (最大个数为13个)");
jLabel1.setBounds(new Rectangle(19, 24, 355, 37));
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jButton4, null);
this.getContentPane().add(jButton3, null);
this.getContentPane().add(jButton2, null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton1) {
int number = (int) (Math.random() * 2 + 11);
String s = "";
int a[] = new int[number];
int tag = 0;
boolean flag = false;
for (int i = 0; i < a.length; i++)
a[i] = 0;
for (int i = 0; i < number; i++) {
int p = (int) (Math.random() * 180 + 5);
for (int j = 0; j < tag; j++) {
if (p == a[j])
flag = true;
else
flag = false;
}
a[tag] = p;
tag++;
if (!flag) {
if (i != number - 1)
s = s + String.valueOf(p) + ",";
else
s = s + String.valueOf(p);
}
}
jTextField1.setText(s);
}
else if (e.getSource() == jButton2) {
String s = "";
int p = -12;
for (int i = 0; i < 13; i++) {
p = p + 16;
if (i != 12)
s = s + String.valueOf(p) + ",";
else
s = s + String.valueOf(p);
}
jTextField1.setText(s);
}
else if (e.getSource() == jButton3) {
String s = "";
int p = 214;
for (int i = 0; i < 13; i++) {
p = p - 16;
if (i != 12)
s = s + String.valueOf(p) + ",";
else
s = s + String.valueOf(p);
}
jTextField1.setText(s);
}
else if (e.getSource() == jButton4) {
//用于表示数组a的下标
int tag = 0;
String s = jTextField1.getText().trim();
if (s.equals("")) {
JOptionPane.showConfirmDialog(this,
" 演示数据不能为空\n \n 请从新输入正确的数据\n",
"出错信息", JOptionPane.DEFAULT_OPTION);
return;
}
StringTokenizer str = new StringTokenizer(s, " ,");
int number = str.countTokens();
a = new int[number];
while (str.hasMoreTokens()) {
String s1 = str.nextToken();
try {
a[tag] = Integer.parseInt(s1);
if (a[tag] < 1 || a[tag] > 200) {
JOptionPane.showConfirmDialog(this,
" 输入的数据超出范围\n \n 请从新输入正确的数据\n",
"出错信息", JOptionPane.DEFAULT_OPTION);
return;
}
tag++;
}
catch (NumberFormatException e1) {
JOptionPane.showConfirmDialog(this,
" 输入的数据不合法\n \n 请从新输入正确的数据\n",
"出错信息", JOptionPane.DEFAULT_OPTION);
return;
}
}
if (tag >= 14) {
JOptionPane.showConfirmDialog(this,
" 输入的数据个数大于13个\n \n 请从新输入正确的数据\n",
"出错信息", JOptionPane.DEFAULT_OPTION);
return;
}
setVisible(false);
}
}
public int[] getResult()
{
return a;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -