• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java Programming Question

Pegun

Golden Member
Hey all,
I tried posting this in Porgramming but not much response. I'm trying to write a program that will have a drop down choice box in which the user can choose a type face from the choices of TimesRoman, Courier, and Dialog. The Problem is once i fixed the errors i had previously, When i run the program, it only makes the text a smaller size when i select the different font style, rather than converting it to a different font style. (In other words, when i select Courier from the drop down menu, it makes the text small rather than courier.) Any one have any advice they can offer?



static final String COLOR_NAMES[] = {"Red", "Blue", "Green", "Pink", "Orange"};
static final Color COLORS[] = {Color.red, Color.blue, Color.green, Color.pink, Color.orange};
static final String FACE_NAMES[] = {"TimesRoman", "Courier", "Dialog"};
static final Font FACES[] = {Font.getFont("TimesRoman"), Font.getFont("Courier"), Font.getFont("Dialog")};
static final String INITIAL_FACE = "TimesRoman"; // initial typeface
static final int INITIAL_STYLE = Font.PLAIN; // initial type style
static final int INITIAL_SIZE = LARGE_SIZE; // initial type size

String typeFace = INITIAL_FACE; // current typeface
int typeStyle = INITIAL_STYLE; // current type style
int typeSize = INITIAL_SIZE; // current type size


Choice faceChoice;
Button resetButton; // button to reset font and color
Button exitButton; //exitButton

/**
* Constructor
*/
public Welcome() {
setTitle("Welcome"); // set frame title

// create text
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);


// create font choice box
Panel fontpanel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
controlPanel.add(fontpanel);
Label fontlabel = new Label("Select a Font");
panel.add(fontlabel);
faceChoice = new Choice();
faceChoice.add(FACE_NAMES[0]);
faceChoice.add(FACE_NAMES[1]);
faceChoice.add(FACE_NAMES[2]);

panel.add(faceChoice);
faceChoice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent event) {
text.setFont(FACES[faceChoice.getSelectedIndex ()]);
}
});



/**
* Method to change the text font
*/
private void fontChange(String typeFace, int typeStyle, int typeSize) {
Font font = new Font(typeFace, typeStyle, typeSize);
text.setFont (font);
}
 
Back
Top