Swing 添加滚动条

搞了几天 ,终于搞好了。


代码:

dto:

package gov.mof.fasp2.app.books.dto;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


public class JButtonDTO extends HashMap implements Serializable, Cloneable {


/**

*/
private static final long serialVersionUID = 1L;

private String guid;
private String code;
private String name;
private String remark;

public JButtonDTO(Map map) {
    this.setName((String)map.get("name"));
    this.setCode((String) map.get("code"));
    this.setGuid((String) map.get("guid"));
    this.setRemark((String) map.get("remark"));
    }

/**
* @return the guid
*/
public String getGuid() {
return guid;
}
/**
* @param guid the guid to set
*/
public void setGuid(String guid) {
this.guid = guid;
}
/**
* @return the code
*/
public String getCode() {
return code;
}
/**
* @param code the code to set
*/
public void setCode(String code) {
this.code = code;
}
/**
* @return name 
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return remark
*/
public String getRemark() {
return remark;
}
/**
* @param remark the reamrk to set
*/
public void setRemark(String remark) {
this.remark = remark;
}

}


重写 JButton

package gov.mof.fasp2.app.books;

import gov.mof.fasp2.app.books.dto.JButtonDTO;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class BooksButton extends JButton{

/**

*/
private static final long serialVersionUID = 1L;

private JButtonDTO jbuttondto;

public JButtonDTO getJButtondto() {
return jbuttondto;
}


public BooksButton(JButtonDTO jbuttondto) {
setText(jbuttondto.getName());
//去掉选中标识。
this.setFocusPainted(false);
this.setLayout(null);
//去掉边框
this.setBorderPainted(false); 
        this.jbuttondto = jbuttondto;
    }
public BooksButton(String name){
//设置名称
this.setText(name);
//去掉选中标识。
this.setFocusPainted(false);
//去掉边框
this.setBorderPainted(false); 
}
public BooksButton(JButtonDTO jbuttondto,ImageIcon image){
setText(jbuttondto.getName());
this.setIcon(image);
//去掉选中标识。
this.setFocusPainted(false);
//去掉边框
this.setBorderPainted(false); 
this.jbuttondto = jbuttondto;
}
}

布局:添加JButton

//给jbutton赋值
List datas = getBooksData();
int x = 0;
int y = 0;
int count = 0;


//实例化一个JPanel;
jp = new JPanel();
jp.setLayout(null);
for (Object object : datas) {
Map map  = (Map<String, String>)object;
JButtonDTO jdto = new JButtonDTO(map);
count ++;
jbutton = new BooksButton(jdto,new ImageIcon(this.getClass().getResource("/images/book.png")));
jbutton.setLayout(null);
jbutton.setBackground(null);
jbutton.setHorizontalTextPosition(SwingConstants.CENTER);
jbutton.setVerticalTextPosition(SwingConstants.BOTTOM);
jbutton.setVisible(true);

int rowNum = (int)Math.ceil(count/6);//行
int colRum = count%6;//列
x = (Math.abs(colRum-1))*95;
y = (Math.abs(rowNum))*95;
if(colRum == 0){
x = 475;
y = (rowNum -1)*95;
}
jbutton.setBounds(x, y, 60, 85);
//绑定监听事件
jbutton.addMouseListener(new MouseAdapter() {
 public void mouseClicked(MouseEvent e) {
   if(e.getClickCount()==2){
    //双击事件
    JOptionPane.showMessageDialog(null,"double click!");
    closeCurrentWindow();
   }else{
    //单击选中
    postData = null;//存储选中条目的数据
BooksButton n = (BooksButton) e.getSource();//获得当前Bookslabel对象
if(!n.isSelected()){
//没有被选中,设置为选中,背景色改为亮灰色
n.setSelected(true);
n.setBackground(Color.LIGHT_GRAY);
//数据传递
postData = n.getJButtondto();
}else{
//选中,设置为未选择,背景色设置为null
n.setSelected(false);
n.setBackground(null);
postData = null;
preButton = null;
return;
}
//控制各个按钮之间不能同时选中。
if(preButton != null){
preButton.setSelected(false);
preButton.setBackground(null);
}
preButton = n;
   }
 }
});
//添加到JPanel
jp.add(jbutton,BorderLayout.WEST);
}
//实例化一个JScrollPane;
jscp = new JScrollPane(jp);
// jscp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
// jscp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jscp.setBounds(0, 30, 595, 365);

jp.setPreferredSize(new Dimension(600,9999));

jp2.setBounds(0, 0, 600, 30);
jp2.setBackground(Color.GRAY);
this.setLayout(null);
this.add(jscp, BorderLayout.WEST);
this.add(jp2, BorderLayout.WEST);
this.setIconImage(new ImageIcon(this.getClass().getResource(
"/images/logo.png")).getImage());
this.setTitle("账簿管理");
this.setBounds(400, 200, 600, 400);
this.setVisible(true);
this.setResizable(false);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值