在我的新applet窗口而不是控制台屏幕

时间:2017-01-06 04:57:21

标签: java applet

我想在新的applet窗口中打开输出部分。目前,它在控制台屏幕中显示结果。如何实现这一目标?

我使用过Eclipse,我使用API​​比较两个数据库,但它运行良好,但我想在另一个applet窗口中显示我的输出。这两个方法me.run是从其他类调用的,只是模式和目录的比较,不再生成输出。

package com.vecna.dbDiff.Swing;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JComboBox;

import org.apache.commons.lang.StringUtils;

import com.vecna.dbDiff.tools.CompareDatabase;
import com.vecna.dbDiff.tools.MakeReferenceDatabase;

public class DatabaseForm extends Frame implements ActionListener {

    private static final String DEFAULT_DB_FILENAME = "resourceFiles/myDb.ser";
    private static final String DEFAULT_JDBC_DRIVER_CLASS = "com.mysql.jdbc.Driver";
    Label l1 = new Label("Database Type");
    Label l2 = new Label("Database Name1");
    Label l3 = new Label("Host");
    Label l4 = new Label("User Name1");
    Label l5 = new Label("Password1");
    Label l6 = new Label("Database Name2");
    Label l7 = new Label("User Name2");
    Label l8 = new Label("Password2");

    String[] dbType = { "MySQL", "MSSQL", "ORACLE", "DB2", "PostgreSQL", "AWS TeraData", "Hive" };
    @SuppressWarnings("unchecked")
    JComboBox dbTypeList = new JComboBox(dbType);
    TextField dbNameText1 = new TextField();
    TextField hostText = new TextField();
    TextField userNameText1 = new TextField();
    TextField passText1 = new TextField();

    TextField dbNameText2 = new TextField();
    TextField userNameText2 = new TextField();
    TextField passText2 = new TextField();
    Button okButton = new Button("OK");
    Button cancelButton = new Button("Cancel");

    public DatabaseForm() {
        addWindowListener(new mwa());
        setLayout(null);

        add(okButton);
        add(l1);
        add(dbTypeList);
        add(l2);
        add(dbNameText1);
        add(l4);
        add(userNameText1);
        add(l5);
        add(passText1);
        add(l6);
        add(dbNameText2);
        add(l7);
        add(userNameText2);
        add(l8);
        add(passText2);

        add(okButton);
        okButton.addActionListener(this);
        add(cancelButton);
        cancelButton.addActionListener(this);

        l1.setBounds(20, 45, 100, 20);
        dbTypeList.setBounds(180, 45, 200, 20);

        l2.setBounds(20, 95, 100, 20);
        dbNameText1.setBounds(180, 95, 200, 20);

        /*
         * l3.setBounds(20, 145, 100, 20); hostText.setBounds(180, 145, 200,
         * 20);
         */
        l4.setBounds(20, 145, 100, 20);
        userNameText1.setBounds(180, 145, 200, 20);

        l5.setBounds(20, 195, 100, 20);
        passText1.setBounds(180, 195, 200, 20);
        passText1.setEchoChar('*');

        l6.setBounds(20, 245, 100, 20);
        dbNameText2.setBounds(180, 245, 200, 20);
        l7.setBounds(20, 295, 100, 20);
        userNameText2.setBounds(180, 295, 200, 20);

        l8.setBounds(20, 345, 100, 20);
        passText2.setBounds(180, 345, 200, 20);
        passText2.setEchoChar('*');
        okButton.setBounds(150, 395, 100, 30);
        okButton.setBackground(Color.CYAN);
        okButton.addActionListener(this);
        cancelButton.setBounds(270, 395, 100, 30);
        cancelButton.setBackground(Color.CYAN);
        cancelButton.addActionListener(this);
        /*
        */
        // okButton.setForeground(Color.blue);

    }

    public static void main(String s[]) {
        DatabaseForm l = new DatabaseForm();
        l.setSize(new Dimension(600, 600));
        l.setTitle("Database");
        l.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        StringBuilder referenceDbFile = new StringBuilder(DEFAULT_DB_FILENAME);
        StringBuilder jdbcDriver = new StringBuilder(DEFAULT_JDBC_DRIVER_CLASS);

        String url1 = null, username1 = null, pw1 = null, url2 = null, username2 = null, pw2 = null;
        // parseArgs(args, url1, username1, pw1, referenceDbFile, jdbcDriver);

        if (e.getSource() == okButton) {
            // System.out.println("Welcome");
            url1 = dbNameText1.getText();
            username1 = userNameText1.getText();
            pw1 = passText1.getText();

            url2 = dbNameText2.getText();
            username2 = userNameText2.getText();
            pw2 = passText2.getText();
            if (StringUtils.isBlank(url1.toString())) {
                System.out.println(
                        "[ERROR] JDBC url required, eg \"jdbc:postgresql://localhost/sqm\".  Use --url or -l param");
            } else if (StringUtils.isBlank(username1.toString())) {
                System.out.println("[ERROR] JDBC username required, eg \"admin\".  Use --username or -u param");
            } else {
                MakeReferenceDatabase me = new MakeReferenceDatabase();
                try {
                    me.run(url1.toString(), username1.toString(), pw1.toString(), referenceDbFile.toString(),
                            jdbcDriver.toString());
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            if (StringUtils.isBlank(url2.toString())) {
                System.out.println(
                        "[ERROR] JDBC url required, eg \"jdbc:postgresql://localhost/sqm\".  Use --url or -l param");
            } else if (StringUtils.isBlank(username2.toString())) {
                System.out.println("[ERROR] JDBC username required, eg \"admin\".  Use --username or -u param");
            } else {
                CompareDatabase me = new CompareDatabase();
                try {
                    me.run(url2.toString(), username2.toString(), pw2.toString(), referenceDbFile.toString(),
                            jdbcDriver.toString());
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }
}

class mwa extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

0 个答案:

没有答案