fontchooser.java
来自「WAP ide 代码」· Java 代码 · 共 443 行
JAVA
443 行
package Designer;
import javax.swing.*;
import java.awt.*;
/**
* Displays a font selection dialog box. Used by the menu designer to
* change the text styles and sizes of the various itmes in the menu.
* Copyright: Copyright (c) 2003
* @author Mark Busman
* @version 1.0
*
* For License and contact information see PhoneDesigner.java
*/
public class FontChooser extends javax.swing.JDialog {
private javax.swing.JPanel buttonPanel;
private javax.swing.JButton okButton;
private javax.swing.JButton cancelButton;
private javax.swing.JPanel MainPanel;
private javax.swing.JLabel fontLabel;
private javax.swing.JScrollPane fontScrollPane;
private javax.swing.JList fontList;
private javax.swing.JTextField selectedfontText;
private javax.swing.JLabel fontstyleLabel;
private javax.swing.JTextField selectedfontstyleText;
private javax.swing.JScrollPane fontstyleScrollPane;
private javax.swing.JList fontstyleList;
private javax.swing.JLabel fontsizeLabel;
private javax.swing.JTextField selectedfontsizeText;
private javax.swing.JScrollPane fontsizeScrollPane;
private javax.swing.JList fontsizeList;
private javax.swing.JLabel previewLabel;
private int returnStatus = RET_CANCEL;
/** A return status code - returned if Cancel button has been pressed */
public static final int RET_CANCEL = 0;
/** A return status code - returned if OK button has been pressed */
public static final int RET_OK = 1;
/** Creates new form FontChooser */
public FontChooser(java.awt.Frame parent,boolean modal) {
super (parent, modal);
jbInit();
pack();
this.setSize(400, 350);
FillLists();
}
/** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
public int getReturnStatus () {
return returnStatus;
}
/**
* Returns the font style currently selected
*/
public Font GetFont() {
int style = -1;
switch (fontstyleList.getSelectedIndex()) {
case 0:
style = Font.PLAIN;
break;
case 1:
style = Font.BOLD;
break;
case 2:
style = Font.ITALIC;
break;
case 3:
style = Font.BOLD & Font.ITALIC;
break;
}
Font f = new Font(fontList.getSelectedValue().toString(), style, fontsizeList.getSelectedIndex());
return f;
}
/**
* Sets the default font, text style, and size to select in the dialog.
* @param String name - the name of the font.
* @param String style - the style such as bold or plain.
* @param String size - the font size, normally b/w 0 and 72 but can
* be different for different fonts.
*
*/
public void SetDefaults(String name, String style, int size) {
fontList.setSelectedValue((Object)name, true);
fontstyleList.setSelectedValue((Object)style, true);
fontsizeList.setSelectedValue((Object)Integer.toString(size), true);
}
/**
* Sets the default font, style, and size to select in the dialog.
* @param Font font - a font object from which family name, style, and size
* can be extracted.
*/
public void SetDefaults(Font font) {
String name = font.getFontName();
String temp = name.substring(0, 1);
name = temp.toUpperCase() + name.substring(1).toLowerCase();
String style = "";
switch (font.getStyle()) {
case Font.PLAIN:
style = "Plain";
break;
case Font.BOLD:
style = "Bold";
break;
case Font.ITALIC:
style = "Italic";
break;
default:
style = "Bold Italic";
break;
}
fontList.setSelectedValue((Object)name, true);
fontstyleList.setSelectedValue((Object)style, true);
fontsizeList.setSelectedValue((Object)Integer.toString(font.getSize()), true);
}
/**
* Returns the currently selected style: bold, plain, underline, or italic.
*/
public String GetStyle(Font font) {
String style = "";
switch (font.getStyle()) {
case Font.PLAIN:
style = "Plain";
break;
case Font.BOLD:
style = "Bold";
break;
case Font.ITALIC:
style = "Italic";
break;
default:
style = "Bold Italic";
break;
}
return style;
}
/**
* Populates the Fontl list, styles list, and sizes list.
*/
private void FillLists() {
String [] fonts = getToolkit().getFontList(); //needed for older SDKs
fontList.setListData(fonts);
String [] fontstyles = {"Plain", "Bold", "Italic", "Bold Italic"};
fontstyleList.setListData(fontstyles);
String [] fontsizes = new String[72];
for (int i = 0; i < 72; i++)
fontsizes[i] = Integer.toString(i + 1);
fontsizeList.setListData(fontsizes);
}
/**
* Updates the preview pane to refelct the choosen selections.
*/
private void UpdatePreview() {
int style = -1;
switch (fontstyleList.getSelectedIndex()) {
case 0:
style = Font.PLAIN;
break;
case 1:
style = Font.BOLD;
break;
case 2:
style = Font.ITALIC;
break;
case 3:
style = Font.BOLD & Font.ITALIC;
break;
}
Font f = new Font(fontList.getSelectedValue().toString(), style, fontsizeList.getSelectedIndex());
previewLabel.setFont(f);
fontsizeList.setAutoscrolls(true);
}
/** This method is called from within the constructor to
* initialize the form.
*/
private void jbInit() {
buttonPanel = new javax.swing.JPanel ();
okButton = new javax.swing.JButton ();
cancelButton = new javax.swing.JButton ();
MainPanel = new javax.swing.JPanel ();
fontLabel = new javax.swing.JLabel ();
fontScrollPane = new javax.swing.JScrollPane ();
fontList = new javax.swing.JList ();
selectedfontText = new javax.swing.JTextField ();
fontstyleLabel = new javax.swing.JLabel ();
selectedfontstyleText = new javax.swing.JTextField ();
fontstyleScrollPane = new javax.swing.JScrollPane ();
fontstyleList = new javax.swing.JList ();
fontsizeLabel = new javax.swing.JLabel ();
selectedfontsizeText = new javax.swing.JTextField ();
fontsizeScrollPane = new javax.swing.JScrollPane ();
fontsizeList = new javax.swing.JList ();
previewLabel = new javax.swing.JLabel ();
setModal (true);
setTitle ("Choose Font");
addWindowListener (new java.awt.event.WindowAdapter () {
public void windowClosing (java.awt.event.WindowEvent evt) {
closeDialog (evt);
}
}
);
buttonPanel.setLayout (new java.awt.FlowLayout (2, 5, 5));
okButton.setText ("OK");
okButton.addActionListener (new java.awt.event.ActionListener () {
public void actionPerformed (java.awt.event.ActionEvent evt) {
okButtonActionPerformed (evt);
}
}
);
buttonPanel.add (okButton);
cancelButton.setText ("Cancel");
cancelButton.addActionListener (new java.awt.event.ActionListener () {
public void actionPerformed (java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed (evt);
}
}
);
buttonPanel.add (cancelButton);
getContentPane ().add (buttonPanel, java.awt.BorderLayout.SOUTH);
MainPanel.setLayout (new java.awt.GridBagLayout ());
java.awt.GridBagConstraints gridBagConstraints1;
MainPanel.setPreferredSize (new java.awt.Dimension(450, 280));
MainPanel.setMinimumSize (new java.awt.Dimension(450, 284));
fontLabel.setText ("Font:");
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 136;
gridBagConstraints1.ipady = 4;
gridBagConstraints1.insets = new java.awt.Insets (10, 10, 0, 0);
MainPanel.add (fontLabel, gridBagConstraints1);
fontScrollPane.setAutoscrolls (true);
fontList.setSelectionMode (javax.swing.ListSelectionModel.SINGLE_SELECTION);
fontList.addListSelectionListener (new javax.swing.event.ListSelectionListener () {
public void valueChanged (javax.swing.event.ListSelectionEvent evt) {
fontListValueChanged (evt);
}
}
);
fontScrollPane.setViewportView (fontList);
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 6;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 142;
gridBagConstraints1.ipady = 132;
gridBagConstraints1.insets = new java.awt.Insets (10, 10, 0, 0);
MainPanel.add (fontScrollPane, gridBagConstraints1);
selectedfontText.setEditable (false);
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 3;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 156;
gridBagConstraints1.insets = new java.awt.Insets (0, 10, 0, 0);
MainPanel.add (selectedfontText, gridBagConstraints1);
fontstyleLabel.setText ("Font Style:");
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 3;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 59;
gridBagConstraints1.ipady = 4;
gridBagConstraints1.insets = new java.awt.Insets (10, 10, 0, 0);
MainPanel.add (fontstyleLabel, gridBagConstraints1);
selectedfontstyleText.setEditable (false);
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 3;
gridBagConstraints1.gridy = 3;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 106;
gridBagConstraints1.insets = new java.awt.Insets (0, 10, 0, 0);
MainPanel.add (selectedfontstyleText, gridBagConstraints1);
fontstyleScrollPane.setAutoscrolls (true);
fontstyleList.setSelectionMode (javax.swing.ListSelectionModel.SINGLE_SELECTION);
fontstyleList.addListSelectionListener (new javax.swing.event.ListSelectionListener () {
public void valueChanged (javax.swing.event.ListSelectionEvent evt) {
fontstyleListValueChanged (evt);
}
}
);
fontstyleScrollPane.setViewportView (fontstyleList);
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 3;
gridBagConstraints1.gridy = 6;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 92;
gridBagConstraints1.ipady = 132;
gridBagConstraints1.insets = new java.awt.Insets (10, 10, 0, 0);
MainPanel.add (fontstyleScrollPane, gridBagConstraints1);
fontsizeLabel.setText ("Size:");
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 6;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 36;
gridBagConstraints1.ipady = 4;
gridBagConstraints1.insets = new java.awt.Insets (10, 10, 0, 0);
MainPanel.add (fontsizeLabel, gridBagConstraints1);
selectedfontsizeText.setEditable (false);
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 6;
gridBagConstraints1.gridy = 3;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 56;
gridBagConstraints1.insets = new java.awt.Insets (0, 10, 0, 0);
MainPanel.add (selectedfontsizeText, gridBagConstraints1);
fontsizeScrollPane.setBackground (java.awt.Color.white);
fontsizeScrollPane.setAutoscrolls (true);
fontsizeList.setSelectionMode (javax.swing.ListSelectionModel.SINGLE_SELECTION);
fontsizeList.addListSelectionListener (new javax.swing.event.ListSelectionListener () {
public void valueChanged (javax.swing.event.ListSelectionEvent evt) {
fontsizeListValueChanged (evt);
}
}
);
fontsizeScrollPane.setViewportView (fontsizeList);
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 6;
gridBagConstraints1.gridy = 6;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 42;
gridBagConstraints1.ipady = 132;
gridBagConstraints1.insets = new java.awt.Insets (10, 10, 0, 0);
MainPanel.add (fontsizeScrollPane, gridBagConstraints1);
previewLabel.setBorder (new javax.swing.border.TitledBorder("Preview"));
previewLabel.setText ("The Quick Brown Fox Jumped Over The Lazy Dogs");
previewLabel.setForeground (java.awt.Color.black);
previewLabel.setHorizontalAlignment (javax.swing.SwingConstants.CENTER);
gridBagConstraints1 = new java.awt.GridBagConstraints ();
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 9;
gridBagConstraints1.gridwidth = 7;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 90;
gridBagConstraints1.ipady = 17;
gridBagConstraints1.insets = new java.awt.Insets (10, 10, 0, 0);
MainPanel.add (previewLabel, gridBagConstraints1);
getContentPane ().add (MainPanel, java.awt.BorderLayout.CENTER);
}
/**
* Updates the preview pane and stores size selection.
*/
private void fontsizeListValueChanged (javax.swing.event.ListSelectionEvent evt) {
selectedfontsizeText.setText(fontsizeList.getSelectedValue().toString());
UpdatePreview();
}
/**
* Updates the preview pane and stores style selection.
*/
private void fontstyleListValueChanged (javax.swing.event.ListSelectionEvent evt) {
selectedfontstyleText.setText(fontstyleList.getSelectedValue().toString());
UpdatePreview();
}
/**
* Updates the preview pane and stores font family selection.
*/
private void fontListValueChanged (javax.swing.event.ListSelectionEvent evt) {
selectedfontText.setText(fontList.getSelectedValue().toString());
UpdatePreview();
}
/**
* Closes dialog with a RET_OK
*/
private void okButtonActionPerformed (java.awt.event.ActionEvent evt) {
doClose (RET_OK);
}
/**
* Closes dialog with a RET_CANCEL
*/
private void cancelButtonActionPerformed (java.awt.event.ActionEvent evt) {
doClose (RET_CANCEL);
}
/**
* Closes dialog with a RET_CANCEL
*/
private void closeDialog(java.awt.event.WindowEvent evt) {
doClose (RET_CANCEL);
}
/**
* Does actiual closing of dialog, sets status of close and hodes dialog.
*/
private void doClose (int retStatus) {
returnStatus = retStatus;
setVisible (false);
dispose ();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?