我有以下课程...
public class MessageFrame extends JFrame {
public MessageFrame(List<HistoryMessage> messages){
setLayout(null);
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
getContentPane().add(scrPane);
int i = 1;
for(HistoryMessage m : messages){
//TODO: needs to be StringBuilder
StringBuilder sb = new StringBuilder();
sb.append("<html> <strong>");
sb.append(m.getSender());
sb.append(" ");
Date d = new Date(m.getDate());
sb.append(d);
sb.append(":</strong>");
sb.append(m.getPayload());
sb.append("</html>");
JLabel l = new JLabel(sb.toString());
l.setBounds(30, i, 400, 50);
i += 125;
container.add(l);
}
setTitle("Messages");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
但是,如果我不使用滚动窗格,这不会显示任何内容...
public MessageFrame(List<HistoryMessage> messages){
setLayout(null);
int i = 1;
for(HistoryMessage m : messages){
//TODO: needs to be StringBuilder
StringBuilder sb = new StringBuilder();
sb.append("<html> <strong>");
sb.append(m.getSender());
sb.append(" ");
Date d = new Date(m.getDate());
sb.append(d);
sb.append(":</strong>");
sb.append(m.getPayload());
sb.append("</html>");
JLabel l = new JLabel(sb.toString());
l.setBounds(30, i, 400, 50);
i += 125;
add(l);
}
setTitle("Messages");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
但是没有滚动,所以只有前几个出现,有人可以帮我解决我所缺少的吗?