Write a GUI program to sample different fonts. The user enters a line of text in a text field. The user then selects a font from a font menu. Offer the three guaranteed fonts and at least two other fonts. The user also selects any desired style modifiers (bold and/or italic) from a style menu, and selects point size from a point size menu that offers the following choices: 9, 10, 12, 14, 16, 24, and 32. There is also a " Display " button. When the " Display " button is clicked, the text is displayed in the font, style, and point size chosen.
// import packages
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class stylefonts extends JFrame implements ActionListener {
//declare the variables
private boolean Fontitalic;
private boolean Fontbold;
private JTextField IO;
//initialize the current values
private int currentFontSize = 12;
private String currentFontName = "Times New Roman";
// default constructor
public stylefonts() {
super("Text Modifier");
setSize(700, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JMenuBar bar = new JMenuBar();
//create the menu for font,size and style.
JMenu fontMenu = new JMenu("Font");
JMenu sizeMenu = new JMenu("Size");
JMenu styleMenu = new JMenu("Style");
//add the font name to menu
JMenuItem font_monospaced = new JMenuItem("SansSerif");
font_monospaced.addActionListener(this);
fontMenu.add(font_monospaced);
//add the font name to menu
JMenuItem font_sansSerif = new JMenuItem("Verdana");
font_sansSerif.addActionListener(this);
fontMenu.add(font_sansSerif);
//add the font name to menu
JMenuItem font_serif = new JMenuItem("Arial");
font_serif.addActionListener(this);
fontMenu.add(font_serif);
//add the font name to menu
JMenuItem font_times = new JMenuItem("FreeMano");
font_times.addActionListener(this);
fontMenu.add(font_times);
//add the font name to menu
JMenuItem font_courier = new JMenuItem("Courier New");
font_courier.addActionListener(this);
fontMenu.add(font_courier);
//add the font size to menu
JMenuItem fontSize_9 = new JMenuItem("9");
fontSize_9.addActionListener(this);
sizeMenu.add(fontSize_9);
//add the font size to menu
JMenuItem fontSize_10 = new JMenuItem("10");
fontSize_10.addActionListener(this);
sizeMenu.add(fontSize_10);
//add the font size to menu
JMenuItem fontSize_12 = new JMenuItem("12");
fontSize_12.addActionListener(this);
sizeMenu.add(fontSize_12);
//add the font size to menu
JMenuItem fontSize_14 = new JMenuItem("14");
fontSize_14.addActionListener(this);
sizeMenu.add(fontSize_14);
//add the font size to menu
JMenuItem fontSize_16 = new JMenuItem("16");
fontSize_16.addActionListener(this);
sizeMenu.add(fontSize_16);
//add the font size to menu
JMenuItem fontSize_24 = new JMenuItem("24");
fontSize_24.addActionListener(this);
sizeMenu.add(fontSize_24);
//add the font size to menu
JMenuItem fontSize_32 = new JMenuItem("32");
fontSize_32.addActionListener(this);
sizeMenu.add(fontSize_32);
//add the font style to menu
JMenuItem boldMenu = new JMenuItem("Bold");
boldMenu.addActionListener(this);
styleMenu.add(boldMenu);
//add the font style to menu
JMenuItem italicMenu = new JMenuItem("Italic");
italicMenu.addActionListener(this);
styleMenu.add(italicMenu);
//add the menus to bar
bar.add(fontMenu);
bar.add(sizeMenu);
bar.add(styleMenu);
IO = new JTextField(30);
JPanel top = new JPanel();
top.add(bar);
top.add(IO);
//create the button
JButton displayButton = new JButton("Display");
displayButton.addActionListener(this);
displayButton.setActionCommand("Display");
add(top, BorderLayout.NORTH);
add(displayButton, BorderLayout.SOUTH);
currentFontName = "Times New Roman";
currentFontSize = 12;
Fontitalic = false;
Fontbold = false;
}
// paint method
public void paint(Graphics g) {
super.paint(g);
Font currentFont = null;
if (!Fontbold && !Fontitalic)
currentFont = new Font(currentFontName, Font.PLAIN, currentFontSize);
else if (Fontbold && !Fontitalic)
currentFont = new Font(currentFontName, Font.BOLD, currentFontSize);
else if (!Fontbold && Fontitalic)
currentFont = new Font(currentFontName, Font.ITALIC, currentFontSize);
else
currentFont = new Font(currentFontName, Font.BOLD | Font.ITALIC, currentFontSize);
g.setFont(currentFont);
g.drawString(IO.getText(), 50, 100);
}
// actionPerformed method
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("9") || s.equals("10") || s.equals("12") || s.equals("14") || s.equals("16") || s.equals("24") || s.equals("32")) {
currentFontSize = Integer.parseInt(s);
}
if (s.equals("SansSerif") || s.equals("Verdana") || s.equals("Arial") || s.equals("FreeMano") || s.equals("Courier New")) {
currentFontName = s;
}
if (s.equals("Bold")) {
Fontbold = !Fontbold;
}
if (s.equals("Italic")) {
Fontitalic = !Fontitalic;
}
if (s.equals("Display")) {
repaint();
}
}
// main method
public static void main(String args[]) {
//create the object for class stylefonts
stylefonts test = new stylefonts();
test.setVisible(true);
}
}