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