字体设置的功能
package jtest;import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.event.*;public class FontDialog
extends JDialog
implements Constants, ActionListener,ListSelectionListener { private Frame1 window;
private Font font;
private int fontstyle;
private int fontsize; private JButton ok;
private JButton cancel; private JList fontList;
private JComboBox chooseSize; JLabel fontDisplay; public FontDialog(Frame1 window) {
super(window, "Font Properties", true);
this.window = window;
font = window.getCurrentFont();
fontstyle = font.getStyle();
fontsize = font.getSize(); JPanel buttonPane = new JPanel(); buttonPane.add(ok = createButton("OK"));
buttonPane.add(cancel = createButton("Cancel"));
this.getContentPane().add(buttonPane,BorderLayout.SOUTH); JPanel dataPane = new JPanel();
dataPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.
createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagLayout gbLayout = new GridBagLayout();
dataPane.setLayout(gbLayout);
GridBagConstraints constraints = new GridBagConstraints(); JLabel label = new JLabel("Choose a font");
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gbLayout.setConstraints(label,constraints);
dataPane.add(label); GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontnames = e.getAvailableFontFamilyNames(); fontList = new JList(fontnames);
fontList.setValueIsAdjusting(true);
fontList.setSelectedIndex(ListSelectionModel.SINGLE_SELECTION);
fontList.setSelectedValue(font.getFamily(),true);
fontList.addListSelectionListener(this);
JScrollPane chooseFont = new JScrollPane(fontList);
chooseFont.setMinimumSize(new Dimension(300,100)); JPanel display = new JPanel();
fontDisplay = new JLabel("That is Ali's Virgin Work");
fontDisplay.setPreferredSize(new Dimension(300,100)); display.add(fontDisplay); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,chooseFont,display);
splitPane.setOneTouchExpandable(true);
gbLayout.setConstraints(splitPane,constraints);
dataPane.add(splitPane); JPanel sizePane = new JPanel();
label = new JLabel("Choose point size");
sizePane.add(label);
String[] sizeList = {"8","10","12","14","16","18","20","22","24"};
chooseSize = new JComboBox(sizeList);
chooseSize.setSelectedItem(Integer.toString(fontsize));
chooseSize.addActionListener(this);
sizePane.add(chooseSize);
gbLayout.setConstraints(sizePane,constraints);
dataPane.add(sizePane); JRadioButton bold = new JRadioButton("Bold",(fontstyle & font.BOLD)>0);
JRadioButton italic = new JRadioButton("italic",(fontstyle & font.ITALIC)>0);
bold.addItemListener(new StyleListener(Font.BOLD));
italic.addItemListener(new StyleListener(Font.ITALIC));
JPanel stylePane = new JPanel();
stylePane.add(bold);
stylePane.add(italic);
gbLayout.setConstraints(stylePane,constraints);
dataPane.add(stylePane); getContentPane().add(dataPane,BorderLayout.CENTER);
this.pack();
this.setVisible(false);
} JButton createButton(String label) {
JButton button = new JButton(label);
button.setPreferredSize(new Dimension(80, 20));
button.addActionListener(this);
return button;
} public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == ok) {
window.setCurrentFont(font);
this.setVisible(false);
}
else if (source == cancel) {
this.setVisible(false);
}
else if(source == chooseSize) {
fontsize = Integer.parseInt((String)chooseSize.getSelectedItem());
font = font.deriveFont((float)fontsize);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
} public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) {
font = new Font((String)fontList.getSelectedValue(),fontstyle,fontsize);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
} // ********************************************************
// ******************** Style Listener ********************
// ********************************************************
class StyleListener implements ItemListener { private int style; public StyleListener(int style) {
this.style = style;
} public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
fontstyle |= style;
}
else
fontstyle &= ~style;
font = font.deriveFont(fontstyle);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}}
package jtest;import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.event.*;public class FontDialog
extends JDialog
implements Constants, ActionListener,ListSelectionListener { private Frame1 window;
private Font font;
private int fontstyle;
private int fontsize; private JButton ok;
private JButton cancel; private JList fontList;
private JComboBox chooseSize; JLabel fontDisplay; public FontDialog(Frame1 window) {
super(window, "Font Properties", true);
this.window = window;
font = window.getCurrentFont();
fontstyle = font.getStyle();
fontsize = font.getSize(); JPanel buttonPane = new JPanel(); buttonPane.add(ok = createButton("OK"));
buttonPane.add(cancel = createButton("Cancel"));
this.getContentPane().add(buttonPane,BorderLayout.SOUTH); JPanel dataPane = new JPanel();
dataPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.
createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
GridBagLayout gbLayout = new GridBagLayout();
dataPane.setLayout(gbLayout);
GridBagConstraints constraints = new GridBagConstraints(); JLabel label = new JLabel("Choose a font");
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridwidth = GridBagConstraints.REMAINDER;
gbLayout.setConstraints(label,constraints);
dataPane.add(label); GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontnames = e.getAvailableFontFamilyNames(); fontList = new JList(fontnames);
fontList.setValueIsAdjusting(true);
fontList.setSelectedIndex(ListSelectionModel.SINGLE_SELECTION);
fontList.setSelectedValue(font.getFamily(),true);
fontList.addListSelectionListener(this);
JScrollPane chooseFont = new JScrollPane(fontList);
chooseFont.setMinimumSize(new Dimension(300,100)); JPanel display = new JPanel();
fontDisplay = new JLabel("That is Ali's Virgin Work");
fontDisplay.setPreferredSize(new Dimension(300,100)); display.add(fontDisplay); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,chooseFont,display);
splitPane.setOneTouchExpandable(true);
gbLayout.setConstraints(splitPane,constraints);
dataPane.add(splitPane); JPanel sizePane = new JPanel();
label = new JLabel("Choose point size");
sizePane.add(label);
String[] sizeList = {"8","10","12","14","16","18","20","22","24"};
chooseSize = new JComboBox(sizeList);
chooseSize.setSelectedItem(Integer.toString(fontsize));
chooseSize.addActionListener(this);
sizePane.add(chooseSize);
gbLayout.setConstraints(sizePane,constraints);
dataPane.add(sizePane); JRadioButton bold = new JRadioButton("Bold",(fontstyle & font.BOLD)>0);
JRadioButton italic = new JRadioButton("italic",(fontstyle & font.ITALIC)>0);
bold.addItemListener(new StyleListener(Font.BOLD));
italic.addItemListener(new StyleListener(Font.ITALIC));
JPanel stylePane = new JPanel();
stylePane.add(bold);
stylePane.add(italic);
gbLayout.setConstraints(stylePane,constraints);
dataPane.add(stylePane); getContentPane().add(dataPane,BorderLayout.CENTER);
this.pack();
this.setVisible(false);
} JButton createButton(String label) {
JButton button = new JButton(label);
button.setPreferredSize(new Dimension(80, 20));
button.addActionListener(this);
return button;
} public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == ok) {
window.setCurrentFont(font);
this.setVisible(false);
}
else if (source == cancel) {
this.setVisible(false);
}
else if(source == chooseSize) {
fontsize = Integer.parseInt((String)chooseSize.getSelectedItem());
font = font.deriveFont((float)fontsize);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
} public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) {
font = new Font((String)fontList.getSelectedValue(),fontstyle,fontsize);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
} // ********************************************************
// ******************** Style Listener ********************
// ********************************************************
class StyleListener implements ItemListener { private int style; public StyleListener(int style) {
this.style = style;
} public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
fontstyle |= style;
}
else
fontstyle &= ~style;
font = font.deriveFont(fontstyle);
fontDisplay.setFont(font);
fontDisplay.repaint();
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货