📄 mwindow.java
字号:
//界面窗口
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mwindow extends JFrame implements ActionListener,WindowListener
{
private static final int WIDTH=400;
private static final int HEIGHT=300;
public Container content=null;
private String str="maopao";
private JTextField text1=new JTextField(100);
private JTextField text2=new JTextField(100);
public static int num=0;
public Mwindow()
{
super();
setTitle("Sort");
setSize(WIDTH,HEIGHT);
Dimension tmp=getToolkit().getScreenSize();
setLocation((tmp.width-WIDTH)/2,(tmp.height-HEIGHT)/2);
addWindowListener(this);
setResizable(false);
content=getContentPane();
//组件:
JLabel lable1=new JLabel("请输入需要排序的数字 以空格隔开:");
JLabel lable2=new JLabel("排序的结果为:");
text2.setEditable(false);
text1.addActionListener(this);
text2.addActionListener(this);
JRadioButton b1=new JRadioButton("冒泡排序",true);
b1.setActionCommand("maopao");
JRadioButton b2=new JRadioButton("选择排序",false);
b2.setActionCommand("xuanze");
JRadioButton b3=new JRadioButton("插入排序",false);
b3.setActionCommand("charu");
JRadioButton b4=new JRadioButton("快速排序",false);
b4.setActionCommand("kuaisu");
JRadioButton b5=new JRadioButton("堆排序",false);
b5.setActionCommand("dui");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
group.add(b3);
group.add(b4);
group.add(b5);
JButton button=new JButton("开始排序");
button.addActionListener(this);
JButton button2=new JButton("清空结果");
button2.addActionListener(this);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
panel1.setLayout(new GridLayout(4,1));
panel2.setLayout(new FlowLayout());
panel3.setLayout(new FlowLayout());
content.setLayout(new GridLayout(3,1));
panel1.add(lable1);
panel1.add(text1);
panel1.add(lable2);
panel1.add(text2);
panel2.add(new JLabel("选择使用的排序方式:"));
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(b4);
panel2.add(b5);
panel3.add(button);
panel3.add(button2);
content.add(panel1);
content.add(panel2);
content.add(panel3);
setVisible(true);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e) {}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()!="开始排序"&&e.getActionCommand()!="清空结果")str=e.getActionCommand();
else if(e.getActionCommand()=="开始排序")
{
text2.setText(null);
String str1=text1.getText();
if(str1.trim().length()<=0)
{
MessageBox Blank=new MessageBox("警告","请正确的输入需要排列的数字");
Blank.setVisible(true);
}
else
{
try
{
double [] arrayneedsort=Convert.stringtoarray(str1);
//这里接受到的字符串Str1不一定可以正确的转换为数组
//System.out.println("共"+arrayneedsort.length+"个数字排序");
setTitle("共"+arrayneedsort.length+"个数字排序");
if(str.equals("maopao"))
{BubbleSort temp=new BubbleSort(arrayneedsort);text2.setText(Convert.arraytostring(temp.getresult()));}
else if(str.equals("xuanze"))
{SelectionSort temp=new SelectionSort(arrayneedsort);text2.setText(Convert.arraytostring(temp.getresult()));}
else if(str.equals("kuaisu"))
{QuickSort temp=new QuickSort(arrayneedsort);text2.setText(Convert.arraytostring(temp.getresult()));}
else if(str.equals("dui"))
{HeapSort temp=new HeapSort(arrayneedsort);text2.setText(Convert.arraytostring(temp.getresult()));}
else if(str.equals("charu"))
{InsertSort temp=new InsertSort(arrayneedsort);text2.setText(Convert.arraytostring(temp.getresult()));}
else
{
MessageBox Blank=new MessageBox("警告","未知程序错误");
Blank.setVisible(true);
}
}
catch (NullPointerException e2)
{
MessageBox Blank=new MessageBox("警告","请正确的输入需要排列的数字");
Blank.setVisible(true);
}
}
}
else if(e.getActionCommand()=="清空结果")
{
text2.setText("");
//setTitle("sort");
}
else
{
MessageBox Blank=new MessageBox("警告","程序错误");
Blank.setVisible(true);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -