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();
}
*/

}
}
});
}


}


阅读更多
文章标签: java swing滚动条
想对作者说点什么? 我来说一句

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

关闭
关闭
关闭