Q. Write a program to create the following GUI and apply the changes to the text in the TextField.
Program:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class Swing1 extends JFrame implements ItemListener
{
JLabel font, style, size;
JComboBox fontcb, sizecb;
JCheckBox bold, italic;
JTextField t;
JPanel p1, p2;
Swing1()
{ p1 = new JPanel();
p2 = new JPanel();
font = new JLabel("Font");
style = new JLabel("Style");
fontcb = new JComboBox();
fontcb.addItem("Arial");
fontcb.addItem("Sans");
fontcb.addItem("Monospace");
bold = new JCheckBox("Bold");
size = new JLabel("Size");
italic = new JCheckBox("Italic");
sizecb = new JComboBox();
sizecb.addItem("10");
sizecb.addItem("12");
sizecb.addItem("16");
t = new JTextField(10);
p1.setLayout(new GridLayout(4,2));
p1.add(font);
p1.add(style);
p1.add(fontcb);
p1.add(bold);
p1.add(size);
p1.add(italic);
p1.add(sizecb);
p2.setLayout(new FlowLayout());
p2.add(t);
bold.addItemListener(this);
italic.addItemListener(this);
fontcb.addItemListener(this);
sizecb.addItemListener(this);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ie)
{
String f = (String)fontcb.getSelectedItem();
System.out.println("font = "+f);
t.setFont(new Font(f,Font.BOLD,10));
String no =(String)sizecb.getSelectedItem();
int num=Integer.parseInt(no);
if(bold.isSelected())
{
t.setFont(new Font(f,Font.BOLD,num));
}
if(italic.isSelected())
{
t.setFont(new Font(f,Font.ITALIC,num));
}
}
public static void main(String args[])
{
Swing1 f1 = new Swing1();
}
}
import java.awt.*;
import javax.swing.*;
class Swing1 extends JFrame implements ItemListener
{
JLabel font, style, size;
JComboBox fontcb, sizecb;
JCheckBox bold, italic;
JTextField t;
JPanel p1, p2;
Swing1()
{ p1 = new JPanel();
p2 = new JPanel();
font = new JLabel("Font");
style = new JLabel("Style");
fontcb = new JComboBox();
fontcb.addItem("Arial");
fontcb.addItem("Sans");
fontcb.addItem("Monospace");
bold = new JCheckBox("Bold");
size = new JLabel("Size");
italic = new JCheckBox("Italic");
sizecb = new JComboBox();
sizecb.addItem("10");
sizecb.addItem("12");
sizecb.addItem("16");
t = new JTextField(10);
p1.setLayout(new GridLayout(4,2));
p1.add(font);
p1.add(style);
p1.add(fontcb);
p1.add(bold);
p1.add(size);
p1.add(italic);
p1.add(sizecb);
p2.setLayout(new FlowLayout());
p2.add(t);
bold.addItemListener(this);
italic.addItemListener(this);
fontcb.addItemListener(this);
sizecb.addItemListener(this);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent ie)
{
String f = (String)fontcb.getSelectedItem();
System.out.println("font = "+f);
t.setFont(new Font(f,Font.BOLD,10));
String no =(String)sizecb.getSelectedItem();
int num=Integer.parseInt(no);
if(bold.isSelected())
{
t.setFont(new Font(f,Font.BOLD,num));
}
if(italic.isSelected())
{
t.setFont(new Font(f,Font.ITALIC,num));
}
}
public static void main(String args[])
{
Swing1 f1 = new Swing1();
}
}
Output:
2 Comments
This comment has been removed by the author.
ReplyDeleteSlip16_1: Write a program to find the Square of given number using function interface.
ReplyDeleteinterface Square {
int calculate(int x);
}
class Slip16_1 {
public static void main(String args[])
{
//you can read value from user
int a = 5;
Square s = (int x) -> x * x;
int ans = s.calculate(a);
System.out.println(ans);
}
}
Slip16_2: Write a program to design a screen using Awt that,
import javax.swing.*;
import java.awt.*;
class MeEx
{
JMenu File;
JMenu Edit;
JMenu About;
JMenuItem i1, i2, i3, i4, i5;
MeEx(){
JFrame f= new JFrame("Menu and MenuItem Example");
JMenuBar mb=new JMenuBar();
File=new JMenu("File");
Edit=new JMenu("Edit");
About=new JMenu("About");
i1=new JMenuItem("New Ctrl+N");
i2=new JMenuItem("Open");
i3=new JMenuItem("Save");
i4=new JMenuItem("Show About");
i5=new JMenuItem("Exit");
File.add(i1); File.add(i2);
File.add(i3);File.addSeparator();File.add(i4);File.addSeparator();File.add(i5);
mb.add(File);
mb.add(Edit);
mb.add(About);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MeEx();
}
}