繁体   English   中英

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

[英]Java Jframe, Opens several Windows after running it, why?

这是我的问题,我正在研究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(); } } } 

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

ClientFenster gB = new ClientFenster();

请注意,您正在实例化Main应用程序中的Client:

ClientFenster cF = new ClientFenster();

然后,当实例化XmlFileReader时,您将重新实例化它。

XmlFileReader readXml = new XmlFileReader();

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM