You are on page 1of 7

9.

Write a Program Using Swing to


demonstrate creation of file edit menu
with submenu?
package swingnew;

import javax.swing.*;

import java.awt.event.*;

public class SwingNew implements ActionListener{

JFrame f;

JMenuBar mb;

JMenu file,edit;

JMenuItem cut,copy,paste,selectAll;

JTextArea ta;

SwingNew(){

f=new JFrame();

cut=new JMenuItem("cut");

copy=new JMenuItem("copy");

paste=new JMenuItem("paste");

selectAll=new JMenuItem("selectAll");

cut.addActionListener(this);

copy.addActionListener(this);

paste.addActionListener(this);

selectAll.addActionListener(this);

mb=new JMenuBar();

mb.setBounds(5,5,600,20);

file=new JMenu("File");

edit=new JMenu("Edit");

edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);

mb.add(file);mb.add(edit);

ta=new JTextArea();

ta.setBounds(5,30,460,460);

f.add(mb);f.add(ta);
f.setLayout(null);

f.setSize(500,500);

f.setVisible(true);

public void actionPerformed(ActionEvent e) {

if(e.getSource()==cut)

ta.cut();

if(e.getSource()==paste)

ta.paste();

if(e.getSource()==copy)

ta.copy();

if(e.getSource()==selectAll)

ta.selectAll();

public static void main(String[] args) {

new SwingNew();

}
13. Write a Program to implement a
swing program to demonstrate JTree
with atleast 3 tree node and sub nodes?

import javax.swing.*;

import javax.swing.tree.DefaultMutableTreeNode;

public class jtree {

JFrame f;

jtree(){

f=new JFrame();

DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");

DefaultMutableTreeNode color=new DefaultMutableTreeNode("color");

DefaultMutableTreeNode font=new DefaultMutableTreeNode("font");

style.add(color);

DefaultMutableTreeNode red=new DefaultMutableTreeNode("red");

DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");

DefaultMutableTreeNode black=new DefaultMutableTreeNode("black");

DefaultMutableTreeNode green=new DefaultMutableTreeNode("green");

color.add(red); color.add(blue); color.add(black); color.add(green);

DefaultMutableTreeNode shape=new DefaultMutableTreeNode("shape");

style.add(shape);

DefaultMutableTreeNode circle=new DefaultMutableTreeNode("circle");

DefaultMutableTreeNode triangle=new DefaultMutableTreeNode("triangle");

DefaultMutableTreeNode square=new DefaultMutableTreeNode("square");

DefaultMutableTreeNode rectangle=new DefaultMutableTreeNode("rectangle");

shape.add(circle); shape.add(triangle); shape.add(square);


shape.add(rectangle);
DefaultMutableTreeNode size=new DefaultMutableTreeNode("size");

style.add(size);

DefaultMutableTreeNode small=new DefaultMutableTreeNode("small");

DefaultMutableTreeNode normal=new DefaultMutableTreeNode("normal");

DefaultMutableTreeNode medium=new DefaultMutableTreeNode("medium");

DefaultMutableTreeNode large=new DefaultMutableTreeNode("large");

size.add(small); size.add(normal); size.add(medium); size.add(large);

JTree jt=new JTree(style);

f.add(jt);

f.setSize(200,200);

f.setVisible(true);

public static void main(String[] args) {

new jtree();

}}
19. Write a Program to implement swing
program to demonstrate the use of JColor
chooser?
import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class ChooseColor extends JFrame implements ActionListener {

JButton b;

Container c;

ChooseColor(){

c=getContentPane();

c.setLayout(new FlowLayout());

b=new JButton("color");

b.addActionListener(this);

c.add(b);

public void actionPerformed(ActionEvent e) {

Color initialcolor=Color.RED;

Color color=JColorChooser.showDialog(this,"Select a color",initialcolor);

c.setBackground(color);

public static void main(String[] args) {

ChooseColor ch=new ChooseColor();

ch.setSize(400,400);

ch.setVisible(true);

ch.setDefaultCloseOperation(EXIT_ON_CLOSE);

You might also like