?? fontdialog.java
字號:
// Class to define a dialog to choose a font
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.event.*;
class FontDialog extends JDialog
implements Constants,
ActionListener, // For buttons etc
ListSelectionListener // For list box
{
// Constructor
public FontDialog(SketchFrame window)
{
// Call the base constructor to create a modal dialog
super(window, "Font Selection", true);
this.window = window; // Save the window reference
font = window.getCurrentFont(); // Get the current font
fontStyle = font.getStyle(); // ...style
fontSize = font.getSize(); // ...and size
// Create the dialog button panel
JPanel buttonPane = new JPanel(); // Create the panel to hold buttons
// Create and add the buttons to the buttonPane
buttonPane.add(ok = createButton("OK")); // Add the OK button
buttonPane.add(cancel = createButton("Cancel")); // Add the Cancel button
getContentPane().add(buttonPane, BorderLayout.SOUTH); // Add pane to content pane
// Code to create the data input panel
JPanel dataPane = new JPanel(); // Create the data entry panel
dataPane.setBorder(BorderFactory.createCompoundBorder( // Create pane border
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagLayout gbLayout = new GridBagLayout(); // Create the layout
dataPane.setLayout(gbLayout); // Set the pane layout
GridBagConstraints constraints = new GridBagConstraints();
// Code to create the font choice and add it to the input panel
JLabel label = new JLabel("Choose a Font");
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gbLayout.setConstraints(label, constraints);
dataPane.add(label);
// Code to set up font list choice component
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = e.getAvailableFontFamilyNames(); // Get the font names
fontList = new JList(fontNames); // Create list of font names
fontList.setValueIsAdjusting(true); // single event selection
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // Choose 1 font
fontList.setSelectedValue(font.getFamily(),true);
fontList.addListSelectionListener(this);
JScrollPane chooseFont = new JScrollPane(fontList); // Scrollable list
chooseFont.setMinimumSize(new Dimension(300,100));
// Panel to display font sample
JPanel display = new JPanel();
fontDisplay = new JLabel("Sample Size: x X y Y z Z");
fontDisplay.setPreferredSize(new Dimension(300,100));
display.add(fontDisplay);
//Create a split pane with font choice at the top
// and font display at the bottom
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
true,
chooseFont,
display);
gbLayout.setConstraints(splitPane, constraints); // Split pane constraints
dataPane.add(splitPane); // Add to the data pane
// Set up the size choice using a combobox
JPanel sizePane = new JPanel(); // Pane for size choices
label = new JLabel("Choose point size"); // Prompt for point size
sizePane.add(label); // Add the prompt
String[] sizeList = { "8", "10", "12", "14", "16", // Array of sizes
"18", "20", "22", "24"};
chooseSize = new JComboBox(sizeList); // Size choice combobox
chooseSize.setSelectedItem(Integer.toString(fontSize)); // Default selection
chooseSize.addActionListener(this); // Add size listener
sizePane.add(chooseSize); // Add combobox to pane
gbLayout.setConstraints(sizePane, constraints); // Set pane constraints
dataPane.add(sizePane); // Add the pane
// Set up style options using radio buttons
JRadioButton bold = new JRadioButton("Bold", (fontStyle & Font.BOLD) > 0);
JRadioButton italic = new JRadioButton("Italic", (fontStyle & Font.ITALIC) > 0);
bold.addItemListener(new StyleListener(Font.BOLD)); // Add button listeners
italic.addItemListener(new StyleListener(Font.ITALIC));
JPanel stylePane = new JPanel(); // Create style pane
stylePane.add(bold); // Add buttons
stylePane.add(italic); // to style pane...
gbLayout.setConstraints(stylePane, constraints); // Set pane constraints
dataPane.add(stylePane); // Add the pane
getContentPane().add(dataPane, BorderLayout.CENTER);
pack();
setVisible(false);
}
JButton createButton(String label)
{
JButton button = new JButton(label); // Create the button
button.setPreferredSize(new Dimension(80,20)); // Set the size
button.addActionListener(this); // Listener is the dialog
return button; // Return the button
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource(); // Get the source of the event
if(source == ok) // Is it the OK button?
{
window.setCurrentFont(font); // Set the selected font
setVisible(false); // Hide the dialog
}
else if(source == cancel) // If it is the Cancel button
setVisible(false); // just hide the dialog
else if(source == chooseSize)
{
fontSize = Integer.parseInt((String)chooseSize.getSelectedItem());
font = font.deriveFont((float)fontSize);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}
// List selection listener method
public void valueChanged(ListSelectionEvent e)
{
if(!e.getValueIsAdjusting())
{
font = new Font((String)fontList.getSelectedValue(), fontStyle, fontSize);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}
class StyleListener implements ItemListener
{
public StyleListener(int style)
{
this.style = style;
}
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==ItemEvent.SELECTED) // If style was selected
fontStyle |= style; // turn it on in the font style
else
fontStyle &= ~style; // otherwise turn it off
font = font.deriveFont(fontStyle); // Get a new font
fontDisplay.setFont(font); // Change the label font
fontDisplay.repaint(); // repaint
}
private int style; // Style for this listener
}
private SketchFrame window; // The application window
private Font font; // Currently selected font
private int fontStyle; // Font style - Plain,Bold,Italic
private int fontSize; // Font point size
private JButton ok; // OK button
private JButton cancel; // Cancel button
//private String[] fontNames; // Font family names
private JList fontList; // Font list
private JComboBox chooseSize; // Font size options
private JLabel fontDisplay; // Font sample
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -