📄 nick.java
字号:
package main;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import main.myClass.*;
import main.*;
public class Nick extends JFrame{
JMenuBar mb=new JMenuBar();
JMenu fileMenu=new JMenu("文件"),
sortMethodMenu=new JMenu("排序"),
orderMenu=new JMenu("初态"),
amountMenu=new JMenu("个数"),
controlMenu=new JMenu("控制"),
aboutMenu=new JMenu("帮助");
myVector contain=new myVector();
myHashtable algorithm=new myHashtable();
JTabbedPane tab=new JTabbedPane();
JPanel content=new JPanel(true);
ResultPanel result=new ResultPanel(this);
myTable table=new myTable(result,algorithm);
Report report=new Report(algorithm);
myPanel my;
int amount=10;
String order="顺序";
Data data=new Data(amount,order);
public Nick(){
//标题
super("排序演示");
makeFileMenu(fileMenu);
makeSortMethodMenu(sortMethodMenu);
makeOrderMenu(orderMenu);
makeAmountMenu(amountMenu);
makeControlMenu(controlMenu);
makeAboutMenu(aboutMenu);
init();
}
private void init(){
setSize(600,400);
setLocation(100,100);
//为菜单条添加子菜单
mb.setLayout(new GridLayout(0,7));
mb.add(fileMenu);
mb.add(sortMethodMenu);
mb.add(amountMenu);
mb.add(orderMenu);
mb.add(controlMenu);
mb.add(aboutMenu);
setJMenuBar(mb);
tab.add("排序",new JScrollPane(content));
tab.add("直方图",new Histogram(result));
tab.add("饼图",new CircleResult(result));
tab.add("表格",table);
getContentPane().add(tab,BorderLayout.CENTER);
content.setBackground(Color.white);
content.setFont(new Font("宋体",0,14));
content.setLayout(new BoxLayout(content,BoxLayout.X_AXIS));
setVisible(true);
}
//---------------------------------------------------------------------------
//构造fileMenu菜单
private void makeFileMenu(JMenu menu){
String[] item=new String[]{"报告","退出"},
command=new String[]{"Report","Exit"};
for(int i=0;i<item.length;i++){
JMenuItem temp=menu.add(new JMenuItem(item[i]));
temp.setActionCommand(command[i]);
temp.addActionListener(new File_A());
}
}
//处理fileMenu的事件
private class File_A implements ActionListener{
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
//添加Report菜单要处理的代码
if(command.equals("Report")){
report.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
report.hide();
report.dispose();
}});
report.setSize(getSize().width,350);
report.setLocation(100,150);
report.show();
}
//添加Exit菜单要处理的代码
else if(command.equals("Exit")){
System.exit(0);
}
}
}
//---------------------------------------------------------------------------
//构造sortMethodMenu
private void makeSortMethodMenu(JMenu menu){
try{
algorithm=new myHashtable();
File directory=new File("./main/algorithm");
String[] file=directory.list(new ClassFileFilter());
String b,c;
Gracie g;
for(int i=0;i<file.length;i++){
b=file[i].substring(0,file[i].lastIndexOf("."));
Class temp=Class.forName("main.algorithm."+b);
Class superClass=temp.getSuperclass();
b=superClass.toString();
c=b.substring(b.lastIndexOf(".")+1,b.length());
if(c.equals("Gracie")){
g=(Gracie)(temp.newInstance());
algorithm.put(g.getName(),g.getCommand());
}
}
}
catch(Exception e){
e.printStackTrace();
}
table.init(algorithm);
report.init(algorithm);
int size=algorithm.size();
for(int i=0;i<size;i++){
String key=algorithm.getKey(i);
String ele=algorithm.getValue(i);
JCheckBoxMenuItem temp=(JCheckBoxMenuItem)menu.add(new JCheckBoxMenuItem(key));
temp.setActionCommand(ele);
temp.addActionListener(new SortMethod_A());
}
/*
JCheckBoxMenuItem temp=(JCheckBoxMenuItem)menu.add(new JCheckBoxMenuItem("全选"));
temp.setActionCommand("All");
temp.addActionListener(new SortMethod_A());
*/
}
// 处理sortMethod菜单的事件
private class SortMethod_A implements ActionListener{
public void actionPerformed(ActionEvent e){
String name=((JCheckBoxMenuItem)e.getSource()).getText();
String command=e.getActionCommand();
if(!command.equals("All")){
my=new myPanel(new String[]{name,command},data,result,content);
if(((JCheckBoxMenuItem)e.getSource()).getState()){
contain.put(my);
content.add(my);
}
else{
int a=contain.remove(my);
my.finalize(a);
}
}/*
else{
int Mcount=sortMethodMenu.getItemCount();
if(((JCheckBoxMenuItem)e.getSource()).getState()){
for(int i=0;i<Mcount-1;i++){
JMenuItem temp=sortMethodMenu.getItem(i);
if(!temp.isSelected()){
name=temp.getText();
command=temp.getActionCommand();
my=new myPanel(new String[]{name,command},data,result,content);
contain.put(my);
content.add(my);
temp.setSelected(true);
}
}
}
else{
for(int i=0;i<Mcount-1;i++){
JMenuItem temp=sortMethodMenu.getItem(i);
temp.setSelected(false);
my=new myPanel(new String[]{temp.getText(),temp.getActionCommand()},data,result,content);
int a=contain.remove(my);
my.finalize(a);
}
contain=new myVector();
}
}
*/
}
}
//----------------------------------------------------------------------------
//构造orderMenu菜单
private void makeOrderMenu(JMenu menu){
String[] item=new String[]{"顺序","逆序","伪随机","随机"},
command=new String[]{"顺序","逆序","伪随机","随机"};
ButtonGroup bg=new ButtonGroup();
for(int i=0;i<item.length;i++){
JRadioButtonMenuItem temp=(JRadioButtonMenuItem)menu.add(new JRadioButtonMenuItem(item[i]));
bg.add(temp);
temp.setActionCommand(command[i]);
if(i==0) temp.setSelected(true);
temp.addActionListener(new Order_A());
}
}
//处理orderMenu的事件
private class Order_A implements ActionListener{
public void actionPerformed(ActionEvent e){
order=e.getActionCommand();
data.update(amount,order);
}
}
//---------------------------------------------------------------------------
//构造amoutMenu
private void makeAmountMenu(JMenu menu){
String[] item=new String[]{"10","50","100","自定义"},
command=new String[]{"10","50","100","自定义"};
ButtonGroup bg=new ButtonGroup();
for(int i=0;i<item.length;i++){
JRadioButtonMenuItem temp=(JRadioButtonMenuItem)menu.add(new JRadioButtonMenuItem(item[i]));
bg.add(temp);
temp.setActionCommand(command[i]);
if(i==0) temp.setSelected(true);
temp.addActionListener(new Amount_A());
}
}
//处理sortMenu的事件
private class Amount_A implements ActionListener{
public void actionPerformed(ActionEvent e){
if(!e.getActionCommand().equals("自定义")){
amount=Integer.parseInt(e.getActionCommand());
}
else{
String temp=JOptionPane.showInputDialog(Nick.this,"输入待排序数组的元素个数(0~120)");
try{
int tem=Integer.parseInt(temp);
if(tem>120)
JOptionPane.showMessageDialog(Nick.this,("输入数字过大"+temp+",请重新输入"),"输入错误",JOptionPane.ERROR_MESSAGE);
else amount=tem;
}
catch(NumberFormatException f){
JOptionPane.showMessageDialog(Nick.this,("输入非正确内容"+temp+",请输入数字"),"输入错误",JOptionPane.ERROR_MESSAGE);
}
}
data.update(amount,order);
}
}
//----------------------------------------------------------------------------
//构造controlMenu
private void makeControlMenu(JMenu menu){
String[] item=new String[]{"全部开始","全部停止"},
command=new String[]{"Start","Stop"};
ButtonGroup bg=new ButtonGroup();
for(int i=0;i<item.length;i++){
JRadioButtonMenuItem temp=(JRadioButtonMenuItem)menu.add(new JRadioButtonMenuItem(item[i]));
bg.add(temp);
temp.setActionCommand(command[i]);
if(i==1) temp.setSelected(true);
temp.addActionListener(new Control_A());
}
}
//处理controlMenu的事件
private class Control_A implements ActionListener{
public void actionPerformed(ActionEvent e){
myPanel[] i=contain.mypanels();
String command=e.getActionCommand();
if(command.equals("Start")){
tab.setSelectedIndex(0);
tab.setEnabledAt(1,false);
tab.setEnabledAt(2,false);
tab.setEnabledAt(3,false);
result.init(new String[]{order,String.valueOf(amount)});
for(int j=0;i[j]!=null && j<i.length;j++)
i[j].start();
}
else{
for(int j=0;i[j]!=null && j<i.length;j++)
i[j].stop();
}
}
}
//---------------------------------------------------------------------------
//构造about菜单
private void makeAboutMenu(JMenu menu){
String[] item=new String[]{"关于","添加算法"},
command=new String[]{"关于","添加算法"};
for(int i=0;i<item.length;i++){
JMenuItem temp=menu.add(new JMenuItem(item[i]));
temp.setActionCommand(command[i]);
temp.addActionListener(new About_A());
}
}
//处理about菜单的事件
private class About_A implements ActionListener{
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
if(command.equals("关于"))
JOptionPane.showMessageDialog(Nick.this,new Object[]{new JLabel("本程序界面由Nick完成"),new JLabel("本程序算法由Gracie完成")},"关于…………",JOptionPane.INFORMATION_MESSAGE);
else{
try{
JFileChooser choose=new JFileChooser(".");
choose.setFileFilter(new ClassFileFilter());
int result=choose.showDialog(Nick.this,"选择");
if(result==JFileChooser.APPROVE_OPTION){
File source=choose.getSelectedFile();
File des=new File("./main/algorithm/"+source.getName());
BufferedInputStream in=new BufferedInputStream(new DataInputStream(new FileInputStream(source)));
BufferedOutputStream out=new BufferedOutputStream(new DataOutputStream(new FileOutputStream(des)));
int temp;
while((temp=in.read())!=-1){
out.write(temp);
}
in.close();
out.close();
}
}
catch(Exception f){f.printStackTrace();}
sortMethodMenu.removeAll();
makeSortMethodMenu(sortMethodMenu);
content.removeAll();
content.repaint();
contain=new myVector();
}
}
}
public static void main(String[] args){
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e){e.printStackTrace();}
JFrame welcome=new WelcomePane();
welcome.setResizable(false);
welcome.setVisible(true);
while(welcome.isShowing());
JFrame frame=new Nick();
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -