📄 maxsort.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MaxSort extends Applet implements ActionListener
{
Label label1=new Label("请输入数据:");
Label label2=new Label("提示信息: 一切状态很正常!");
TextField text=new TextField(4);
Button buttonInput=new Button("输入");
Button buttonSort=new Button("排序");
Button buttonReset=new Button("复位");
int[] E=new int[8];
int[] F=new int[8];
int max=0;
int index=0;
int input=0;
int k=0;
boolean state=true;
Font MyFont;
public void init()
{
add(label1);label1.setForeground(Color.red);
add(text);text.setBackground(Color.green);
buttonInput.addActionListener(this);
add(buttonInput);buttonInput.setBackground(Color.orange);
buttonInput.setForeground(Color.green);
buttonSort.addActionListener(this);
add(buttonSort);buttonSort.setBackground(Color.blue);
buttonSort.setForeground(Color.red);
buttonReset.addActionListener(this);
add(buttonReset);buttonReset.setBackground(Color.yellow);
buttonReset.setForeground(Color.cyan);
add(label2);label2.setForeground(Color.green);
MyFont=new Font("TimesRoman",Font.PLAIN,20);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.setFont(MyFont);
g.drawString("原始数据:",10,80);
for(int i=0;i<E.length;i++)
g.drawString(Integer.toString(F[i])+" ",100+i*40,80);
if(!state)
{
g.setColor(Color.blue);
g.drawString("排序结果:",10,120);
for(int i=0;i<E.length;i++)
g.drawString(Integer.toString(E[i])+" ",100+i*40,120);
}
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==buttonInput)
{
if(input<E.length)
{
E[input]=Integer.parseInt(text.getText());
F[k]=E[input];
input++;
k++;
text.setText("");
repaint();
}
else
{
state=false;
input=0;
k=0;
text.setVisible(false);
buttonInput.setVisible(false);
}
}
if(event.getSource()==buttonSort)
{
if(state)
label2.setText("数据输入未完,请继续输入!");
else
{
SortProcedure();
label2.setText("排序完毕,请看结果!");
repaint();
}
}
if(event.getSource()==buttonReset)
{
text.setVisible(true);
text.setText("");
label2.setText("状态提示:已复位!");
state=true;
input=k=0;
buttonInput.setVisible(true);
for(int i=0;i<E.length;i++)
F[i]=E[i]=0;
repaint();
}
}
void SortProcedure()
{
int n=E.length;
while(n>0)
{
max=E[n-1];
index=n-1;
for(int i=0;i<n;i++)
{
if(max<E[i])
{
max=E[i];
index=i;
}
}
n--;
if(index<n)
{
E[index]=E[n];
E[n]=max;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -