【问题标题】:labels are not being created dynamically one below other标签不是在另一个之下动态创建的
【发布时间】:2017-10-30 12:54:13
【问题描述】:

http://ibb.co/hvrdbR private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//删除现有面板 字符串 xmldoc="./src/com/ui/Data.xml"; 尝试{ JLabel label2=new JLabel();
文件 fXmlFile = 新文件(xmldoc); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 文档 doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("username"); for (int temp = 0; temp jPanel7.setBackground(Color.WHITE); jPanel7.setLayout(new FlowLayout()); jPanel7.add(label2); label2.setText(文本); label2.setPreferredSize(新维度(100,40)); label2.setFont(new Font("Times New Roman", Font.BOLD, 24));
} } }
捕捉(异常前){ System.out.println("数据库异常:userExists()"); } }

只创建标签并粘贴所有用户名

【问题讨论】:

    标签: java xml


    【解决方案1】:

    在循环的每次迭代中,jPanel7.add(label2) 被调用一次。但是,您一遍又一遍地使用相同的 JLabel 实例。

    接下来会发生什么?

    JPanel.add(Component)java.awt.Container.add(Component) 中定义,它被委托给java.awt.Container.addImpl(Component, Object, int)。并在代码中:

     /* Reparent the component and tidy up the tree's state. */
     if (comp.parent != null) {
          comp.parent.remove(comp);
          if (index > component.size()) {
               throw new IllegalArgumentException("illegal component position");
          }
     }
     // ...
     comp.parent = this;
    

    尝试将Component 添加到已包含此ComponentContainer 将导致实际操作中不会发生任何事情。因此,最后你的jPanel7只有一个孩子——label2

    什么是正确的做法?

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String xmldoc="./src/com/ui/Data.xml";
        try {
            // background and layout should be set only once
            jPanel7.setBackground(Color.WHITE);
            jPanel7.setLayout(new FlowLayout());
    
            File fXmlFile = new File(xmldoc);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("username");
    
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    String text = eElement.getTextContent();
                    System.out.println(text);
    
                    JLabel label2 = new JLabel(text);  // create a new JLabel with the given text
                    // set label2's attributes
                    label2.setPreferredSize(new Dimension(100,40));
                    label2.setFont(new Font("Times New Roman", Font.BOLD, 24)); 
    
                    jPanel7.add(label2);  // add it to jPanel7
                } 
            }
        } catch (Exception ex) {
            System.out.println("Database exception : userExists()");
        }
    }
    

    【讨论】:

    • 它还没有工作实际上它正在将所有检索到的用户名从 xml 文件打印到动态创建的单个标签
    • @shristygupta 你能提供截图吗?我怀疑你所说的“单个标签”是什么意思,你想让标签被一行东西隔开吗?
    猜你喜欢
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多