Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中新框架中未出现的项_Java_Swing - Fatal编程技术网

Java中新框架中未出现的项

Java中新框架中未出现的项,java,swing,Java,Swing,我正在构建一个JFrame,它最终将显示一个程序的输出,该程序中有可变数量的节。我已经解析了输出,但是在框架中显示它是一个问题 当框架出现时,除滚动窗格外,框架完全为空。如何让这些标签显示出来 public class OutputPanel extends JFrame { public OutputPanel(Vector parsedOutput) { this.setTitle("Output"); this.setDefaultCloseOperation(

我正在构建一个JFrame,它最终将显示一个程序的输出,该程序中有可变数量的节。我已经解析了输出,但是在框架中显示它是一个问题

当框架出现时,除滚动窗格外,框架完全为空。如何让这些标签显示出来

public class OutputPanel extends JFrame {

    public OutputPanel(Vector parsedOutput) {

    this.setTitle("Output");
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    JScrollPane scrollPane = new JScrollPane();

    Iterator<Vector> outputIter = parsedOutput.iterator();

    while(outputIter.hasNext()) {
        Vector section = outputIter.next();

        JLabel sectionLabel = new JLabel((String)section.get(0));
        System.out.println((String)section.get(0));
        scrollPane.add(sectionLabel);

    }
    this.add(scrollPane);
    this.pack();
    this.setVisible(true);

    }
}

您不应该向滚动窗格添加组件

scrollPane.add(sectionLabel);
而是将它们添加到单独的面板中,并使用

scrollPane = new JScrollPane(thePanel);

例如:

产生:


您不应该向滚动窗格添加组件

scrollPane.add(sectionLabel);
而是将它们添加到单独的面板中,并使用

scrollPane = new JScrollPane(thePanel);

例如:

产生:


您应该将setViewPortView与容器一起使用,而不是为JScrollPane添加

试试这个

public class OutputPanel extends JFrame {

    public OutputPanel(Vector parsedOutput) {

    this.setTitle("Output");
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    JScrollPane scrollPane = new JScrollPane();

    Iterator<Vector> outputIter = parsedOutput.iterator();
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    scrollPane.setViewportView(panel);
    while(outputIter.hasNext()) {

        Vector section = outputIter.next();

        JLabel sectionLabel = new JLabel((String)section.get(0));
        System.out.println((String)section.get(0));
        panel.add(sectionLabel);

    }
    this.add(scrollPane);
    this.pack();
    this.setVisible(true);

    }
}

您应该将setViewPortView与容器一起使用,而不是为JScrollPane添加

试试这个

public class OutputPanel extends JFrame {

    public OutputPanel(Vector parsedOutput) {

    this.setTitle("Output");
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    JScrollPane scrollPane = new JScrollPane();

    Iterator<Vector> outputIter = parsedOutput.iterator();
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    scrollPane.setViewportView(panel);
    while(outputIter.hasNext()) {

        Vector section = outputIter.next();

        JLabel sectionLabel = new JLabel((String)section.get(0));
        System.out.println((String)section.get(0));
        panel.add(sectionLabel);

    }
    this.add(scrollPane);
    this.pack();
    this.setVisible(true);

    }
}

是否确实要为每行parsedOutput创建一个JLabel?这听起来很奇怪。为什么不简单地使用JList呢?您真的想为每行parsedOutput创建一个JLabel吗?这听起来很奇怪。为什么不简单地使用JList呢?