通过选择框更改字体 [英] Changing fonts through a choice box

查看:70
本文介绍了通过选择框更改字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// Import Core Java packages
import java.awt.*;
import java.awt.event.*;

public class Welcome extends Frame {
 
	// Constants and variables used in the program
	static final int WIDTH = 500;                // frame width
	static final int HEIGHT = 450;               // frame height
	static final int SMALL_SIZE = 16;            // small font size
	static final int MEDIUM_SIZE = 20;           // medium font size
	static final int LARGE_SIZE = 24;            // large font size
	static final String COLOR_NAMES[] = {"Red", "Blue", "Green"};
	static final Color COLORS[] = {Color.RED, Color.BLUE, Color.GREEN};
	
	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
	
	Font TimesRoman = new Font ("TimesRoman", typeStyle, typeSize);
	Font Courier = new Font ("Courier", typeStyle, typeSize);
	Font Dialog = new Font ("Dialog", typeStyle, typeSize);
	
	Label text;                            // display text
	Checkbox small;                        // checkbox for small font
	Checkbox medium;                       // checkbox for medium font
	Checkbox large;                        // checkbox for large font
	Checkbox plain;
	Checkbox bold;
	Checkbox italic;
	Choice colorChoice;                    // choice box for text color
	Choice faceFont;
	Button resetButton;                    // button to reset font and color
	Button exitButton;
    /**
     * Constructor
     */
	public Welcome() {
	    setTitle("Welcome Java Program");             // 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);

	    Panel controlPanel = new Panel();
	    controlPanel.setLayout(new GridLayout(0, 1));
	    add(controlPanel, BorderLayout.SOUTH);

        // create choice box
        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()]);
            }
        });

        // create radio buttons
	    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);
	            }
	        }
	    });


        // create reset button
        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);          // reset radio button
		        colorChoice.select(0);         // reset color choice box
	            text.setForeground(COLORS[0]);  // reset text color
	            typeStyle = INITIAL_STYLE;
	            typeSize = INITIAL_SIZE;       // reset type size
	            fontChange(typeFace, typeStyle, typeSize);      // reset text font
	        }
	    });
		
		
		exitButton = new Button("Exit");
		panel.add(exitButton);
		exitButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
		        System.exit(0);
	        }
	    });
		
	} // end of constructor

    /**
     *  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);
    }

    /**
     * the main method
     */
    public static void main(String[] argv) {
        // Create frame
        Welcome welcome = new Welcome();
	    welcome.setBackground(Color.WHITE);
        welcome.setSize(WIDTH, HEIGHT);
        welcome.setLocation(150, 100);

        // add window closing listener
        welcome.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                System.exit(0);
            }
        });

        // Show the frame
        welcome.setVisible(true);
    }
}





有我的代码。



我需要做的是制作3种类型的字体(TimesRoman,Courier和Dialog)

选择框中的选项。



不知道我是怎么做的,必须在这里做点什么:





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.

推荐答案

你可能需要使用 JList [ ^ ]。
You probably need to use a JList[^].


这篇关于通过选择框更改字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆