I am trying to Create a JFrame which holds five buttons with the names of five d
ID: 3766224 • Letter: I
Question
I am trying to Create a JFrame which holds five buttons with the names of five different fonts. I am also to include a 6th button that when the user clicks will make the font larger or smaller.
Then I am to display a demonstration JLabel using the font and size that the user selects. Save
the file as JFontSelector.java.
This is the code I have written so far. I am stuck at this point and can use some help to finish this code. Please explain the steps if at all possible. Also I do not need to use a cardLayout if it's not the best route but if it its used there should only be one button where the code I have written has two I have been unable to combine them into the same button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JFontSelector extends JFrame implements ActionListener
{
JButton button1 = new JButton("Arial");
JButton button2 = new JButton("Calibri");
JButton button3 = new JButton("Chiller");
JButton button4 = new JButton("JokerMan");
JButton button5 = new JButton("SansSerif");
JButton size1 = new JButton("Big");
JButton size2 = new JButton("Small");
Font arialFont = new Font("Arial", Font.PLAIN, 12);
Font calibri = new Font("Calibri", Font.PLAIN, 12);
Font chiller = new Font("Chiller", Font.PLAIN, 12);
Font jokerMan = new Font("JokerMan", Font.PLAIN, 12);
Font sansSerif = new Font("SansSerif", Font.PLAIN, 12);
Container con = getContentPane();
JPanel panel1 = new JPanel(new GridLayout(3, 0));
JPanel panel2 = new JPanel(new CardLayout());
public JFontSelector()
{
setLayout(new FlowLayout());
con.add(panel1);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
con.add(panel2);
panel2.add(size1);
panel2.add(size2);
button1.setFont(arialFont);
button2.setFont(calibri);
button3.setFont(chiller);
button4.setFont(jokerMan);
button5.setFont(sansSerif);
size1.addActionListener(this);
size2.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e)
{
panel2.next(getContentPane());
}
public static void main(String[] args)
{
JFontSelector font = new JFontSelector();
final int HEIGHT = 400;
final int WIDTH = 300;
font.setSize(HEIGHT, WIDTH);
font.setVisible(true);
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
public class FontFrame extends Frame implements ActionListener
{
Button hButton = new Button("Helvetica");
Button tButton = new Button("Times Roman");
Button cButton = new Button("Courier");
Button sButton = new Button("Sans Serif");
Button aButton = new Button("Arial");
Panel p = new Panel();
Font cFont = new Font("Courier", Font.PLAIN, 16);
Font hFont = new Font("Helvetica", Font.PLAIN, 16);
Font tFont = new Font("TimesRoman", Font.PLAIN, 16);
Font sFont = new Font("SansSerif", Font.PLAIN, 16);
Font aFont = new Font("Arial", Font.PLAIN, 16);
public FontFrame()
{
setLayout(new GridLayout(2,3));
setTitle("Font Frame");
add(hButton);
add(tButton);
add(cButton);
add(sButton);
add(aButton);
add(p);
hButton.addActionListener(this);
tButton.addActionListener(this);
cButton.addActionListener(this);
sButton.addActionListener(this);
aButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Graphics gr = getGraphics();
if(e.getSource() == hButton)
{
gr.setFont(hFont);
gr.drawString("Howdy",200,180);
}
else if(e.getSource() == tButton)
{
gr.setFont(tFont);
gr.drawString("Howdy",200,200);
}
else if(e.getSource() == sButton)
{
gr.setFont(sFont);
gr.drawString("Howdy",200,220);
}
else
{
gr.setFont(cFont);
gr.drawString("Howdy",200,240);
}
}
}
import java.awt.*;
public class UseFontFrame
{
public static void main(String [ ] args)
{
FontFrame ff = new FontFrame();
ff.setSize(300,300);
ff.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.