import java.awt.*;
import java.awt.event.*;
public class Welcome extends Frame {
static final int WIDTH = 500;
static final int HEIGHT = 450;
static final int SMALL_SIZE = 16;
static final int MEDIUM_SIZE = 20;
static final int LARGE_SIZE = 24;
static final String COLOR_NAMES[] = {"Red", "Blue", "Green"};
static final Color COLORS[] = {Color.RED, Color.BLUE, Color.GREEN};
static final String INITIAL_FACE = "TimesRoman";
static final int INITIAL_STYLE = Font.PLAIN;
static final int INITIAL_SIZE = LARGE_SIZE;
String typeFace = INITIAL_FACE;
int typeStyle = INITIAL_STYLE;
int typeSize = INITIAL_SIZE;
Font TimesRoman = new Font ("TimesRoman", typeStyle, typeSize);
Font Courier = new Font ("Courier", typeStyle, typeSize);
Font Dialog = new Font ("Dialog", typeStyle, typeSize);
Label text;
Checkbox small;
Checkbox medium;
Checkbox large;
Checkbox plain;
Checkbox bold;
Checkbox italic;
Choice colorChoice;
Choice faceFont;
Button resetButton;
Button exitButton;
public Welcome() {
setTitle("Welcome Java Program");
text = new Label("Welcome to the World of Java");
text.setAlignment(Label.CENTER);
text.setFont(new Font(typeFace, typeStyle, typeSize));
text.setForeground(COLORS[0]);
add(text, BorderLayout.CENTER);
Panel controlPanel = new Panel();
controlPanel.setLayout(new GridLayout(0, 1));
add(controlPanel, BorderLayout.SOUTH);
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
controlPanel.add(panel);
Label label = new Label("Select a text color");
panel.add(label);
colorChoice = new Choice();
colorChoice.add(COLOR_NAMES[0]);
colorChoice.add(COLOR_NAMES[1]);
colorChoice.add(COLOR_NAMES[2]);
panel.add(colorChoice);
colorChoice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
text.setForeground(COLORS[colorChoice.getSelectedIndex()]);
}
});
Label label1 = new Label("Select a Font style");
panel.add(label1);
faceFont = new Choice();
faceFont.add(COLOR_NAMES[0]);
faceFont.add(COLOR_NAMES[1]);
faceFont.add(TimesRoman);
panel.add(faceFont);
faceFont.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
text.setForeground(COLORS[colorChoice.getSelectedIndex()]);
}
});
panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
controlPanel.add(panel);
label1 = new Label("Select a type size");
panel.add(label1);
CheckboxGroup sizeGroup = new CheckboxGroup();
small = new Checkbox("Small", sizeGroup, false);
panel.add(small);
small.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(small.getState()) {
typeSize = SMALL_SIZE;
fontChange(typeFace, typeStyle, typeSize);
}
}
});
medium = new Checkbox("Medium", sizeGroup, true);
panel.add(medium);
medium.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(medium.getState()) {
typeSize = MEDIUM_SIZE;
fontChange(typeFace, typeStyle, typeSize);
}
}
});
large = new Checkbox("Large", sizeGroup, false);
panel.add(large);
large.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(large.getState()) {
typeSize = LARGE_SIZE;
fontChange(typeFace, typeStyle, typeSize);
}
}
});
panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
controlPanel.add(panel);
label1 = new Label("Select a type style");
panel.add(label1);
CheckboxGroup styleGroup = new CheckboxGroup();
italic = new Checkbox("Italic", styleGroup, false);
panel.add(italic);
italic.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(italic.getState()) {
typeStyle = Font.ITALIC ;
fontChange(typeFace, typeStyle, typeSize);
}
}
});
bold = new Checkbox("Bold", styleGroup, false);
panel.add(bold);
bold.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(bold.getState()) {
typeStyle = Font.BOLD ;
fontChange(typeFace, typeStyle, typeSize);
}
}
});
plain = new Checkbox("Plain", styleGroup, true);
panel.add(plain);
plain.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
if(plain.getState()) {
typeStyle = Font.PLAIN ;
fontChange(typeFace, typeStyle, typeSize);
}
}
});
panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
controlPanel.add(panel);
resetButton = new Button("Reset");
panel.add(resetButton);
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
large.setState(true);
colorChoice.select(0);
text.setForeground(COLORS[0]);
typeStyle = INITIAL_STYLE;
typeSize = INITIAL_SIZE;
fontChange(typeFace, typeStyle, typeSize);
}
});
exitButton = new Button("Exit");
panel.add(exitButton);
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
}
private void fontChange(String typeFace, int typeStyle, int typeSize) {
Font font = new Font(typeFace, typeStyle, typeSize);
text.setFont(font);
}
public static void main(String[] argv) {
Welcome welcome = new Welcome();
welcome.setBackground(Color.WHITE);
welcome.setSize(WIDTH, HEIGHT);
welcome.setLocation(150, 100);
welcome.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
welcome.setVisible(true);
}
}
There is my code.
What I need to do is make 3 types of Font ( TimesRoman, Courier, and Dialog)
Choices in the Choice Boxes.
Not sure how i go about doing that, something has to be done here:
Label label1 = new Label("Select a Font style");
panel.add(label1);
faceFont = new Choice();
faceFont.add(COLOR_NAMES[0]);
faceFont.add(COLOR_NAMES[1]);
faceFont.add(TimesRoman);
panel.add(faceFont);
faceFont.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
text.setForeground(COLORS[colorChoice.getSelectedIndex()]);
}
});
Any help is appreciated.