/* * Gets the user choice for font, style and size and redraws the text * accordingly. */ public void setSampleFont() { // Get the font name from the JComboBox fontName = (String) facenameCombo.getSelectedItem(); sampleField.setText(textField.getText()); // Get the font style from the JCheckBoxes fontStyle = 0; if (italicCheckBox.isSelected()) fontStyle += Font.ITALIC; if (boldCheckBox.isSelected()) fontStyle += Font.BOLD; // Get the font size fontSize = 0; fontSize = Integer.parseInt((String) sizeCombo.getSelectedItem()); // THE FOLLOWING IS NO LONGER NEEDED // if(smallButton.isSelected()) // fontSize=SMALL; // else if(mediumButton.isSelected()) // fontSize=MEDIUM; // else if(largeButton.isSelected()) // fontSize=LARGE; // Set the font of the text field sampleField.setFont(new Font(fontName, fontStyle, fontSize)); sampleField.setForeground(fontColor); sampleField.repaint(); pack(); } // end setSampleFont method
/* * The following method creates the textfield to change the text * and the button to update the label. * postcondition: returns the panel containing the textfield and button. */ public JPanel createUpdateButton() { JLabel textLabel = new JLabel(new String("Change text to: ")); textField = new JTextField(new String("Big Java"), 20); textField.setFont(new Font(("Times"), Font.PLAIN, 12)); update = new JButton(new String("Update")); update.setDefaultCapable(true); // This class is used to create a special ActionListener for this menu item class ButtonListener implements ActionListener { /* * This method is called when the update button is clicked */ public void actionPerformed(ActionEvent event) { // Call the method to change the text on the screen. setSampleFont(); } // end actionPerformed method } ActionListener listener = new ButtonListener(); update.addActionListener(listener); JPanel panel = new JPanel(); panel.add(textLabel); panel.add(textField); panel.add(update); return panel; } // end createUpdateButton method
/* * The following method resets the window to its original status. */ public void init() { sampleField.setText(new String("Big Java")); textField.setText(new String("Big Java")); facenameCombo.setSelectedIndex(0); sizeCombo.setSelectedIndex(2); italicCheckBox.setSelected(false); boldCheckBox.setSelected(false); // largeButton.setSelected(true); fontColor = Color.BLACK; setSampleFont(); } // end init method