📄 mainwindow.java
字号:
package com.dbhunter.ga;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author dbhunter
*
*/
public class MainWindow extends WindowAdapter implements ActionListener{
private static final long serialVersionUID = 1L;
private static final int MAXHEIGHT = 700,MAXWIDTH = 800;
private String IMAGESTR = "images/dbhuntericon.jpg";
private MainMenuBar mainMenuBar;
private InputPanel inputPanel ;
private PrintPanel printPanel ;
private String strIcon;
private int funID;
private char codeID;
private int selectionID;
private int crossID;
private int genovariationID;
private int populationNum;
private int iterateNum ;
private int showStyle;
private double pc; //cross 0.6~1.0
private double pv ; //variation 0.005~0.01
private int precision;
public MainWindow(){
JFrame jfr = new JFrame("GA");
jfr.setSize(MAXWIDTH, MAXHEIGHT);
jfr.setLocation(60, 0);
Container contentpane = jfr.getContentPane();
contentpane.setLayout(new BorderLayout());
mainMenuBar = new MainMenuBar();
jfr.setJMenuBar(mainMenuBar);
JPanel upPanel = new JPanel();
upPanel.setLayout(new BorderLayout());
inputPanel = new InputPanel();
printPanel = new PrintPanel();
mainMenuBar.getMiExit().addActionListener(this);
mainMenuBar.getMiAbout().addActionListener(this);
inputPanel.getCbFun().addActionListener(this);
printPanel.getExeButton().addActionListener(this);
contentpane.add(inputPanel,BorderLayout.NORTH);
contentpane.add(printPanel,BorderLayout.CENTER);
jfr.setVisible(true);
jfr.addWindowListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==mainMenuBar.getMiExit()){
System.exit(0);
}
else if(e.getSource()==mainMenuBar.getMiAbout()){
JFrame frameAbout = new JFrame();
frameAbout.setSize(400,200);
frameAbout.setLayout(new BorderLayout());
Container contentpane = frameAbout.getContentPane();
JPanel panelInstruction = new JPanel();
JPanel panelicon = new JPanel();
panelInstruction.setLayout(new GridLayout(8,1));
JLabel labelnull = new JLabel("");
JLabel labelAuthor = new JLabel("author: dbhunter");
JLabel labelWebsite = new JLabel("www.dbhunter.com");
JLabel labelEmain = new JLabel("E-main: dbhunter@126.com");
JLabel labelschool = new JLabel("安徽大学计算机科学与技术学院");
JLabel labeldate = new JLabel("2008-4-12");
JLabel labelicon = new JLabel(new ImageIcon(IMAGESTR));
panelicon.add(labelicon);
panelInstruction.add(labelnull);
panelInstruction.add(labelnull);
panelInstruction.add(labelnull);
panelInstruction.add(labelnull);
panelInstruction.add(labelAuthor);
panelInstruction.add(labelEmain);
panelInstruction.add(labelWebsite);
panelInstruction.add(labelschool);
panelInstruction.add(labeldate);
contentpane.add(panelicon,BorderLayout.WEST);
contentpane.add(panelInstruction,BorderLayout.CENTER);
frameAbout.setLocation(200, 200);
frameAbout.setVisible(true);
System.out.println(selectionID+" "+crossID+" "+genovariationID);
}
else if(e.getSource()==inputPanel.getCbFun()){
strIcon = "images/"+inputPanel.getCbFun().getSelectedIndex()+".jpg";
inputPanel.setFunIconShow(strIcon);
}
else if(e.getSource()==printPanel.getExeButton()){
this.populationNum = Integer.valueOf(inputPanel.getSpinPopulationNum().getValue().toString()).intValue();
this.pc = Double.parseDouble(inputPanel.getSpinCrossP().getValue().toString());
this.pv = Double.parseDouble(inputPanel.getSpinGenovariationP().getValue().toString());
this.precision = Integer.valueOf(inputPanel.getSpinPrecision().getValue().toString()).intValue();
this.iterateNum = Integer.valueOf(inputPanel.getSpinIterateNum().getValue().toString()).intValue();
if(mainMenuBar.getRbmiBinaryCode().isSelected()){
this.codeID = 'b';
}
else if(mainMenuBar.getRbmiDecimalCode().isSelected()){
this.codeID = 'd';
}
if(mainMenuBar.getRbmiRoulette().isSelected()){
this.selectionID=1;
}
if(mainMenuBar.getRbmiOnepointcrossover().isSelected()){
this.crossID = 1;
}
else if(mainMenuBar.getRbmiGoodPoint().isSelected()){
this.crossID = 2;
}
if(mainMenuBar.getRbmiPoint().isSelected()){
this.genovariationID = 1;
}
if(printPanel.getRbmisimpal().isSelected()){
showStyle=1;
}
else if(printPanel.getRbmidetail().isSelected()){
showStyle=2;
}
this.funID = inputPanel.getCbFun().getSelectedIndex();
if(this.funID==0){
JOptionPane.showMessageDialog(null,"请选择要评估函数");
inputPanel.getCbFun().setFocusable(true);
}
else{
new Thread(new Runnable() {
public void run()
{
Function ef = new Function(funID,codeID,precision);
Population population = new Population(populationNum,iterateNum,selectionID,crossID,genovariationID,pc,pv,ef,showStyle);
Gene[] gene = population.getGenes();
printPanel.clear();
printPanel.append("-------------------------------------------------------------------------【初始种群】--------------------------------------------------------------------------------\n");
printPanel.append(population.getSbInitGenes().toString());
printPanel.append("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-~~-【最后一代种群】-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printPanel.append(ef.getSbLastGenes(showStyle, gene).toString());
printPanel.append(ef.getSbBestGeneetail(population).toString());
}
}).start();
}
}
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public static void main(String[] args) {
new MainWindow();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -