Programmers Files :-
1. SampleMenu
Ans
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SampleMenu
implements ActionListener, ItemListener {
private JFrame f;
private JMenuBar mb;
private JMenu m1;
private JMenu m2;
private JMenu m3;
private JMenuItem mi1;
private JMenuItem mi2;
private JMenuItem mi3;
private JMenuItem mi4;
private JCheckBoxMenuItem mi5;
public void go() {
f = new JFrame("Menu");
mb = new JMenuBar();
m1 = new JMenu("File");
m2 = new JMenu("Edit");
m3 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
f.setJMenuBar(mb);
mi1 = new JMenuItem("New");
mi2 = new JMenuItem("Save");
mi3 = new JMenuItem("Load");
mi4 = new JMenuItem("Quit");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.addSeparator();
m1.add(mi4);
mi5 = new JCheckBoxMenuItem("Persistent");
mi5.addItemListener(this);
m1.add(mi5);
f.setSize(200,200);
f.setVisible(true);
}
public void actionPerformed( ActionEvent ae) {
System.out.println("Button \"" +
ae.getActionCommand() + "\" pressed.");
if (ae.getActionCommand().equals("Quit")) {
System.exit(0);
}
}
public void itemStateChanged(ItemEvent ie) {
String state = "deselected";
if (ie.getStateChange() == ItemEvent.SELECTED) {
state = "selected";
}
System.out.println(ie.getItem() + " " + state);
}
public static void main (String args[]) {
SampleMenu sampleMenu = new SampleMenu();
sampleMenu.go();
}
}
1. SampleMenu
Ans
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SampleMenu
implements ActionListener, ItemListener {
private JFrame f;
private JMenuBar mb;
private JMenu m1;
private JMenu m2;
private JMenu m3;
private JMenuItem mi1;
private JMenuItem mi2;
private JMenuItem mi3;
private JMenuItem mi4;
private JCheckBoxMenuItem mi5;
public void go() {
f = new JFrame("Menu");
mb = new JMenuBar();
m1 = new JMenu("File");
m2 = new JMenu("Edit");
m3 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
f.setJMenuBar(mb);
mi1 = new JMenuItem("New");
mi2 = new JMenuItem("Save");
mi3 = new JMenuItem("Load");
mi4 = new JMenuItem("Quit");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.addSeparator();
m1.add(mi4);
mi5 = new JCheckBoxMenuItem("Persistent");
mi5.addItemListener(this);
m1.add(mi5);
f.setSize(200,200);
f.setVisible(true);
}
public void actionPerformed( ActionEvent ae) {
System.out.println("Button \"" +
ae.getActionCommand() + "\" pressed.");
if (ae.getActionCommand().equals("Quit")) {
System.exit(0);
}
}
public void itemStateChanged(ItemEvent ie) {
String state = "deselected";
if (ie.getStateChange() == ItemEvent.SELECTED) {
state = "selected";
}
System.out.println(ie.getItem() + " " + state);
}
public static void main (String args[]) {
SampleMenu sampleMenu = new SampleMenu();
sampleMenu.go();
}
}
2. SampleMenu1
Ans
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SampleMenu1 {
private JFrame f;
private JMenuBar mb;
public void go() {
f = new JFrame("MenuBar");
mb = new JMenuBar();
f.setJMenuBar(mb);
f.setSize(200,200);
f.setVisible(true);
}
public static void main (String args[]) {
SampleMenu1 sampleMenu = new SampleMenu1();
sampleMenu.go();
}
}
3. SampleMenu2
Ans
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SampleMenu2 {
private JFrame f;
private JMenuBar mb;
private JMenu m1;
private JMenu m2;
private JMenu m3;
public void go() {
f = new JFrame("Menu");
mb = new JMenuBar();
m1 = new JMenu("File");
m2 = new JMenu("Edit");
m3 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
f.setJMenuBar(mb);
f.setSize(200,200);
f.setVisible(true);
}
public static void main (String args[]) {
SampleMenu2 sampleMenu = new SampleMenu2();
sampleMenu.go();
}
}
4. SampleMenu3
Ans
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SampleMenu3
implements ActionListener, ItemListener {
private JFrame f;
private JMenuBar mb;
private JMenu m1;
private JMenu m2;
private JMenu m3;
private JMenuItem mi1;
private JMenuItem mi2;
private JMenuItem mi3;
private JMenuItem mi4;
public void go() {
f = new JFrame("MenuItem");
mb = new JMenuBar();
m1 = new JMenu("File");
m2 = new JMenu("Edit");
m3 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
f.setJMenuBar(mb);
mi1 = new JMenuItem("New");
mi2 = new JMenuItem("Save");
mi3 = new JMenuItem("Load");
mi4 = new JMenuItem("Quit");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.addSeparator();
m1.add(mi4);
f.setSize(200,200);
f.setVisible(true);
}
public void actionPerformed( ActionEvent ae) {
System.out.println("Button \"" +
ae.getActionCommand() + "\" pressed.");
if (ae.getActionCommand().equals("Quit")) {
System.exit(0);
}
}
public void itemStateChanged(ItemEvent ie) {
String state = "deselected";
if (ie.getStateChange() == ItemEvent.SELECTED) {
state = "selected";
}
System.out.println(ie.getItem() + " " + state);
}
public static void main (String args[]) {
SampleMenu3 sampleMenu = new SampleMenu3();
sampleMenu.go();
}
}
5. SampleMenu4
Ans
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SampleMenu4
implements ActionListener, ItemListener {
private JFrame f;
private JMenuBar mb;
private JMenu m1;
private JMenu m2;
private JMenu m3;
private JMenuItem mi1;
private JMenuItem mi2;
private JMenuItem mi3;
private JMenuItem mi4;
private JCheckBoxMenuItem mi5;
public void go() {
f = new JFrame("CheckboxMenuItem");
mb = new JMenuBar();
m1 = new JMenu("File");
m2 = new JMenu("Edit");
m3 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
mb.add(m3);
f.setJMenuBar(mb);
mi1 = new JMenuItem("New");
mi2 = new JMenuItem("Save");
mi3 = new JMenuItem("Load");
mi4 = new JMenuItem("Quit");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.addSeparator();
m1.add(mi4);
mi5 = new JCheckBoxMenuItem("Persistent");
mi5.addItemListener(this);
m1.add(mi5);
f.setSize(200,200);
f.setVisible(true);
}
public void actionPerformed( ActionEvent ae) {
System.out.println("Button \"" +
ae.getActionCommand() + "\" pressed.");
if (ae.getActionCommand().equals("Quit")) {
System.exit(0);
}
}
public void itemStateChanged(ItemEvent ie) {
String state = "deselected";
if (ie.getStateChange() == ItemEvent.SELECTED) {
state = "selected";
}
System.out.println(ie.getItem() + " " + state);
}
public static void main (String args[]) {
SampleMenu4 sampleMenu = new SampleMenu4();
sampleMenu.go();
}
}
6. TestColor
Ans
import java.awt.*;
public class TestColor {
public static void main(String[] args) {
TestColor tester = new TestColor();
tester.launchFrame();
}
TestColor() {}
private void launchFrame() {
Frame f = new Frame();
Button b = new Button("Purple");
Color purple = new Color(255, 0, 255);
b.setBackground(purple);
f.add(b);
f.pack();
f.setVisible(true);
}
}
7. TestPrinting
Ans
import java.awt.*;
import java.awt.event.*;
public class TestPrinting {
private Frame frame;
private TextArea output;
private TextField input;
public TestPrinting() {
frame = new Frame("Chat Room");
output = new TextArea(10,50);
input = new TextField(50);
}
public void launchFrame() {
Button button;
frame.setLayout(new BorderLayout());
frame.add(output,BorderLayout.WEST);
frame.add(input,BorderLayout.SOUTH);
Panel p1 = new Panel();
// Add Send button
button = new Button("Send");
button.addActionListener(new SendHandler());
p1.add(button);
// Add Print button
button = new Button("Print");
button.addActionListener(new PrintHandler());
p1.add(button);
// Add Quit button
button = new Button("Quit");
// Use an anonymous inner class for variety.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
p1.add(button);
frame.add(p1,BorderLayout.CENTER);
// Attach listener to the appropriate components
frame.addWindowListener(new CloseHandler());
input.addActionListener(new InputHandler());
frame.setSize(440,210);
frame.setVisible(true);
}
private void copyText() {
String text = input.getText();
output.setText(output.getText()+text+"\n");
input.setText("");
}
private class SendHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
copyText();
}
}
private class PrintHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
Toolkit toolkit = frame.getToolkit();
PrintJob job = toolkit.getPrintJob(frame, "Print the Chat Room", null);
Graphics g = job.getGraphics();
frame.print(g);
g.dispose();
job.end();
}
}
private class CloseHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
private class InputHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
copyText();
}
}
public static void main(String[] args) {
TestPrinting c = new TestPrinting();
c.launchFrame();
}
}
No comments:
Post a Comment