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-01-29 12:02:07 浏览: 24
可以使用JScrollPane来包裹msgPanel,这样就可以自动添加滚动条了。具体实现可以参考下面的代码:
```
JScrollPane scrollPane = new JScrollPane(msgPanel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollPane, BorderLayout.CENTER);
```
这样就可以在msgPanel的高度超过容器高度时显示滚动条了。
相关问题
保留完整的原有功能的基础上,确保滚动条显示为最新的界面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(); }
为了确保滚动条显示为最新的界面,可以在添加消息后,将滚动条设置到最底部。修改后的代码如下:
```
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();
// 将滚动条设置到最底部
JScrollBar vertical = ((JScrollPane)msgPanel.getParent()).getVerticalScrollBar();
vertical.setValue(vertical.getMaximum());
}
```
在msgAddToMsgPanel()方法的最后,添加了获取滚动条并将其设置到最底部的代码。这样就能保证每次添加消息后,滚动条都会自动滑动到最底部。
保留完整的原有功能的基础上,为msgPanel添加滑动条: private void initMsgPanel(){ msgPanel=new JPanel(); msgPanel.setBounds(5,5,370,330); msgPanel.setBackground(Color.WHITE); add(msgPanel);}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.add(itemPanel); //更新界面 msgPanel.updateUI(); }
你可以使用JScrollPane来为msgPanel添加滑动条。具体实现方法如下:
1. 将msgPanel的布局方式设置为Boxlayout,以便支持纵向滚动条。
2. 创建一个JScrollPane对象,并将msgPanel添加到JScrollPane中。
3. 将JScrollPane添加到当前界面中。
修改后的代码如下:
```
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();
}
```