📄 fontdialog.java
字号:
else {
String old = sampleTextLabel.getText();
sampleTextLabel.setText(old.substring(9, old.length()-4));
}
}
else if (actionCommand.equals("Color")) {
Color tempColor = JColorChooser.showDialog(null, "Font Color", sampleTextLabel.getForeground());
if (tempColor != null) {
sampleTextLabel.setForeground(tempColor);
this.repaint();
}
}
else if (actionCommand.equals("OK")) {
selectedFont = sampleTextLabel.getFont();
selectedColor = sampleTextLabel.getForeground();
this.setVisible(false);
}
else if (actionCommand.equals("Cancel")) {
selectedFont = null;
selectedColor = null;
this.setVisible(false);
}
}
/*****************************************************************************/
/**
* Returns the label being used for the font list.
*
* @return The label for the font list.
* @see #setFontLabel
*/
public final String getFontLabel() {
return fontChooserLabel.getText();
}
/*****************************************************************************/
/**
* Returns the label being used for the font sample area.
*
* @return The label for the font sample area.
* @see #setFontSampleLabel
*/
public final String getFontSampleLabel() {
return sampleLabel;
}
/*****************************************************************************/
/**
* Returns the text being used for the font sample.
*
* @return The text being used for the font sample.
* @see #setFontSampleText
*/
public final String getFontSampleText() {
return sampleTextLabel.getText();
}
/*****************************************************************************/
/**
* Returns the label being used for the font size list.
*
* @return The label for the font size list.
* @see #setFontSizeLabel
*/
public final String getFontSizeLabel() {
return sizeLabel.getText();
}
/*****************************************************************************/
/**
* Returns the label being used for the font style checkbox area.
*
* @return The label for the font style checkbox area.
* @see #setFontStyleLabel
*/
public final String getFontStyleLabel() {
return styleLabel;
}
/*****************************************************************************/
/**
* Gets the color last chosen to use for fonts.
*
* @return The color to use for the current font, or <code>null</code> if
* the dialog was cancelled.
*/
public Color getSelectedColor() {
return selectedColor;
}
/*****************************************************************************/
/**
* Gets the font last selected to use.
*
* @return The font to use for the current font, or <code>null</code>
* if the dialog was cancelled.
* @see #getUnderlineSelected
*/
public Font getSelectedFont() {
return selectedFont;
}
/*****************************************************************************/
/**
* Returns whether or not the user checked the "Underline" check box.
* Note that if this dialog was created such that "Underline" is not
* selectable, this method will always return <code>false</code>.
*
* @return Whether or not "underline" was selected.
* @see #setUnderlineSelected
*/
public boolean getUnderlineSelected() {
return underlineCheckBox==null ? false :
underlineCheckBox.isSelected();
}
/*****************************************************************************/
/**
* Sets the label being used for the font list.
*
* @param text The label for the font list.
* @see #getFontLabel
*/
public void setFontLabel(String text) {
fontChooserLabel.setText(text);
}
/*****************************************************************************/
/**
* Sets the label being used for the font sample area.
*
* @param text The label for the font sample area.
* @see #getFontSampleLabel
*/
public void setFontSampleLabel(String text) {
sampleLabel = text;
samplePanel.setBorder(BorderFactory.createCompoundBorder(
UIUtilities.getEmpty5Border(),
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(sampleLabel),
BorderFactory.createCompoundBorder(
UIUtilities.getEmpty5Border(),
BorderFactory.createBevelBorder(BevelBorder.LOWERED)))));
}
/*****************************************************************************/
/**
* Sets the text being used for the font sample.
*
* @param text The text to use for the font sample.
* @see #getFontSampleText
*/
public void setFontSampleText(String text) {
sampleTextLabel.setText(text);
}
/*****************************************************************************/
/**
* Sets the label to use for the font size list.
*
* @param text The label to usefor the font size list.
* @see #getFontSizeLabel
*/
public void setFontSizeLabel(String text) {
sizeLabel.setText(text);
}
/*****************************************************************************/
/**
* Sets the label to use for the font style checkbox area.
*
* @param text The label for the font style checkbox area.
* @see #getFontStyleLabel
*/
public void setFontStyleLabel(String text) {
styleLabel = text;
fontFormatPanel.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(styleLabel),
UIUtilities.getEmpty5Border()));
}
/*****************************************************************************/
/**
* Sets the currently selected font.
*
* @param toSelect The new currently-selected font.
* @param color The color to use as the font color.
*/
public void setSelectedFont(Font toSelect, Color color) {
// Null is not a valid font.
if (toSelect==null)
return;
// TODO: Find index of font to select with a case-insensitive
// string comparator.
sampleTextLabel.setForeground(color!=null ? color : Color.BLACK);
fontList.setSelectedValue(toSelect.getFamily(), true);
fontList.ensureIndexIsVisible(fontList.getSelectedIndex());
boldCheckBox.setSelected(toSelect.isBold());
italicCheckBox.setSelected(toSelect.isItalic());
//underlineCheckBox.setSelected(toSelect.isUnderline());
fontSizeList.setSelectedValue((new Integer(toSelect.getSize())).toString(), true);
fontSizeList.ensureIndexIsVisible(fontSizeList.getSelectedIndex());
}
/*****************************************************************************/
/**
* Sets or clears the "Underline" check box.
* Note that if this dialog was created such that "Underline" is not
* selectable, this method does nothing.
*
* @param underline Whether or not the Underline check box should be
* selected.
* @see #getUnderlineSelected
*/
public void setUnderlineSelected(boolean underline) {
if (underlineCheckBox.isEnabled())
underlineCheckBox.setSelected(underline);
}
/*****************************************************************************/
// Listen for when they change font or font size.
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
// If they've chosen a new font, update the sample.
if (lsm == fontList.getSelectionModel()) {
Font newFont = new Font((String)fontList.getSelectedValue(), properties, size);
sampleTextLabel.setFont(newFont);
}
// If they've chosen a new font size, also update the sample.
else if (lsm == fontSizeList.getSelectionModel()) {
size = Integer.parseInt((String)fontSizeList.getSelectedValue() );
Font newFont = sampleTextLabel.getFont().deriveFont(properties, size);
sampleTextLabel.setFont(newFont);
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -