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

}
}
});
}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值