📄 buttonscrollpane.java
字号:
package yaokun.yuki.guangshi;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ButtonScrollPane extends javax.swing.JPanel {
//该控件有两种模式,一是单行多列,二是多行多列;其模式在对象创建时选定,创建后不能修改
static final int TABLE_MODE=0;
static final int ONELINE_MODE=1;
JButton buts[]=null;
int currentBut=0;
int scrollPaneHeight=50; //控件高度
int mode=ONELINE_MODE; //模式
int column=4; //列数
int row=1; //行数
int visibleNum=0;
JScrollPane scrollPane = new JScrollPane();
JPanel mainP = new JPanel();
GridLayout gridLayout = new GridLayout();
BorderLayout borderLayout1 = new BorderLayout();
JButton leftBut = new JButton();
JButton rightBut = new JButton();
//构造函数
public ButtonScrollPane(int mode) {
super();
this.mode=mode;
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public ButtonScrollPane(int mode,int columns,int rows) {
super();
this.mode=mode;
this.column=columns;
this.row=rows;
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public ButtonScrollPane(int mode,int columns) {
super();
this.mode=mode;
this.column=columns;
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public ButtonScrollPane(int mode,JButton[] but,int columns) {
super();
this.mode=mode;
this.column=columns;
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public ButtonScrollPane(int mode,JButton[] but,int columns,int rows) {
super();
this.mode=mode;
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//初始化
private void jbInit() throws Exception {
mainP.setLayout(gridLayout);
gridLayout.setColumns(column);
gridLayout.setRows(row);
this.setLayout(borderLayout1);
leftBut.setEnabled(false);
leftBut.setBorder(null);
leftBut.setMaximumSize(new Dimension(20, 20));
leftBut.setMinimumSize(new Dimension(20, 20));
leftBut.setPreferredSize(new Dimension(20, 20));
leftBut.setIcon(new ImageIcon("pic/left.png"));
leftBut.addActionListener(new ButtonScrollPane_leftBut_actionAdapter(this));
leftBut.setRequestFocusEnabled(false);
this.scrollPane.setMaximumSize(new Dimension(2000, scrollPaneHeight));
this.scrollPane.setMinimumSize(new Dimension(94, scrollPaneHeight));
this.scrollPane.setPreferredSize(new Dimension(90, scrollPaneHeight));
rightBut.setBorder(null);
rightBut.setMaximumSize(new Dimension(20, 20));
rightBut.setMinimumSize(new Dimension(20, 20));
rightBut.setPreferredSize(new Dimension(20, 20));
rightBut.setIcon(new ImageIcon("pic/right.png"));
rightBut.addActionListener(new ButtonScrollPane_rightBut_actionAdapter(this));
rightBut.setRequestFocusEnabled(false);
this.setBorder(BorderFactory.createEtchedBorder());
this.add(scrollPane, BorderLayout.CENTER);
scrollPane.getViewport().add(mainP, null);
this.scrollPane.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
//不同模式显示不同SCROLLBAR
if(mode==this.TABLE_MODE){
this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_ALWAYS);
}else{
this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_NEVER);
this.scrollPane.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_NEVER);
//添加左、右移动按钮
this.add(leftBut, BorderLayout.WEST);
this.add(rightBut, BorderLayout.EAST);
}
if(buts!=null){
//this.getVisibleNum();
/*buts[0].scrollRectToVisible(new Rectangle(0,0,
(int)buts[0].getPreferredSize().getWidth(),
(int)this.gridLayout.preferredLayoutSize(this).getHeight()));
*/
//使得第一个按钮得以显示
this.scrollToVisible(buts[0]);
//设置按钮可画边框
for(int i=0;i<buts.length;i++){
buts[i].setBorderPainted(true);
}
}
}
//向控件添加按钮
public void setButton(JButton[] buts){
this.buts=buts;
//this.getVisibleNum();
/*buts[0].scrollRectToVisible(new Rectangle(0,0,
(int)buts[0].getPreferredSize().getWidth(),
(int)this.gridLayout.preferredLayoutSize(this).getHeight()));
*/
this.scrollToVisible(buts[0]);
for(int i=0;i<buts.length;i++){
this.mainP.add(buts[i]);
buts[i].setBorderPainted(true);
}
this.repaint();
}
/*private int getVisibleNum(){
visibleNum=(int)(this.scrollPane.getSize().getWidth()/
this.buts[0].getSize().getWidth());
//System.out.println("show "+visibleNum+" buttons"+"size"+(int)this.scrollPane.getSize().getWidth()+":"+
// (int)this.buts[0].getSize().getWidth());
//this.currentBut=num-1;
return visibleNum;
}*/
//右移按钮被点击后,使控件中下一个按钮被显示
void rightBut_actionPerformed(ActionEvent e) {
//this.getVisibleNum();
if(currentBut+1>=buts.length){ //下一个按钮编号不小于按钮总数时,右按钮失效
rightBut.setEnabled(false); //设置按钮失效
return; //返回
}
currentBut=this.currentBut+1; //更新当前按钮编号
/*buts[this.currentBut].scrollRectToVisible(new Rectangle(0,0,
(int)buts[this.currentBut].getPreferredSize().getWidth(),
(int)this.gridLayout.preferredLayoutSize(this).getHeight()));
buts[this.currentBut].isVisible();*/
leftBut.setEnabled(true); //左按钮可用
//System.out.println((currentBut+1)+":"+(buts.length-visibleNum+2));
if(currentBut+1>=buts.length){ //检查
rightBut.setEnabled(false);
}
this.scrollToVisible(buts[this.currentBut]); //使当前按钮得以显示
}
void leftBut_actionPerformed(ActionEvent e) {
if(currentBut<=0){ //上一个按钮编号小于等于零时,左按钮失效
leftBut.setEnabled(false); //设置按钮失效
return;
}
currentBut=this.currentBut-1; //更新当前按钮编号
rightBut.setEnabled(true); //右按钮可用
/*buts[this.currentBut].scrollRectToVisible(new Rectangle(0,0,
(int)buts[this.currentBut].getPreferredSize().getWidth(),
(int)this.gridLayout.preferredLayoutSize(this).getHeight()));*/
if(currentBut<=0){
leftBut.setEnabled(false);
}
this.scrollToVisible(buts[this.currentBut]); //使得当前按钮可以显示
}
//使按钮显示于SCROLLPANE的可见区域
public void scrollToVisible(JButton but){
/*but.scrollRectToVisible(new Rectangle(0,0,
(int)buts[this.currentBut].getPreferredSize().getWidth(),
(int)this.gridLayout.preferredLayoutSize(this).getHeight()));*/
but.scrollRectToVisible(new Rectangle(0,0,
(int)this.gridLayout.preferredLayoutSize(this).getWidth(),
(int)this.gridLayout.preferredLayoutSize(this).getHeight()));
but.requestFocus(true);
//but.setSelected(true);
}
}
class ButtonScrollPane_rightBut_actionAdapter implements java.awt.event.ActionListener {
ButtonScrollPane adaptee;
ButtonScrollPane_rightBut_actionAdapter(ButtonScrollPane adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.rightBut_actionPerformed(e);
}
}
class ButtonScrollPane_leftBut_actionAdapter implements java.awt.event.ActionListener {
ButtonScrollPane adaptee;
ButtonScrollPane_leftBut_actionAdapter(ButtonScrollPane adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.leftBut_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -