本文不讲概念,直接上真实代码
一、效果图
效果图一.png
效果图二.png
二、代码
2.1、显示界面的Frame(WarDeployFrame.java)
package MainFrame;
import com.primeton.governor.management.DatabaseManageUtil;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
public class WarDeployFrame extends JFrame {
int i = 1;//记录步骤数
int total = 8;//总步骤数
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height =Toolkit.getDefaultToolkit().getScreenSize().height;
JPanel up_panel = new JPanel();
JPanel down_panel = new JPanel();
JPanel up_left_panel = new JPanel();
JPanel up_right_panel = new JPanel();
JPanel up_left_list_panel = new JPanel();
JButton last_button = new JButton("上一步");
JButton next_button = new JButton("下一步");
JButton cancle_button = new JButton("取消");
JButton finish_button = new JButton("完成");
JTextField descFile = new JTextField();//安装文件路径
public WarDeployFrame(){
last_button.setVisible(false);
finish_button.setVisible(false);
this.setTitle("数据质量V1.3正式版");
ImageIcon icon=new ImageIcon("C:\\primeton\\di62\\ico\\title_logo.png"); //xxx代表图片存放路径,2.png图片名称及格式
this.setIconImage(icon.getImage());
this.setSize(640,460);
this.setResizable(false);
this.setLocation(width/2-176,height/2-170);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon icon11=new ImageIcon("C:\\primeton\\di62\\ico\\background.gif");
JLabel label=new JLabel(icon11);
label.setBounds(0,0,640,460);
//获取窗口的第二层,将label放入
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
//获取frame的顶层容器,并设置为透明
JPanel j=(JPanel)this.getContentPane();
j.setOpaque(false);
//up_panel.setBorder(new TitledBorder(BorderFactory.createMatteBorder(0,0,1,1,Color.red),""));
//down_panel.setBorder(new TitledBorder("下"));
up_left_panel.setBorder(new TitledBorder(BorderFactory.createMatteBorder(0,0,1,0,Color.CYAN),""));
//up_right_panel.setBorder(new TitledBorder(""));
up_right_panel.setBorder(new TitledBorder(BorderFactory.createMatteBorder(0,1,1,0,Color.CYAN),""));
up_panel.setOpaque(false);
up_left_panel.setOpaque(false);
up_right_panel.setOpaque(false);
up_left_list_panel.setOpaque(false);
down_panel.setOpaque(false);
//对下面的区域作设置
down_panel.add(last_button);
down_panel.add(next_button);
down_panel.add(cancle_button);
down_panel.add(finish_button);
JLabel imageLable=new JLabel(new ImageIcon("C:\\primeton\\di62\\ico\\logo.gif"));
imageLable.setPreferredSize(new Dimension(170, 20));
up_left_panel.add(imageLable);
up_left_panel.add(up_left_list_panel);
BoxLayout layout = new BoxLayout(up_left_list_panel, BoxLayout.Y_AXIS);
up_left_list_panel.setLayout(layout);
up_left_list_panel.setBorder(BorderFactory.createEmptyBorder (0, 30, 0, 0));
up_left_list_panel.add(createNewLable("简介","C:\\primeton\\di62\\ico\\runing.png",SwingConstants.LEFT));
up_left_list_panel.add(createNewLable("安装路径","C:\\primeton\\di62\\ico\\close.png",SwingConstants.LEFT));
up_left_list_panel.add(createNewLable("选择应用服务器","C:\\primeton\\di62\\ico\\close.png",SwingConstants.LEFT));
up_left_list_panel.add(createNewLable("选择安装组件","C:\\primeton\\di62\\ico\\close.png",SwingConstants.LEFT));
up_left_list_panel.add(createNewLable("端口管理","C:\\primeton\\di62\\ico\\close.png",SwingConstants.LEFT));
up_left_list_panel.add(createNewLable("配置数据库","C:\\primeton\\di62\\ico\\close.png",SwingConstants.LEFT));
up_left_list_panel.add(createNewLable("安装中","C:\\primeton\\di62\\ico\\close.png",SwingConstants.LEFT));
up_left_list_panel.add(createNewLable("完成","C:\\primeton\\di62\\ico\\close.png",SwingConstants.LEFT));
// Box vBox01 = Box.createVerticalBox();
// up_left_list_panel.add(vBox01);
//BoxLayout layout=new BoxLayout(up_left_list_panel, BoxLayout.Y_AXIS);
//up_left_list_panel.setLayout(layout);
// Box hBox01 = Box.createHorizontalBox();
// hBox01.setPreferredSize(new Dimension(150, 40));
// hBox01.add(l1);
// vBox01.add(hBox01);
// Box hBox02 = Box.createHorizontalBox();hBox02.setPreferredSize(new Dimension(150, 40));
// hBox02.add(icon2);hBox02.add(first2);
// vBox01.add(hBox02);
up_right_panel.add(getPanelByID(1));
this.setLayout(new BorderLayout());
this.add(up_panel,BorderLayout.CENTER);
this.add(down_panel,BorderLayout.SOUTH);
up_panel.setLayout(new BorderLayout(0,0));
up_left_panel.setPreferredSize(new Dimension(180, 150));;
up_panel.add(up_left_panel,BorderLayout.WEST);
up_panel.add(up_right_panel,BorderLayout.CENTER);
//设置按钮点击事件
next_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
up_right_panel.getComponent(i-1).setVisible(false);
int compCount = up_right_panel.getComponentCount();
if(compCount>i){
up_right_panel.getComponent(i).setVisible(true);
JLabel tempLable2 = (JLabel)up_left_list_panel.getComponent(i-1);
tempLable2.setIcon(new ImageIcon("C:\\primeton\\di62\\ico\\finish.png"));
JLabel tempLable3 = (JLabel)up_left_list_panel.getComponent(i);
tempLable3.setIcon(new ImageIcon("C:\\primeton\\di62\\ico\\runing.png"));
++i;
}else{
up_right_panel.add(getPanelByID(++i));
JLabel tempLable = (JLabel)up_left_list_panel.getComponent(i-2);
tempLable.setIcon(new ImageIcon("C:\\primeton\\di62\\ico\\finish.png"));
JLabel tempLable4 = (JLabel)up_left_list_panel.getComponent(i-1);
tempLable4.setIcon(new ImageIcon("C:\\primeton\\di62\\ico\\runing.png"));
}
if(i>1){last_button.setVisible(true);}
if(i==total){last_button.setVisible(false);next_button.setVisible(false);cancle_button.setVisible(false);finish_button.setVisible(true);}
}
});
last_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(i>=2){
finish_button.setVisible(false);
next_button.setVisible(true);
up_right_panel.getComponent(i-1).setVisible(false);
up_right_panel.getComponent(i-2).setVisible(true);
if(i==2){last_button.setVisible(false);}
JLabel tempLable = (JLabel)up_left_list_panel.getComponent(i-2);
tempLable.setIcon(new ImageIcon("C:\\primeton\\di62\\ico\\runing.png"));
JLabel tempLable2 = (JLabel)up_left_list_panel.getComponent(i-1);
tempLable2.setIcon(new ImageIcon("C:\\primeton\\di62\\ico\\close.png"));
i--;
}
}
});
finish_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
cancle_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
public JLabel createNewLable(String text,String url,int horizontalAlignment){
JLabel tempLable =new JLabel(text, new ImageIcon(url), horizontalAlignment);
tempLable.setFont(new Font("宋体", Font.PLAIN, 12));
tempLable.setBorder(BorderFactory.createEmptyBorder (15, 0, 0, 0));
tempLable.setPreferredSize(new Dimension(170, 40));
return tempLable;
}
public JPanel getPanelByID(int index){
JPanel tempPanel = new JPanel();
tempPanel.setOpaque(false);
switch (index){
case 1:
JLabel label=new JLabel("<html>本向导将指引您完成EOS Platform 7.6的安装。<br><br>" +
"建议您在继续本次安装前退出所有应用程序。<br><br>" +
"请单击“下一步”按钮转到下一屏幕继续。安装过程中若需更改在<br>上一屏幕中输入的内容,可单击“上一步”按钮。您可以在安装过<br>程中随时单击“取消”按钮取消本次安装。</html>");
label.setFont(new Font("宋体", Font.PLAIN, 12));
label.setBorder(BorderFactory.createEmptyBorder (15, 20, 0, 0));
label.setPreferredSize(new Dimension(420, 200));
tempPanel.add(label);
break;
case 2:
JPanel tempPanel_case2_1 = new JPanel();tempPanel_case2_1.setOpaque(false);
JPanel tempPanel_case2_2 = new JPanel();tempPanel_case2_2.setOpaque(false);
BoxLayout layout=new BoxLayout(tempPanel, BoxLayout.Y_AXIS);
tempPanel.setLayout(layout);
JLabel label2=new JLabel("<html>安装程序将在以下文件夹中安装。<br><br>" +
"要安装到此文件夹,请单击“下一步”。要安装到其他文件夹,请单击<br>“浏览”或直接在文件框内输入安装路径。</html>");
label2.setFont(new Font("宋体", Font.PLAIN, 12));
label2.setBorder(BorderFactory.createEmptyBorder (15, 20, 0, 0));
label2.setPreferredSize(new Dimension(420, 200));
JLabel label3=new JLabel("目标文件夹:");
label3.setFont(new Font("宋体", Font.PLAIN, 14));
label3.setBorder(BorderFactory.createEmptyBorder (15, 20, 0, 0));
descFile.setPreferredSize(new Dimension(200, 28));
JButton button_case2_1 = new JButton("浏览(R)");
button_case2_1.setBounds(340,20,40,25);
button_case2_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();// 文件选择器
jfc.setFileSelectionMode(1);// 设定只能选择到文件夹
int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
if (state == 1) {
return;
} else {
File f = jfc.getSelectedFile();// f为选择到的目录
descFile.setText(f.getAbsolutePath());
}
}
});
tempPanel_case2_1.add(label2);
tempPanel_case2_2.add(label3);
tempPanel_case2_2.add(descFile);
tempPanel_case2_2.add(button_case2_1);
tempPanel_case2_2.setPreferredSize(new Dimension(420, 200));
tempPanel.add(tempPanel_case2_1);
tempPanel.add(tempPanel_case2_2);
break;
case 3:
JLabel label4=new JLabel("<html>安装包将帮助您完成在应用服务器上部署工作。<br><br>" +
"请从以下列表中选择应用服务器。</html>");
label4.setFont(new Font("宋体", Font.PLAIN, 12));
label4.setBorder(BorderFactory.createEmptyBorder (15, 20, 0, 0));
label4.setPreferredSize(new Dimension(420, 200));
tempPanel.add(label4);
break;
case 4:
break;
case 5:
break;
case 6:
BoxLayout layout_case7=new BoxLayout(tempPanel, BoxLayout.Y_AXIS);
tempPanel.setLayout(layout_case7);
tempPanel.add(createNewPanel01("数据源名称:"));
tempPanel.add(createNewPanel02("数据库类型:",tempPanel));
tempPanel.add(createNewPanel01("服务器IP:"));
tempPanel.add(createNewPanel01("服务器端口:"));
tempPanel.add(createNewPanel01("SID:"));
tempPanel.add(createNewPanel01("数据库用户:"));
tempPanel.add(createNewPanel01("数据库密码:"));
tempPanel.add(createNewPanel01("数据库URL:"));
tempPanel.add(createNewPanel01("数据库驱动:"));
JPanel Panel_case7_1 =new JPanel();
Panel_case7_1.setOpaque(false);
JButton button_case7_1 = new JButton("测试数据库连接");
button_case7_1.setBounds(340,20,60,25);
JButton button_case7_2 = new JButton("初始化数据库");
button_case7_2.setBounds(340,20,60,25);
button_case7_1.addActionListener(new MyActionListener01(tempPanel));
button_case7_2.addActionListener(new MyActionListener01(tempPanel));
Panel_case7_1.add(button_case7_1);Panel_case7_1.add(button_case7_2);
tempPanel.add(Panel_case7_1);
break;
case 7:
break;
case 8:
break;
}
return tempPanel;
}
public JPanel createNewPanel01(String title){
JPanel tempPanel =new JPanel();
tempPanel.setOpaque(false);
JLabel label1=new JLabel(title);label1.setPreferredSize(new Dimension(130, 28));
label1.setFont(new Font("宋体", Font.PLAIN, 14));
label1.setBorder(BorderFactory.createEmptyBorder (15, 10, 0, 0));
JTextField text1 = new JTextField();//
text1.setPreferredSize(new Dimension(250, 28));
tempPanel.add(label1);
tempPanel.add(text1);
return tempPanel;
}
public JPanel createNewPanel02(String title,JPanel panel){
JPanel tempPanel =new JPanel();
tempPanel.setOpaque(false);
JLabel label1=new JLabel(title);label1.setPreferredSize(new Dimension(130, 28));
label1.setFont(new Font("宋体", Font.PLAIN, 14));
label1.setBorder(BorderFactory.createEmptyBorder (15, 10, 0, 0));
// 需要选择的条目
String[] listData = new String[]{"Oracle9i/10g/11g/12c", "MySql5.0/5.5/5.6", "DB2 UDB8.2/9.5", "DB2 UDB9.7","SQLServer2000/2005/2008/2012","KinbaseESV7","DMV7","GBase8s","Oscar7.0"};
// 创建一个下拉列表框
final JComboBox<String> comboBox = new JComboBox<String>(listData);
// 添加条目选中状态改变的监听器
comboBox.addItemListener(new MyItemListener01(panel,comboBox));
comboBox.setPreferredSize(new Dimension(250, 28));
// 设置默认选中的条目
comboBox.setSelectedIndex(0);
// 添加到内容面板
tempPanel.add(label1);
tempPanel.add(comboBox);
return tempPanel;
}
class MyActionListener01 implements ActionListener{
public JPanel panel;
public MyActionListener01(JPanel panel){
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
JPanel j1 = (JPanel)panel.getComponent(0);JTextField t1 = (JTextField)j1.getComponent(1);String db_name = t1.getText();
JPanel j2 = (JPanel)panel.getComponent(1);JTextField t2 = (JTextField)j2.getComponent(1);String db_type = t2.getText();
JPanel j3 = (JPanel)panel.getComponent(2);JTextField t3 = (JTextField)j3.getComponent(1);String db_ip = t3.getText();
JPanel j4 = (JPanel)panel.getComponent(3);JTextField t4 = (JTextField)j4.getComponent(1);String db_port = t4.getText();
JPanel j5 = (JPanel)panel.getComponent(4);JTextField t5 = (JTextField)j5.getComponent(1);String db_sid = t5.getText();
JPanel j6 = (JPanel)panel.getComponent(5);JTextField t6 = (JTextField)j6.getComponent(1);String db_username = t6.getText();
JPanel j7 = (JPanel)panel.getComponent(6);JTextField t7 = (JTextField)j7.getComponent(1);String db_password = t7.getText();
JPanel j8 = (JPanel)panel.getComponent(7);JTextField t8 = (JTextField)j8.getComponent(1);String db_url = t8.getText();
JPanel j9 = (JPanel)panel.getComponent(8);JTextField t9 = (JTextField)j9.getComponent(1);String db_driver = t9.getText();
DatabaseManageUtil.initEOSDatabase(db_type, db_username,db_password, db_url, db_driver,new String[]{"coframe"});
System.out.println(db_name+"测试数据库连接....");
System.out.println(db_type+db_username+db_password+db_url+db_driver);
}
}
class MyItemListener01 implements ItemListener{
public JPanel panel;
public JComboBox<String> comboBox;
public JTextField t1 =null;
public JTextField t2 =null;
public JTextField t3 =null;
public JTextField t4 =null;
public JTextField t5 =null;
public JTextField t6 =null;
public JTextField t7 =null;
public JTextField t8 =null;
public JTextField t9 =null;
public MyItemListener01(JPanel panel,JComboBox<String> comboBox){
this.panel = panel;
this.comboBox = comboBox;
}
@Override
public void itemStateChanged(ItemEvent e) {
JPanel j1 = (JPanel)panel.getComponent(0);this.t1 = (JTextField)j1.getComponent(1);
JPanel j3 = (JPanel)panel.getComponent(2);this.t3 = (JTextField)j3.getComponent(1);
JPanel j4 = (JPanel)panel.getComponent(3);this.t4 = (JTextField)j4.getComponent(1);
JPanel j5 = (JPanel)panel.getComponent(4);this.t5 = (JTextField)j5.getComponent(1);
JPanel j6 = (JPanel)panel.getComponent(5);this.t6 = (JTextField)j6.getComponent(1);
JPanel j7 = (JPanel)panel.getComponent(6);this.t7 = (JTextField)j7.getComponent(1);
JPanel j8 = (JPanel)panel.getComponent(7);this.t8 = (JTextField)j8.getComponent(1);
JPanel j9 = (JPanel)panel.getComponent(8);this.t9 = (JTextField)j9.getComponent(1);
// 只处理选中的状态
if (e.getStateChange() == ItemEvent.SELECTED) {
//System.out.println("选中: " + this.comboBox.getSelectedIndex() + " = " + comboBox.getSelectedItem());
String dbType = (String)comboBox.getSelectedItem();
if(dbType.equalsIgnoreCase("Oracle9i/10g/11g/12c")){
t1.setText("default");
t3.setText("127.0.0.1");
t4.setText("1521");
t5.setText("orcl");
t6.setText("system");
t7.setText("000000");
t8.setText("jdbc:oracle:thin:@127.0.0.1:1521:orcl");
t9.setText("oracle.jdbc.driver.OracleDriver");
}else if(dbType.equalsIgnoreCase("MySql5.0/5.5/5.6")){
t1.setText("default");
t3.setText("127.0.0.1");
t4.setText("3306");
t5.setText("dbname");
t6.setText("root");
t7.setText("000000");
t8.setText("jdbc:mysql://127.0.0.1:3306/dbname");
t9.setText("org.gjt.mm.mysql.Driver");
}else if(dbType.equalsIgnoreCase("DB2 UDB8.2/9.5")){
}else if(dbType.equalsIgnoreCase("DB2 UDB9.7")){
}else if(dbType.equalsIgnoreCase("SQLServer2000/2005/2008/2012")){
}else if(dbType.equalsIgnoreCase("KinbaseESV7")){
}else if(dbType.equalsIgnoreCase("DMV7")){
}else if(dbType.equalsIgnoreCase("GBase8s")){
}else if(dbType.equalsIgnoreCase("Oscar7.0")){
}
}
}
}
}
2.2、文件压缩与解压缩工具(ZipUtil.java)
package CaililiangTools;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
/**
* 此工具是用来压缩与解压文件的工具
*/
public class ZipUtil {
private static byte[] _byte = new byte[1024];
public static List<File> getFileArray(String filePath){
File file = new File(filePath);
File[] array = file.listFiles();
return Arrays.asList(array);
}
public static void zipFile(String zip,String filePath){
zipFile(zip,getFileArray(filePath));
}
/**
* 压缩文件或路径
* @param zip 被压缩文件的文件路径或文件对象
* @param srcFiles 压缩后文件存放的目标路径
*/
public static void zipFile(String zip,List<File> srcFiles){
if(zip.endsWith(".ecd") || zip.endsWith(".jar") || zip.endsWith(".zip") || zip.endsWith(".ZIP")){
try {
ZipOutputStream _zipOut = new ZipOutputStream(new FileOutputStream(new File(zip)));
_zipOut.setEncoding("GBK");
for (File _f :srcFiles){
handlerFile(zip,_zipOut,_f,"");
}
_zipOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.err.println("目标文件不是ecd/jar/zip/ZIP,不能进行压缩");
}
}
private static void handlerFile(String zip,ZipOutputStream zipOut,File srcFile,String path) throws IOException {
if(!"".equals(path) && !path.endsWith(File.separator)){
path +=File.separator;
}
if(!srcFile.getPath().equals(zip)){
if(srcFile.isDirectory()){
File[] _files = srcFile.listFiles();
if(_files.length == 0){
zipOut.putNextEntry(new ZipEntry(path+srcFile.getName()+File.separator));
zipOut.closeEntry();
}else{
for(File _f:_files){
handlerFile(zip,zipOut,_f,path+srcFile.getName());
}
}
}else{
InputStream _in = new FileInputStream(srcFile);
zipOut.putNextEntry(new ZipEntry(path+srcFile.getName()));
int len = 0;
while((len = _in.read(_byte))>0){
zipOut.write(_byte,0,len);
}
_in.close();
zipOut.closeEntry();
}
}
}
public static List<File> upzipFile(String zipPath,String descDir){
return upzipFile(new File(zipPath),descDir);
}
/**
* 解压缩文件
* @param zipFile 被解压的原文件路径或文件对象
* @param descDir 解压后的目标文件夹路径
* @return
*/
public static List<File> upzipFile(File zipFile,String descDir){
List<File> _list = new ArrayList<File>();
try{
ZipFile _zipFile = new ZipFile(zipFile,"GBK");
for(Enumeration entries = _zipFile.getEntries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry) entries.nextElement();
File _file = new File(descDir+ File.separator+entry.getName());
if(entry.isDirectory()){
_file.mkdirs();
}else{
File _parent = _file.getParentFile();
if(!_parent.exists()){
_parent.mkdirs();
}
InputStream _in = _zipFile.getInputStream(entry);
OutputStream _out = new FileOutputStream(_file);
int len = 0;
while((len = _in.read(_byte))>0){
_out.write(_byte,0,len);
}
_in.close();
_out.flush();
_out.close();
_list.add(_file);
}
}
}catch (IOException e){
System.err.println("文件解压失败");
e.printStackTrace();
}
return _list;
}
/**
* 删除文件
* @param delpath 被删的文件路径
*/
public static void deletefile(String delpath){
try{
File file = new File(delpath);
if(!file.isDirectory()){
file.delete();
}else if(file.isDirectory()){
String[] filelist = file.list();
for(int i = 0;i<filelist.length;i++){
File delfile = new File(delpath+File.separator+filelist[i]);
if(!delfile.isDirectory()){
delfile.delete();
}else if(delfile.isDirectory()){
deletefile(delpath+File.separator+filelist[i]);
}
file.delete();
}
}
}catch (Exception e){
System.err.println("文件删除失败");
e.printStackTrace();
}
}
public static void main(String[] args){
List<File> list = getFileArray("C:\\tomcat_colony\\apache-tomcat-8.0.36-8081");
//zipFile("D:\\caililiang.zip",list);
upzipFile("D:\\caililiang.zip","C:\\tmp\\zookeeper");
}
}
2.3、java数据库初始化代码(此代码查看我的另外一篇文章“java数据库初始化(执行数据库文件)”)
三、总结
本文贴出了关键代码,代码为个人测试的代码(非正式项目),所以结构很乱,忘耐心阅读
网友评论