Professional Documents
Culture Documents
Gui
Gui
Gui
swing)
Q1. Q6.
import javax.swing.*; import javax.swing.*;
class Calculator1{
public static void main(String args[]){ class DemoJPanel extends JFrame{
JFrame cal=new JFrame("Calculator"); JPanel pane;
} DemoJPanel(){
} pane=new JPanel();
add(pane);//add panel to frame
Q2. setTitle("JPanelDemo");//set the title
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
import javax.swing.*;
setSize(200,300);
class Calculator2{
setVisible(true);
public static void main(String args[]){
}
JFrame cal=new JFrame("Calculator");
}
cal.setVisible(true); //cal.show();
class MyDemoPanel{
}
public static void main(String []args){
}
new DemoJPanel();
}
Q3. }
import javax.swing.*;
class Calculator{
public static void main(String args[]){
Q7.
import javax.swing.*;
JFrame cal=new JFrame("Calculator");
cal.setSize(350,300);
class DemoLabel extends JFrame{
cal.setVisible(true);
JLabel l;
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
DemoLabel(){
}
l=new JLabel("This is a Label");
add(l);
Q4. setSize(100,100);
import javax.swing.*; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class SimpleJFrame extends JFrame{ show();
SimpleJFrame(){ }
setSize(200,300);//set the size }
show();//show the frame class MyLabel{
} public static void main(String []args){
} new DemoLabel();
class DemoMySimpleFrame{ }
public static void main(String []args){ }
new SimpleJFrame();
}
}
Q8.
import javax.swing.*;
import java.awt.*;
Q5.
import javax.swing.*; class DemoLabel extends JFrame{
JLabel l;
class DemoJFrame extends JFrame{
DemoJFrame(){ DemoLabel(){
super("JFrameDemo");//set the title l=new JLabel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); l.setText("This is a Label");
setSize(200,300); add(l);
setVisible(true);//show the frame setSize(100,100);
} show();
} }
class DemoMySimpleFrame{ public static void main(String []args){
public static void main(String []args){ new DemoLabel();
new DemoJFrame(); }
} }
}
DemoLabel(){ DemoJLabel(){
l=new JLabel("Label",new ImageIcon("im2.jpg"),2); lbl1=new JLabel("My Label");
add(l); lbl2=new JLabel("Your Label");
setSize(200,200); pane=new JPanel();
show();
} pane.add(lbl1);//add label to pane
} pane.add(lbl2);//add label to pane
class MyDemoLabel{
public static void main(String []args){ getContentPane().add(pane);//add panel to frame
new DemoLabel(); //add(pane);
} setTitle("JLabelDemo");
} setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
Q10. setVisible(true);
}
import javax.swing.*;
}
class DemoLabel extends JFrame{
class MyDemoLabel{
JLabel l;
public static void main(String []args){
DemoJLabel jlabel=new DemoJLabel();
DemoLabel(){
}
l=new JLabel("Label");
}
l.setIcon(new ImageIcon("im2.jpg"));
l.setHorizontalTextPosition(JLabel.LEFT);
add(l); Q13.
pack(); //Auto arrange import javax.swing.*;
show(); class GoodLabel extends JFrame{
} JPanel pane;
public static void main(String []args){ JLabel lbl;
new DemoLabel();
} GoodLabel(){
} pane=new JPanel();
lbl=new JLabel();
Q11.
import javax.swing.*; lbl.setText("Good");//display the message
class DemoJLabel extends JFrame{ lbl.setIcon(new ImageIcon("icon.gif"));//set Icon
JLabel lbl1;
JLabel lbl2; pane.add(lbl);
DemoJLabel(){ getContentPane().add(pane);
lbl1=new JLabel("My Label"); setTitle("JLabelDemo");
lbl2=new JLabel("Your Label"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(lbl1);//add label to Jframe setSize(200,100);
add(lbl2);//add label to Jframe setVisible(true);
}
setTitle("JLabelDemo"); }
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); class DemoGoodLabel{
pack(); public static void main(String []args){
setVisible(true); GoodLabel jlabel=new GoodLabel();
} }
}
}
class MyDemoLabel{
public static void main(String []args){
DemoJLabel jlabel=new DemoJLabel();
}
}
cal.add(b1); DemoJButton(){
cal.add(b2); pane=new JPanel();
cal.setSize(350,300); yes=new JButton("Yes");
cal.setVisible(true); no=new JButton("No");
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cancel=new JButton("Cancel");
}
} pane.add(yes);
pane.add(no);
Q15. pane.add(cancel);
import javax.swing.*;
getContentPane().add(pane);
setTitle("JButtonDemo");
class Calculator{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String args[]){
pack();
JFrame cal=new JFrame("Calculator");
setVisible(true);
JButton b1=new JButton("Yes");
}
JButton b2=new JButton("No");
}
class DemoButton{
JPanel pane=new JPanel();
public static void main(String []args){
pane.add(b1); //Adds Button to the JFram Container
DemoJButton jButton=new DemoJButton();
pane.add(b2);
}
cal.getContentPane().add(pane);
}
cal.setSize(350,300);
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cal.setVisible(true); Q18.
} import javax.swing.*;
} class GoodJButton extends JFrame{
JPanel pane;
Q16. JButton btn;
import javax.swing.*;
GoodJButton(){
import java.awt.*;
pane=new JPanel();
class Calculator{
btn=new JButton();
public static void main(String args[]){
JFrame cal=new JFrame("Calculator");
btn.setText("Good");
JButton b1=new JButton("Yes");
btn.setIcon(new ImageIcon("icon.gif"));
JButton b2=new JButton("No");
pane.add(btn);
cal.setLayout(new FlowLayout());
//java.awt.FlowLayout
getContentPane().add(pane);
cal.add(b1); //Adds Button to the JFram Container
setTitle("JButtonDemo");
cal.add(b2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cal.setSize(350,300);
pack();
cal.setVisible(true);
setVisible(true);
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
public static void main(String []args){
GoodJButton jbutton=new GoodJButton();
}
}
DemoCheckBox(){ p.add(combo);
super( "RadioButton Test" ); getContentPane().add(p);
p=new JPanel(); setSize(200, 140);
setVisible(true);
plain = new JCheckBox( "Plain"); }
add(p);
setSize( 300, 100 );
show();
}
public static void main( String args[] ){
DemoCheckBox ch = new DemoCheckBox();
}
}
import javax.swing.*;
class DemoJOptionPane1{ import javax.swing.*;
public static void main(String []args){ class DemoJOptionPane1{
JOptionPane.showMessageDialog(null, "Press Ok"); public static void main(String []args){
} ImageIcon image=new ImageIcon("Wow.gif");
} String num1=JOptionPane.showInputDialog(null, "Input First
Numer");
String num2=JOptionPane.showInputDialog(null, "Input
Q40. JOptionPane –Message Dialog Second Numer");
static void showMessageDialog(Component parentComponent, int x1=Integer.parseInt(num1);
Object message, String title,
int messageType) int x2=Integer.parseInt(num2);
int tot=x1+x2;
import javax.swing.*; String message="Total :"+tot;
class DemoJOptionPane1{ JOptionPane.showMessageDialog(null, message);
public static void main(String []args){ }
JOptionPane.showMessageDialog(null, "Press Ok","This is }
messagebox",0);
}
} Q43. JOptionPane –Input Dialog
static String showInputDialog(Component
parentComponent, Object message,
Type of message boxes : Object initialSelectionValue)
JOptionPane.ERROR_MESSAGE - 0
JOptionPane.PLAIN_MESSAGE - -1
JOptionPane.QUESTION_MESSAGE - import javax.swing.*;
JOptionPane.WARNING_MESSAGE - 2 class DemoJOptionPane1{
JOptionPane.INFORMATION_MESSAGE - 1 public static void main(String []args){
ImageIcon image=new ImageIcon("Wow.gif");
String num1=JOptionPane.showInputDialog(null, "Input First
Q41. JOptionPane –Message Dialog Numer","000");
showInternalMessageDialog(Component parentComponent, String num2=JOptionPane.showInputDialog(null, "Input
Object message, String title, int messageType, Icon icon) Second Numer","000");
int x1=Integer.parseInt(num1);
int x2=Integer.parseInt(num2);
import javax.swing.*;
class DemoJOptionPane1{
int tot=x1+x2;
public static void main(String []args){
String message="Total :"+tot;
ImageIcon image=new ImageIcon("Wow.gif");
JOptionPane.showMessageDialog(null, message);
JOptionPane.showMessageDialog(null, "alert", "alert",
}
JOptionPane.ERROR_MESSAGE,image);
}
}
}
Q50.
import javax.swing.*;
class DemoJOptionPane5{
public static void main(String []args){
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",JOptionPane.INFORMATION_MESSAGE,
null,possibleValues, possibleValues[0]);
}
}
DemoMenu(){ GoodMenu(){
pane=new JPanel(); pane=new JPanel();
mb=new JMenuBar();
mb=new JMenuBar();
mFile=new JMenu("File");
fNew=new JMenuItem("New"); mFile=new JMenu("File");
fOpen=new JMenuItem("Open"); fNew=new JMenuItem("New");
fExit=new JMenuItem("Exit"); fOpen=new JMenuItem("Open");
fExit=new JMenuItem("Exit");
mFile.add(fNew);
mFile.add(fOpen); mFile.setMnemonic(KeyEvent.VK_F);
mFile.add(fExit);
fNew.setMnemonic(KeyEvent.VK_N);
mEdit=new JMenu("Edit"); fOpen.setMnemonic(KeyEvent.VK_O);
eCopy=new JMenuItem("Copy"); fExit.setMnemonic(KeyEvent.VK_E);
ePaste=new JMenuItem("Paste");
eDelete=new JMenuItem("Delete"); fNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N
,Event.CTRL_MASK));
mEdit.add(eCopy); fOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_
mEdit.add(ePaste); O,Event.CTRL_MASK));
mEdit.add(eDelete); fExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
Event.CTRL_MASK));
mSearch=new JMenu("Search");
sFind=new JMenuItem("Find"); mFile.add(fNew);
mFile.add(fOpen);
mSearch.add(sFind); mFile.add(fExit);
mb.add(mEdit);
mb.add(mFile); mEdit=new JMenu("Edit");
mb.add(mSearch); eCopy=new JMenuItem("Copy");
ePaste=new JMenuItem("Paste");
setJMenuBar(mb); //set JMenuBar to JFrame eDelete=new JMenuItem("Delete");
setTitle("JLabelDemo"); mEdit.setMnemonic(KeyEvent.VK_E);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); eCopy.setMnemonic(KeyEvent.VK_C);
setSize(200,100); ePaste.setMnemonic(KeyEvent.VK_P);
setVisible(true); eDelete.setMnemonic(KeyEvent.VK_D);
}
public static void main(String []args){
DemoMenu mnu=new DemoMenu();
}
}
DemoInternalFrame(){
inter=new JInternalFrame("inter",true,true);
add(inter);
inter.show();
setSize(400,400);
show();
}
public static void main(String []args){
DemoInternalFrame d=new DemoInternalFrame();
}
}
Q66.
import javax.swing.*;
class Tabbed extends JFrame{
JTabbedPane tb;
Tabbed(){
tb=new JTabbedPane();
tb.addTab("A",new JButton("aaa"));
tb.addTab("B",new JButton("bbb"));
tb.addTab("C",new JButton("ccc"));
add(tb);
setSize(400,400);
setVisible(true);
}
public static void main(String []args){
new Tabbed();
}
}
}
class Demo extends JFrame{
JPanel p;
JButton btn;
Demo(){
p=new JPanel();
btn=new JButton("Press");
btn.addActionListener(new Event1());
p.add(btn);
add(p);
setSize(100,100);
show();
}
p.add(yes); add(p);
p.add(no); setSize(400,400);
p.add(cancel); show();
p.add(exit); }
Demo(){ Demo(){
p=new JPanel(); p=new JPanel();
yes=new JButton("Yes"); yes=new JButton("Yes");
no=new JButton("No"); no=new JButton("No");
cancel=new JButton("Cancel"); cancel=new JButton("Cancel");
exit=new JButton("Exit"); exit=new JButton("Exit");
yes.addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent evt){
JOptionPane.showMessageDialog(null, "You
pressed Yes Button");
}
public void mouseDragged(MouseEvent evt){
// JOptionPane.showMessageDialog(null, "You
pressed Yes Button");
}
});
no.addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent evt){
JOptionPane.showMessageDialog(null, "You
pressed No Button");
}
public void mouseDragged(MouseEvent evt){
// JOptionPane.showMessageDialog(null, "You
pressed No Button");
}
});
cancel.addMouseMotionListener(new
MouseMotionListener(){
public void mouseMoved(MouseEvent evt){
JOptionPane.showMessageDialog(null, "You
pressed Cancel Button");
}
public void mouseDragged(MouseEvent evt){
// JOptionPane.showMessageDialog(null, "You
pressed Cancel Button");
}
});
exit.addMouseMotionListener(new MouseMotionListener(){
public void mouseMoved(MouseEvent evt){
System.exit(0);
}
public void mouseDragged(MouseEvent evt){
System.exit(0);
}
Q78. BorderLayoyut
import java.awt.*;
import javax.swing.*;
class DemoBorder2 extends JFrame{
JButton btn1;
JButton btn2;
JButton btn3;
JButton btn4;
JButton btn5;
DemoBorder2(){
setLayout(new BorderLayout());
btn1=new JButton("West");
btn2=new JButton("East");
btn3=new JButton("South");
btn4=new JButton("North");
btn5=new JButton("Center");
add(btn1,"West");
add(btn2,"East");
add(btn3,"South");
add(btn4,"North");
add(btn5,"Center");
setSize(300,300);
show();
}
public static void main(String []args){
DemoBorder2 db=new DemoBorder2();
}
}
class KeyDisplay extends JFrame { class Positive extends JFrame implements FocusListener {
JLabel keyLabel = new JLabel("Hit any key"); JTextField[] value = new JTextField[3];