📄 jannetoolbar.java
字号:
/*
* Copyright (c) 1996 by Jan Andersson, Torpa Konsult AB.
*
* Permission to use, copy, and distribute this software for
* NON-COMMERCIAL purposes and without fee is hereby granted
* provided that this copyright notice appears in all copies.
*
*/
import java.awt.*;
import java.applet.Applet;
/**
* A class that implements a simple toolbar panel. Based on the
* JanneButton class.
*
* @version 1.5 96/01/24
* @author Jan Andersson, Torpa Konsult AB. (janne@torpa.se)
*/
class JanneToolbar extends Panel {
/**
* layout manager
*/
GridBagLayout gridBag;
GridBagConstraints gridConstraints;
/**
* flag to tell if horizontal layout is used
*/
boolean horizontal;
/**
* Insets. Specifies the external of each button.
*/
Insets insets;
/**
* shadow and border widths used by the buttons
*/
int shadow = 2;
int border = 2;
/**
* flag to tell if string labels are dispayed in the buttons
*/
boolean showLabel = true;
/**
* Creates a Toolbar panel with grid layout with the specified
* rows, columns, horizontal gap, and vertical gap.
* @param rows the rows; zero means 'any number.'
* @param cols the columns; zero means 'any number.' Only one of 'rows'
* and 'cols' can be zero, not both.
* @param hgap the horizontal gap variable
* @param vgap the vertical gap variable
* @param showLabel true if the button labels should be displayed.
*/
JanneToolbar(boolean horizontal, int gap, boolean showLabel) {
this.horizontal = horizontal;
this.showLabel = showLabel;
gridBag = new GridBagLayout();
setLayout(gridBag);
gridConstraints = new GridBagConstraints();
if (horizontal) {
insets = new Insets(2, 2, gap, 2);
//weightx = ??
}
else {
gridConstraints.gridwidth = GridBagConstraints.REMAINDER;
insets = new Insets(2, 2, 2, gap);
}
gridConstraints.insets = insets;
gridConstraints.fill = GridBagConstraints.BOTH;
}
/**
* Creates a horizontal toolbar panel.
* Default gap (4) are used. Buttons are displayed without string label.
*/
JanneToolbar() {
this(true, 4, false);
}
/**
* Add button.
* @param image the button image
* @param label the button label
*/
public void addButton(Image image, String label) {
// Add button to panel. Images are not resized.
JanneButton button = new JanneButton(
image, label, shadow, border, false, showLabel);
gridBag.setConstraints(button, gridConstraints);
add(button);
}
/**
* Add button.
* @param image the button image
* @param label the button label
* @extraGap extra (leading gap)
*/
public void addButton(Image image, String label, int extraGap) {
// create new insets for extra gap
Insets tmpInsets;
if (horizontal)
tmpInsets = new Insets(insets.top, insets.left + extraGap,
insets.bottom, insets.right);
else
tmpInsets = new Insets(insets.top + extraGap, insets.left,
insets.bottom, insets.right);
gridConstraints.insets = tmpInsets;
// Add button to panel.
addButton(image, label);
// reset insets
gridConstraints.insets = insets;
}
/**
* Does a (re-)layout on this panel.
*
* Called by JanneButton when size is updated. This is a hack to allow
* the size of the panel to change (when images loaded) and to make sure
* the toolbar is resized properly.
*/
public synchronized void layout() {
// x & y position of toolbar
int x = 0;
int y = 0;
// get toolbar preferred size
Dimension size = preferredSize();
// center toolbar in parent (if parent known)
Container parent = getParent();
if (parent != null) {
Dimension parentSize = parent.size();
if (size.width < parentSize.width)
x = parentSize.width/2 - size.width/2;
if (size.height < parentSize.height)
y = parentSize.height/2 - size.height/2;
}
// reshape toolbar to new size and re-layout
reshape(x, y, size.width, size.height);
super.layout();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -