Download Session 11 Student Name

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Object-Oriented Programming
Session 11
Student Name:
Roll/No:
Group:
Swing
Swing is the next generation GUI toolkit that sun micro system is developing to enable
enterprise development in java.
We have discussed detail in the class. There fore explore more features of swing on your
own.
Let’s see a few sample codes:
Experiment 1.0:
AWT Example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ToolbarFrame extends Frame implements ActionListener
{
Button cutButton,copyButton,pasteButton;
public ToolbarFrame()
{
super("AWT example of Toolbar");
setSize(500,500);
Panel toolbar=new Panel();
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
cutButton=new Button("cut");
cutButton.addActionListener(this);
toolbar.add(cutButton);
copyButton=new Button("copy");
copyButton.addActionListener(this);
toolbar.add(copyButton);
pasteButton=new Button("paste");
pasteButton.addActionListener(this);
toolbar.add(pasteButton);
Object-Oriented Programming
add(toolbar,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent ae)
{
System.out.println(ae.getActionCommand());
}
public static void main(String args[])
{
ToolbarFrame tbf=new ToolbarFrame();
tbf.setVisible(true);
}
}
Experiment 1.1:
I. Switching from AWT to Swing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Label_Text extends JFrame
{
JLabel label1,label2,label3;
public Label_Text()
{
super("JLabel Code");
setSize(500,500);
Container c=new Container();
c.setLayout(new FlowLayout());
label1=new JLabel("Label with text");
label1.setToolTipText("Label1");
c.add(label1);
Icon bug=new ImageIcon("Winter.gif");
Object-Oriented Programming
label2=new JLabel("Label with text and icon",bug,SwingConstants.LEFT);
label2.setToolTipText("Label2");
label3=new JLabel();
label3.setText("Label with text and icon at bottom");
label3.setIcon(bug);
label3.setToolTipText("Label2");
label3.setVerticalTextPosition(SwingConstants.BOTTOM);
c.add(label2);
c.add(label3);
add(c);
}
public static void main(String args[])
{
Label_Text lt=new Label_Text();
lt.setVisible(true);
}
}
II.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JButton_Test extends JFrame implements ActionListener
{
JButton plainButton,fancyButton;
public JButton_Test()
{
super("JButton Code");
setSize(500,500);
Container c=new Container();
c.setLayout(new FlowLayout());
Object-Oriented Programming
plainButton=new JButton("Plain Button");
c.add(plainButton);
Icon bug1=new ImageIcon("Winter.gif");
Icon bug2=new ImageIcon("Winter.gif");
fancyButton=new JButton("Fancy button",bug1);
fancyButton.setRolloverIcon(bug2);
c.add(fancyButton);
fancyButton.addActionListener(this);
plainButton.addActionListener(this);
add(c);
}
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null,ae.getActionCommand());
}
public static void main(String args[])
{
JButton_Test jbt=new JButton_Test();
jbt.setVisible(true);
jbt.setSize(500,500);
}
}
III.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JComboBoxTest extends JFrame implements ItemListener
{
JComboBox images;
JLabel label;
Object-Oriented Programming
String names[]={"Winter.gif","CIR.gif"};
Icon icons[]={new ImageIcon(names[0]),new ImageIcon(names[1])};
public JComboBoxTest()
{
super("JComboBoxTest");
Container c=new Container();
c.setLayout(new FlowLayout());
images=new JComboBox(names);
images.setMaximumRowCount(1);
images.addItemListener(this);
c.add(images);
add(c);
}
public void ItemStateChanged(ItemEvent ie)
{
label.setIcon(icons[images.getSelectedIndex()]);
}
public static void main(String args[])
{
JComboBoxTest jct=new JComboBoxTest();
jct.setVisible(true);
}
}
Experiment 1.2: Task
Q1) Write a code in Swing that switches from metal look & feel to metal and then to
windows.
Q2) Write a code that shows the implementation of JDesktopPane and JInternalFrame.
Q3) Try to implement a tiny editor by using Swing.
Related documents