保留完整的原有功能的基础上,确保滚动条显示为最新的界面private void initMsgPanel(){ msgPanel=new JPanel(); msgPanel.setLayout(new BoxLayout(msgPanel, BoxLayout.Y_AXIS)); msgPanel.setBackground(Color.WHITE); JScrollPane scrollPane = new JScrollPane(msgPanel); scrollPane.setBounds(5,5,370,330); add(scrollPane); } private void msgAddToMsgPanel(String msg,int align){ System.out.println("==========msgAddToMsgPanel==========="+msg); JLabel msgLbl=new JLabel(msg,SwingConstants.CENTER); msgLbl.setForeground(Color.decode("#61565A")); msgLbl.setBackground(Color.decode("#BACCD9")); msgLbl.setSize(50,25); msgLbl.setBorder(BorderFactory.createLineBorder(Color.getColor("#8FB2C9"),2,true)); msgLbl.setOpaque(true);//设置不透明 JPanel itemPanel=new JPanel(); itemPanel.setBackground(Color.WHITE); itemPanel.setPreferredSize(new Dimension(msgPanel.getWidth(),25)); itemPanel.add(msgLbl); FlowLayout layout = (FlowLayout) itemPanel.getLayout(); layout.setAlignment(align); msgPanel.add(itemPanel); msgPanel.updateUI(); }
时间: 2024-02-19 07:01:34 浏览: 5
给txtQueryResult加滚动条
您可以将滚动条的位置设置为显示最新内容。可以通过以下代码来实现:
scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
在 `msgAddToMsgPanel` 方法中,在 `msgPanel.updateUI()` 之后,添加上述代码,即可实现滚动条一直显示最新内容。
阅读全文