In this tutorial we are going to learn how we can create following GUI using java.
Program :-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class B1 extends JFrame implements ItemListener
{
JLabel l1,l2,l3;
JCheckBox jc[]=new JCheckBox[3];
JComboBox jcs;
JComboBox jcf;
JPanel p4,p3,p1,p2;
JTextField t;
B1()
{
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
t=new JTextField(10);
l1=new JLabel("Font");
l2=new JLabel("Style");
l3=new JLabel("Size",JLabel.CENTER);
String[] fonts=new String[]{"Arial","Times New Roman","Courier New"};
String[] size=new String[]{"10","15","20","25"};
jcf=new JComboBox(fonts);
jcs=new JComboBox(size);
jcs.addItemListener(this);
jcf.addItemListener(this);
p1.add(l1);
p1.add(jcf);
p2.add(l2);
p3.add(l3);
p3.add(jcs);
l1.setBounds(100,100,25,25);
l2.setBounds(210,100,25,25);
p4.add(t);
jc[0]=new JCheckBox("Bold");
jc[1]=new JCheckBox("Italic");
jc[2]=new JCheckBox("UnderLine");
for(int i=0;i<3;i++)
{
p2.add(jc[i]);
jc[i].addItemListener(this);
}
add(p1);
add(p2);
add(p3);
add(p4);
setLayout(new GridLayout(2,2));
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ie)
{
String fnt=(String)jcf.getSelectedItem();
String size1=(String)jcs.getSelectedItem();
int size=Integer.parseInt(size1);
String cb="";
for(int i=0;i<3;i++)
{
if(jc[i].isSelected())
{
cb=jc[i].getText();
}
}
if(cb.equals("Bold"))
{
t.setFont(new Font(fnt,Font.BOLD,size));
}
else if(cb.equals("Italic"))
{
t.setFont(new Font(fnt,Font.ITALIC,size));
}
else
t.setFont(new Font(fnt,Font.PLAIN,size));
}
public static void main(String ar[])
{
new B1();
}
}
Output :-
Thank You💚
Comments
Post a Comment