scrollingbuttonpanel.java
字号:
package piy;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
/**
* A panel which displays buttons horizontally along it, and has arrows at the end to scroll
* if the number of buttons to display is too much for the panel. Only one of the buttons
* may be selected at any particular time.
* @author David Vivash
* @version 1.0, 26/11/00
*/
public class ScrollingButtonPanel extends JPanel implements SingleSelectionModel, ActionListener
{
private int buttonWidth, buttonHeight, spacing;
private JPanel viewPort = new JPanel(new FlowLayout()), left, right;
private JScrollPane scroller;
private ArrayList buttons;
/**
* Construct a basic scrolling button panel with specified button sizing and spacing.
* @param size the initial capacity for buttons
* @param buttonWidth the standard width of the added buttons
* @param buttonHeight the standard height of the added buttons
* @param spacing the amount of space to allow between buttons
*/
public ScrollingButtonPanel(int size, int buttonWidth, int buttonHeight, int spacing) {
setBorder(null);
scroller = new JScrollPane(viewPort, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setBorder(null);
left = new JPanel(null);
left.setBorder(null);
left.setMinimumSize(new Dimension(16,32));
left.setPreferredSize(new Dimension(16,32));
right = new JPanel(null);
right.setBorder(null);
right.setMinimumSize(new Dimension(16,32));
right.setPreferredSize(new Dimension(16,32));
String sep = java.io.File.separator;
FloatingButton leftButton = new FloatingButton(new ImageIcon("piy" + sep + "images" + sep + "left.gif").getImage(), "left", false);
FloatingButton rightButton = new FloatingButton(new ImageIcon("piy" + sep + "images" + sep + "right.gif").getImage(), "right", false);
leftButton.addActionListener(this);
rightButton.addActionListener(this);
left.add(leftButton);
leftButton.setBounds(0,0,16,32);
right.add(rightButton);
rightButton.setBounds(0,0,16,32);
setLayout(new BorderLayout());
add(left, BorderLayout.WEST);
add(right, BorderLayout.EAST);
add(scroller, BorderLayout.CENTER);
buttons = new ArrayList(size > 1 ? size : 2);
this.buttonWidth = buttonWidth > 3 ? buttonWidth : 4;
this.buttonHeight = buttonHeight > 3 ? buttonHeight : 4;
this.spacing = spacing >=0 ? spacing : 0;
scroller.getViewport().setViewPosition(new Point(0,0));
}
/**
* Add a component (a button) to the panel. The component is added to the end of the
* buttons on the panel.
* @param button the button to add
* @return the number assigned to the button
*/
public int addButton(AbstractButton button) {
buttons.add(button);
viewPort.add(button);
button.setMinimumSize(new Dimension(buttonWidth, buttonHeight));
button.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
button.addActionListener(this);
button.setActionCommand("" + (buttons.size() - 1)); //ie. actionCommand = "0", "2" etc.
return buttons.size()-1;
}
/**
* Returns the reference to the currently selected button.
* @return the currently selected button, or null if no button is currently selected
*/
public AbstractButton getSelectedButton() {
return current == -1 ? null : (AbstractButton)buttons.get(current);
}
//---- SingleSelectionModel Methods ----
private java.util.Vector changeListeners = new java.util.Vector(1);
int current = -1; //currently selected button
/**
* Adds listener as a listener to changes in the model.
* @param listener a ChangeListener object that wishes to be notified of changes in
* this object's selection state
*/
public void addChangeListener(ChangeListener listener) {
changeListeners.add(listener);
}
/**
* Clears the selection (to -1), and notifies all registered listeners to the change.
*/
public void clearSelection() {
current = -1;
for (int i=0; i<buttons.size(); i++)
((AbstractButton)buttons.get(i)).setSelected(false);
notifyListeners();
}
/**
* Retrieve the index of the currently selected button.
* @return the selected button's index, or -1 if there is no currently selected button
*/
public int getSelectedIndex() {
return current;
}
/**
* Determines whether there is a currently selected button.
* @return true if the selection model currently has a selected value.
*/
public boolean isSelected() {
return current != -1;
}
//Removes listener as a listener to changes in the model. Not currently implemented.
public void removeChangeListener(ChangeListener listener) {
}
/**
* Sets the model's selected index to index. Fires an event - ie. the change listeners
* are notified.
* @param index a value between 0 and numberOfButtons-1
*/
public void setSelectedIndex(int index) {
if ( (index > -1) && (index < buttons.size()) ) {
//update buttons to reflect change
for (int i=0; i<buttons.size(); i++) {
if (i==index) ((AbstractButton)buttons.get(i)).setSelected(true);
else ((AbstractButton)buttons.get(i)).setSelected(false);
}
//tell all change listeners of the change
notifyListeners();
}
}
//------- ActionListener Methods ----------
public void actionPerformed(ActionEvent e) {
JViewport port = scroller.getViewport();
int x = port.getViewPosition().x;
String action = e.getActionCommand();
if (action.equals("left")) {
x -= 10;
if (x < 0) x=0;
} else if (action.equals("right")) {
x += 10;
if (port.getWidth() - x > scroller.getWidth())
x = port.getWidth() - scroller.getWidth();
} else {
current = Integer.valueOf(action).intValue();
for (int i=0; i<buttons.size(); i++) {
if (i==current) continue;
((AbstractButton)buttons.get(i)).setSelected(false);
}
if ( !((AbstractButton)buttons.get(current)).isSelected() ) current = -1;
notifyListeners();
}
port.setViewPosition(new Point(x, 0));
}
/**
* Notifies all registered changeListeners of a change in selection value
*/
private void notifyListeners() {
for (int i=0; i<changeListeners.size(); i++)
((ChangeListener)changeListeners.get(i)).stateChanged(new ChangeEvent(this));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -