import javax.swing.*; import java.awt.*; import java.awt.event ...

snottybugbearSoftware and s/w Development

Nov 3, 2013 (3 years and 7 months ago)

74 views

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MidtermFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1;
private JLabel prompt;
private JTextField input;
private JButton aButton;
private JTextArea myArea;
private MidtermFrame anotherFrame;
private MyClass result;

public void actionPerformed(ActionEvent evt){
input.setText(result.toString());
}

public MidtermFrame(){
super("Frame I");
setLayout(new FlowLayout());

prompt = new JLabel("Output");
add(prompt);
myArea = new JTextArea(10, 20);
add(myArea);
setSize(300,200);
setVisible(true);
}

public MidtermFrame(MidtermFrame aFrame){
super("Frame II");
anotherFrame = aFrame;
setLayout(new FlowLayout());
try{
result = new MyClass(0);
anotherFrame.myArea.append("result contains " + result + "\n");
}
catch(Exception e){
System.exit(0);
}
prompt = new JLabel("Input");
add(prompt);
input = new JTextField(10);
add(input);
aButton = new JButton("Press me");
add(aButton);
aButton.addActionListener(this);
input.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent evt){
String inputText = "";
int value;
Addable anObject;
try{
inputText = input.getText();
value = Integer.parseInt(inputText);
anObject = new MyClass(value);
result.add(anObject);
anotherFrame.myArea.append("InputText = " +
inputText + "\n;result contains " +
result+ "\n");
}
catch(NumberFormatException nfe){
anotherFrame.myArea.append("Invalid input"
+ inputText + "\n");
}
catch(Exception e){
anotherFrame.myArea.append(e.getMessage()+ "\n");
}
finally{
input.setText("");
}
}
}
);
setSize(275,200);
setVisible(true);
}

public static void main(String a[]){
MidtermFrame frame1, frame2
;
frame1 = new MidtermFrame();
frame2 = new MidtermFrame(frame1);
}
}
public interface Addable {
public void add(Addable x);

}
public class MyClass implements Addable{
private int value;
public MyClass(int x) throws Exception{
if (x > 9) throw new Exception(x + " is too big");
else {
value = x;
}
}
public void add(Addable x){
MyClass arg;
arg = (MyClass) x;
this.value += arg.value;
if (this.value > 9){
this.value -= 10;
}
}
public String toString(){
return "value = " + value;
}
}
public class Employee {
private String name; // first name of employee
private int salary; // hourly salary of employee
private int employeeNo; // each employee has a unique employee number

public Employee(String name, int salary){
this.name = name;
this.salary = salary;
}
public String toString(){
return (employeeNo + "-" + name + " earns $" + salary);
}
}
import java.awt.FlowLayout;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class MidQuesII
extends JFrame{
private JLabel prompt;
private JTextField inputArea;
private JButton aButton;
private JTextArea outputArea;
private Employee listEmployees[];
private int numEmployees = 0;

private Employee createEmployee(String s)
{
String name;
int age;
StringTokenizer myTokens = new StringTokenizer(s, " ");
name = myTokens.nextToken();
age = Integer.parseInt(myTokens.nextToken());
return new Employee(name, age);
}

public MidQuesII(){
super("Frame II");
setLayout(new FlowLayout());
prompt = new JLabel("Input");
add(prompt);
inputArea = new JTextField(10);
add(inputArea);
inputArea.addActionListener
(
new ActionListener(){
public void actionPerformed(ActionEvent evt){
}
}
);
outputArea = new JTextArea(10, 20);
aButton = new JButton("Press me");
add(aButton);
setSize(275,300);
setVisible(true);
}
public void actionPerformed(ActionEvent evt){
String outputString = "";
inputArea.setText("");
for (int i = 0; i < numEmployees; i++){
outputString += listEmployees[i] + "\n";
}
outputArea.setText(outputString);
}
public static void main(String a[]){
MidQuesII aframe
;
aframe = new MidQuesII();
}
}