简体   繁体   中英

Issue related to JComboBox

I have 3 controls on frame 2 COMBOBOXES(swing) and 1 text box. Text box contains some text.One combobox contains font type and other contains font size.

The text in the text box should change its size and type are per selection in comboboxes. But i am not getting how to know which combobox is selected.

I have written the following code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


class changeFont extends JFrame implements ItemListener
{

    JComboBox fonttype,fontsize;
    JTextField tf;
    Prog17()
    {
        super("Title");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(300,300);

        fonttype=new JComboBox();
        fontsize=new JComboBox();

        String fontlist[];
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        fontlist=ge.getAvailableFontFamilyNames();



        for(int i=0;i<fontlist.length;i++)
            fonttype.addItem(fontlist[i]);  

        add(fonttype);  
        fonttype.addItemListener(this);     


        fontsize.addItem("10");
        fontsize.addItem("20");
        fontsize.addItem("30");
        add(fontsize);

        fontsize.addItemListener(this);
        tf=new JTextField("Game Over");
        add(tf);

    }


    public void itemStateChanged(ItemEvent ie)
    {   

    }

    public static void main(String args[])
    {
        changeFont obj1=new changeFont();
    }
}

itemStateChanged检查ie.getSource()==fonttypeie.getSource()==fontsize

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM