Java Jframe,运行后会打开多个Windows,为什么?

时间:2018-10-16 12:22:25

标签: javascript java xml user-interface jframe

这是我的问题,我正在研究IT,对于我的工作,我需要对服务器进行编程,使人们可以上传7-zip文件,然后将其解压缩到服务器上

现在..问题是我需要用xml设置默认路径后,我的程序才能打开3个窗口...

希望有人可以帮助我,因为我只想要一个窗口

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;


public class ClientFenster {
    private File selectedFile;

    private JPanel Fenster1;
    private JTabbedPane tabbedPane1;
    private JButton selectPathButton;
    private JComboBox comboBox1;
    private JTextArea listSelectedFile;
    private JTable table1;
    private JScrollPane ScrollPane;
    private JPanel JPanelLogo;
    private JComboBox LocalServerBox;
    private JLabel JLabelLogo;
    private JPanel SelectedFilesTest;

    public ClientFenster() {

        BuildServerServerApplicationTests Build = new BuildServerServerApplicationTests();
        DefaultTableModel model = new DefaultTableModel();// Tabelle

        selectPathButton.addActionListener(new FileChooser());
        File file = new File("C:\\temp\\test.txt");
        file.setWritable(true);// punkt 29 in Requirements

        JLabelLogo.setIcon(new ImageIcon("logo3.png")); //Logo
        table1.setModel(model);
        table1.setAutoCreateRowSorter(true);
        table1.setFillsViewportHeight(true);;
        table1.setGridColor(Color.gray);
        table1.setShowGrid(false);

        model.addColumn("Date");
        model.addColumn("Size");
        model.addColumn("Status");
        model.addColumn("Name Zip");
        model.addColumn ("Time");
        model.addColumn("Project Name");
        model.addColumn("User");

        model.addRow(new Object[] {Calendar.getInstance().getTime().toString(),(file.length()/1024), "Offen" , Build.filename + ".zip", "Zeit" , "Proejkt Name", System.getProperty("user.name")});

        JFrame frame = new JFrame("ClientFenster");
        frame.setContentPane(Fenster1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    public void setComboBox(String xmlInput) {
        LocalServerBox.addItem(xmlInput);
    }

    public void setComboBox1(String aDefault) {
       comboBox1.addItem(aDefault);
    }


    // JFileChooser Button "select path"
    private class FileChooser extends Component implements ActionListener {


        @Override
        public void actionPerformed(ActionEvent actionEvent) {

            //Windows aussehen
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
            JFileChooser chooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            int returnValue = chooser.showDialog(FileChooser.this, "Upload");

                //Wenn auf Upload button gedrückt wird == rückgabewert 0
                if(returnValue == 0){

                    BuildServerServerApplicationTests Build = new BuildServerServerApplicationTests();

                    //"löscht" die extensions und lässt dynamische filenames zu
                    Build.filename = chooser.getSelectedFile().getName();
                    Build.extension = Build.filename.substring(Build.filename.lastIndexOf("."));
                    Build.filename = Build.filename.substring(0,Build.filename.lastIndexOf("."));
                    try {
                        Build.uploadFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                    //Combobox
                    //der Pfad des geöffneten File wird ausgegeben
                    if (returnValue == JFileChooser.APPROVE_OPTION) {
                        File selectedFile = chooser.getSelectedFile();
                        comboBox1.addItem(selectedFile);
                    }
                }
            }
        }

// Main

public class main {

    public static void main(String[] args) {
        ClientFenster cF = new ClientFenster();
        XmlFileReader readXml = new XmlFileReader();
        XmlFileReaderPath defaultPath = new XmlFileReaderPath();

    }

}

Xml FileReader

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;


public class XmlFileReader {
    ClientFenster gB = new ClientFenster();

    public XmlFileReader() {

        try {

            File fXmlFile = new File("ClientStartup/ClientStartConfig.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            //optional, but recommended
            //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
            doc.getDocumentElement().normalize();

            doc.getDocumentElement().getNodeName();

            NodeList nList = doc.getElementsByTagName("server");

            String xmlF;
            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                Element eElement = (Element) nNode;
                gB.setComboBox(eElement.getElementsByTagName("address").item(0).getTextContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

根据我的分析,您的问题应该在这里:

ClientFenster gB = new ClientFenster();

请注意,您将在主应用程序中实例化客户端:

ClientFenster cF = new ClientFenster();

然后,当实例化XmlFileReader时,您将再次实例化。

XmlFileReader readXml = new XmlFileReader();

我还假设第三个窗口是因为XmlFileReaderPath扩展了XmlFileReader。

相关问题