Created
June 3, 2019 23:03
-
-
Save ahmednasserpro/b516593565227f151c500e519f373f28 to your computer and use it in GitHub Desktop.
java test all font
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.border.EmptyBorder; | |
public class FontTest extends JFrame { | |
String font = "Dialog"; | |
int size = 12; | |
int fontStyleBold = 0; | |
int fontStyleItalic = 0; | |
JLabel fontNameLabel = new JLabel("Font Name"); | |
JComboBox fontName; | |
JLabel fontSizeLabel = new JLabel("Font Size"); | |
JComboBox fontSize; | |
MessagePanel message = new MessagePanel("Java is cool!"); | |
JCheckBox centered = new JCheckBox("Centered"); | |
JCheckBox bold = new JCheckBox("Bold"); | |
JCheckBox italic = new JCheckBox("Italic"); | |
GraphicsEnvironment locale = GraphicsEnvironment.getLocalGraphicsEnvironment(); | |
FontTest() { | |
String[] fonts = locale.getAvailableFontFamilyNames(); | |
fontName = new JComboBox(fonts); | |
fontSize = new JComboBox(getFontSizes()); | |
JPanel fontNamePanel = new JPanel(new BorderLayout(10, 10)); | |
fontNamePanel.add(fontNameLabel, BorderLayout.WEST); | |
fontNamePanel.add(fontName, BorderLayout.CENTER); | |
JPanel fontSizePanel = new JPanel(new BorderLayout(10, 10)); | |
fontSizePanel.add(fontSizeLabel, BorderLayout.WEST); | |
fontSizePanel.add(fontSize, BorderLayout.CENTER); | |
JPanel northPanel = new JPanel(new BorderLayout(10, 10)); | |
northPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); | |
northPanel.add(fontNamePanel, BorderLayout.CENTER); | |
northPanel.add(fontSizePanel, BorderLayout.EAST); | |
JPanel southPanel = new JPanel(); | |
southPanel.add(centered); | |
southPanel.add(bold); | |
southPanel.add(italic); | |
message.setBackground(Color.yellow); | |
add(northPanel, BorderLayout.NORTH); | |
add(message, BorderLayout.CENTER); | |
add(southPanel, BorderLayout.SOUTH); | |
fontName.addItemListener(new ItemListener() { | |
@Override | |
public void itemStateChanged(ItemEvent e) { | |
font = (String) fontName.getSelectedItem(); | |
message.setFont(new Font(font, fontStyleBold + fontStyleItalic, size)); | |
message.repaint(); | |
} | |
}); | |
fontSize.addItemListener(new ItemListener() { | |
@Override | |
public void itemStateChanged(ItemEvent e) { | |
size = (Integer) fontSize.getSelectedItem(); | |
message.setFont(new Font(font, fontStyleBold + fontStyleItalic, size)); | |
message.repaint(); | |
} | |
}); | |
bold.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (bold.isSelected()) | |
fontStyleBold = Font.BOLD; | |
else | |
fontStyleBold = 0; | |
message.setFont(new Font(font, fontStyleBold + fontStyleItalic, size)); | |
message.repaint(); | |
} | |
}); | |
italic.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (italic.isSelected()) | |
fontStyleItalic = Font.ITALIC; | |
else | |
fontStyleItalic = 0; | |
message.setFont(new Font(font, fontStyleBold + fontStyleItalic, size)); | |
message.repaint(); | |
} | |
}); | |
centered.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (centered.isSelected()) | |
message.setCentered(true); | |
else | |
message.setCentered(false); | |
} | |
}); | |
} | |
private Integer[] getFontSizes() { | |
Integer[] sizes = new Integer[100]; | |
for (int i = 0; i < 100; i++) | |
sizes[i] = i + 1; | |
return sizes; | |
} | |
public static void main(String[] args) { | |
JFrame frame = new FontTest(); | |
frame.setSize(800, 300); | |
frame.setLocationRelativeTo(null); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment