javaswing 如何让滚动条根据内容变化而变化

/**
* 创建消息窗口
*/
public void createMessageWindow() {
setSize(400, 400);
setLocation(1510,500);

/*jpanel.setSize(400,400);*/
jpanel.setPreferredSize(new Dimension(400,1000));
// 设置滚动条
        /*jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);*/
     jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );// 设置垂直滚动条
     jpanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
jsp.setBorder(BorderFactory.createLineBorder(Color.red));
add(jsp);
jsp.setViewportView(jpanel);
jpanel.setLayout(new FlowLayout());
setTitle("收到"+times+"条消息");


}


public void openFile(List<String> list) {
setVisible(true);
for (String srcPath : list) {
String fileName=getPathName(srcPath);


File file=new File(srcPath);
 
srcpath=file.getParent();//要打开的路径

System.out.println("要打开的路径getParent+----------------------------"+srcpath);

JLabel jlabel = new JLabel("双击打开" + fileName + "文件", JLabel.CENTER);
jlabel.setBorder(BorderFactory.createLineBorder(Color.red));
jlabel.setPreferredSize(new Dimension(400,30));//设置JLabel大小
jpanel.add(jlabel);
setVisible(true);

// 文本监听
jlabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {

if (e.getClickCount() == 2) {
System.out.println("打开了文件");

/*try {
Runtime.getRuntime().exec("cmd /c start explorer "+srcpath);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/

}
}
});
}


}


没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试