📄 borderchooser.java
字号:
TitledPanel.add (TitledColorButton);
ProperiesPanel.add (TitledPanel, "card8");
PropertiesScrollPane.setViewportView (ProperiesPanel);
MainPanel.add (PropertiesScrollPane, java.awt.BorderLayout.SOUTH);
getContentPane ().add (MainPanel, java.awt.BorderLayout.CENTER);
}
/**
* Creates a new matte border.
*/
private void MatteTypeButtonActionPerformed (java.awt.event.ActionEvent evt) {
String ifName = "";
// Depending on the type it displays either a color chooser or a file chooser dialog
if (MatteSwingLabel.getText().equals("Color")) { // Display the color chooser
JButton button = (JButton) evt.getSource();
JColorChooser cc = new JColorChooser();
Color c = cc.showDialog(this, "Color Chooser", button.getBackground());
button.setBackground(c);
}
else { // Display the file chooser
try {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(this);
ifName = fc.getSelectedFile().getPath();
MatteTypeButton.setIcon(new ImageIcon(ifName));
}
catch (NullPointerException e) {
}
}
UpdatePreview();
}
/**
* Sets border type.
*/
private void MatteTypeComboActionPerformed (java.awt.event.ActionEvent evt) {
MatteSwingLabel.setText(MatteTypeCombo.getSelectedItem().toString());
MatteTypeButton.setIcon(null);
if (MatteSwingLabel.getText().equals("Color"))
MatteTypeButton.setBackground(Color.lightGray);
else
MatteTypeButton.setBackground(new Color(204, 204, 204));
}
/**
* Chnages the font to the selected font.
*/
private void FontInputListener (java.awt.event.ActionEvent evt) {
JButton b = (JButton) evt.getSource();
FontChooser fc = new FontChooser(new JFrame(), true);
fc.show();
if (fc.getReturnStatus() == 1) { // true
b.setFont(fc.GetFont());
b.setText(fc.GetFont().getFontName() + " " + fc.GetStyle(fc.GetFont()) + " " + fc.GetFont().getSize());
b.setToolTipText(b.getText());
UpdatePreview();
}
}
/**
* Updates the border in the preview with the cirrently selected border.
*/
private void BorderInputListener (java.awt.event.ActionEvent evt) {
BorderChooser bc = new BorderChooser(new JFrame(), true);
bc.show();
int value = bc.GetReturnValue();
if (value == 1) {
Border border = bc.GetBorder();
JButton b = (JButton) evt.getSource();
b.setBorder(border);
b.setText("");
UpdatePreview();
}
}
/**
* Updates the selected color swab with the new color.
*/
private void ColorInputListener (java.awt.event.ActionEvent evt) {
JButton button = (JButton) evt.getSource();
JColorChooser cc = new JColorChooser();
Color c = cc.showDialog(this, "Color Chooser", button.getBackground());
button.setBackground(c);
UpdatePreview();
}
/**
* Updates the preview pane when a change occurs.
*/
private void ComboInputListener (java.awt.event.ActionEvent evt) {
UpdatePreview();
}
/**
* Updates the preview pane when a change occurs.
*/
private void TextInputListener (java.awt.event.ActionEvent evt) {
UpdatePreview();
}
/**
* Closes the dialog and does not save the changes.
*/
private void CancelButtonActionPerformed (java.awt.event.ActionEvent evt) {
ReturnValue = 0;
setVisible(false);
dispose();
}
/**
* Updates the preview pane and closes the dialog.
*/
private void OkButtonActionPerformed (java.awt.event.ActionEvent evt) {
UpdatePreview();
ReturnValue = 1;
setVisible(false);
dispose();
}
/**
* Listener for when border types are changed thru the listbox.
*/
private void BorderListValueChanged (javax.swing.event.ListSelectionEvent evt) {
HideAllPanels();
if (BorderList.getSelectedIndex() == 0)
NoBorderPanel.setVisible(true);
if (BorderList.getSelectedIndex() == 1)
EmptyPanel.setVisible(true);
if (BorderList.getSelectedIndex() == 2)
EtchedPanel.setVisible(true);
if (BorderList.getSelectedIndex() == 3)
BevelPanel.setVisible(true);
if (BorderList.getSelectedIndex() == 4)
BevelPanel.setVisible(true);
if (BorderList.getSelectedIndex() == 5)
MattePanel.setVisible(true);
//Statusbar.setText("Choose between color or icon, not both...");
if (BorderList.getSelectedIndex() == 6)
LinePanel.setVisible(true);
if (BorderList.getSelectedIndex() == 7)
CompoundPanel.setVisible(true);
if (BorderList.getSelectedIndex() == 8)
TitledPanel.setVisible(true);
UpdatePreview();
}
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {
setVisible (false);
dispose ();
}
/**
* Hides the various tab panels
*/
private void HideAllPanels() {
NoBorderPanel.setVisible(false);
EmptyPanel.setVisible(false);
EtchedPanel.setVisible(false);
BevelPanel.setVisible(false);
MattePanel.setVisible(false);
LinePanel.setVisible(false);
CompoundPanel.setVisible(false);
TitledPanel.setVisible(false);
//Statusbar.setText("");
}
/**
* Updates the preview pane with the current boder style.
*/
private void UpdatePreview() {
int Type = 0;
try {
switch(BorderList.getSelectedIndex()) {
case 0: //No Border
border = null;
break;
case 1: // Empty Border
border = new EmptyBorder(Integer.parseInt(EmptyTopText.getText()),Integer.parseInt(EmptyLeftText.getText()),Integer.parseInt(EmptyBottomText.getText()),Integer.parseInt(EmptyRightText.getText()));
break;
case 2: // Etched Border
// Get the etch type
if (EtchedTypeCombo.getSelectedItem().equals("Lowered"))
Type = EtchedBorder.LOWERED;
else
Type = EtchedBorder.RAISED;
border = new EtchedBorder(Type, EtchedHighlightButton.getBackground(), EtchedShadowButton.getBackground());
break;
case 3: // Bevel Border
int bevelType = 0;
if (BevelTypeCombo.getSelectedItem().equals("Lowered"))
Type = BevelBorder.LOWERED;
else
Type = BevelBorder.RAISED;
border = new BevelBorder(Type, BevelHighlightOuterButton.getBackground(), BevelHighlightInnerButton.getBackground(), BevelShadowOuterButton.getBackground(), BevelShadowInnerButton.getBackground());
break;
case 4: // Soft Bevel Border
if (BevelTypeCombo.getSelectedItem().equals("Lowered"))
Type = BevelBorder.LOWERED;
else
Type = BevelBorder.RAISED;
border = new SoftBevelBorder(Type, BevelHighlightOuterButton.getBackground(), BevelHighlightInnerButton.getBackground(), BevelShadowOuterButton.getBackground(), BevelShadowInnerButton.getBackground());
break;
case 5: //Matte Border
if (MatteSwingLabel.getText().equals("Color"))
border = new MatteBorder(Integer.parseInt(MatteTopText.getText()), Integer.parseInt(MatteLeftText.getText()), Integer.parseInt(MatteBottomText.getText()), Integer.parseInt(MatteRightText.getText()), MatteTypeButton.getBackground());
else
border = new MatteBorder(Integer.parseInt(MatteTopText.getText()), Integer.parseInt(MatteLeftText.getText()), Integer.parseInt(MatteBottomText.getText()), Integer.parseInt(MatteRightText.getText()), MatteTypeButton.getIcon());
break;
case 6: // Line Border
boolean Rounded = false;
if (LineRoundCombo.getSelectedItem().equals("False"))
Rounded = false;
else
Rounded = true;
border = new LineBorder(LineColorButton.getBackground(), Integer.parseInt(LineThicknessText.getText()), Rounded);
break;
case 7: // Compound Border
border = new CompoundBorder(CompoundOutsideButton.getBorder(), CompoundInsideButton.getBorder());
break;
case 8: // Titled Border
int justify = 0;
int pos = 0;
if (TitledJustifyCombo.getSelectedIndex() == 0)
justify = TitledBorder.DEFAULT_JUSTIFICATION;
if (TitledJustifyCombo.getSelectedIndex() == 1)
justify = TitledBorder.LEFT;
if (TitledJustifyCombo.getSelectedIndex() == 2)
justify = TitledBorder.RIGHT;
if (TitledJustifyCombo.getSelectedIndex() == 3)
justify = TitledBorder.CENTER;
if (TitledJustifyCombo.getSelectedIndex() == 4)
justify = TitledBorder.LEADING;
if (TitledJustifyCombo.getSelectedIndex() == 5)
justify = TitledBorder.TRAILING;
if (TitledPosCombo.getSelectedIndex() == 0)
pos = TitledBorder.DEFAULT_POSITION;
if (TitledPosCombo.getSelectedIndex() == 1)
pos = TitledBorder.ABOVE_BOTTOM;
if (TitledPosCombo.getSelectedIndex() == 2)
pos = TitledBorder.ABOVE_TOP;
if (TitledPosCombo.getSelectedIndex() == 3)
pos = TitledBorder.BELOW_BOTTOM;
if (TitledPosCombo.getSelectedIndex() == 4)
pos = TitledBorder.BELOW_TOP;
if (TitledPosCombo.getSelectedIndex() == 5)
pos = TitledBorder.BOTTOM;
if (TitledPosCombo.getSelectedIndex() == 6)
pos = TitledBorder.TOP;
border = new TitledBorder(TitledBorderButton.getBorder(), TitledTitleText.getText(), justify, pos, TitledFontButton.getFont(), TitledColorButton.getBackground());
break;
default: // Default = No Border
border = null;
break;
}
}
catch (NumberFormatException e) {
}
PreviewLabel.setBorder(border);
}
/**
* Returns 0 if cancel button pressed.
* @return int value - 1 if OK button pressed, 0 if cancel button pressed.
*/
public int GetReturnValue() {
return ReturnValue;
}
/**
* Returns the border being previewed in the dialog.
* @return Border border - the currently previewed border.
*/
public Border GetBorder() {
return PreviewLabel.getBorder();
}
/**
* @param args the command line arguments
*/
public static void main (String args[]) {
new BorderChooser (new javax.swing.JFrame (), true).show ();
System.exit(0);
}
/**
* Inner class - renders the icons in the list box.
*/
private class BorderListCellRenderer extends JLabel implements ListCellRenderer {
Icon[] icons;
public BorderListCellRenderer (Icon[] icons) {
this.icons = icons;
setOpaque(true);
}
/**
* Method that must be implemented from the interface
* ListCellRenderer.
*/
public Component getListCellRendererComponent (
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
// Set the text for the label
if (value != null) {
String text = value.toString();
setText(text);
}
// Set the icon for each selection cell
// indicated by the index.
setIcon(icons[index]);
// What happens when a selection is made.
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
// NOTE: This step is very important; otherwise, the
// selection colors don't render as expected!
else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
// Finally, return this rubber stamp component.
return this;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -