Hello,
I have the code below done in JBuilder for a calculator. Unlike the previous code I've posted which uses an MVC structure, this one does not. For instance while creating a button, it is "Button button1= new Button(); " instead of "pKey.add(new Button("C")); "(refer to previous post where the code's written by T.Yamazaki). I very much would like to create my code in JBuilder using the same structure in T.Yamazakii's code, where each key that is clicked is handled through its event handlers. How do I overcome this problem? Any suggestion is welcomed.
<code>
package assign1;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
TextField textField1 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
Button button4 = new Button();
Button button5 = new Button();
Button button6 = new Button();
Button button7 = new Button();
Button button8 = new Button();
Button button9 = new Button();
Button button10 = new Button();
Button button11 = new Button();
Button button12 = new Button();
Button button13 = new Button();
Button button14 = new Button();
Button button15 = new Button();
Button button16 = new Button();
//selfmade attributes
int number1, number2,number3;
String operator;
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(xYLayout1);
setSize(new Dimension(400, 300));
setTitle("Calculator");
textField1.setText("");
button1.setLabel("1");
button1.addActionListener(new Frame1_button1_actionAdapter(this));
button2.setLabel("+");
button2.addActionListener(new Frame1_button2_actionAdapter(this));
button3.setLabel("=");
button3.addActionListener(new Frame1_button3_actionAdapter(this));
button4.setLabel("4");
button4.addActionListener(new Frame1_button4_actionAdapter(this));
button5.setLabel("2");
button5.addActionListener(new Frame1_button5_actionAdapter(this));
button6.setLabel("3");
button6.addActionListener(new Frame1_button6_actionAdapter(this));
button7.setLabel("5");
button7.addActionListener(new Frame1_button7_actionAdapter(this));
button8.setLabel("6");
button8.addActionListener(new Frame1_button8_actionAdapter(this));
button9.setLabel("7");
button9.addActionListener(new Frame1_button9_actionAdapter(this));
button10.setLabel("8");
button10.addActionListener(new Frame1_button10_actionAdapter(this));
button11.setLabel("9");
button11.addActionListener(new Frame1_button11_actionAdapter(this));
button12.setLabel("/");
button13.setLabel("*");
button14.setLabel("-");
button15.setLabel("0");
button15.addActionListener(new Frame1_button15_actionAdapter(this));
button16.setLabel(".");
contentPane.add(textField1, new XYConstraints(14, 19, 366, 54));
contentPane.add(button9, new XYConstraints(179, 84, 41, 28));
contentPane.add(button10, new XYConstraints(227, 85, 47, 28));
contentPane.add(button8, new XYConstraints(227, 118, 48, 33));
contentPane.add(button7, new XYConstraints(179, 118, 42, 34));
contentPane.add(button1, new XYConstraints(179, 157, 46, 33));
contentPane.add(button14, new XYConstraints(335, 155, 45, 33));
contentPane.add(button15, new XYConstraints(180, 194, 44, 35));
contentPane.add(button6, new XYConstraints(279, 156, 49, 34));
contentPane.add(button4, new XYConstraints(280, 119, 47, 34));
contentPane.add(button11, new XYConstraints(279, 84, 50, 30));
contentPane.add(button13, new XYConstraints(335, 119, 44, 31));
contentPane.add(button12, new XYConstraints(335, 86, 43, 29));
contentPane.add(button5, new XYConstraints(229, 157, 45, 34));
contentPane.add(button2, new XYConstraints(279, 194, 47, 33));
contentPane.add(button3, new XYConstraints(336, 192, 43, 35));
contentPane.add(button16, new XYConstraints(235, 198, 40, 30));
}
public void button1_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"1");
}
public void button5_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"2");
}
public void button6_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"3");
}
public void button4_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"4");
}
public void button7_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"5");
}
public void button8_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"6");
}
public void button9_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"7");
}
public void button10_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"8");
}
public void button11_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"9");
}
public void button15_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
textField1.setText(help+"0");
}
public void button2_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
number1 = Integer.parseInt(help);
textField1.setText("");
operator = "+";
}
public void button3_actionPerformed(ActionEvent e) {
String help;
help = textField1.getText();
number2 = Integer.parseInt(help);
String si = String.valueOf(number1+number2);
textField1.setText(si);
}
class Frame1_button3_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button3_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button3_actionPerformed(e);
}
}
class Frame1_button2_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button2_actionPerformed(e);
}
}
class Frame1_button15_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button15_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button15_actionPerformed(e);
}
}
class Frame1_button11_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button11_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button11_actionPerformed(e);
}
}
class Frame1_button10_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button10_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button10_actionPerformed(e);
}
}
class Frame1_button9_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button9_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button9_actionPerformed(e);
}
}
class Frame1_button8_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button8_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button8_actionPerformed(e);
}
}
class Frame1_button7_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button7_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button7_actionPerformed(e);
}
}
class Frame1_button4_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button4_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button4_actionPerformed(e);
}
}
class Frame1_button6_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button6_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button6_actionPerformed(e);
}
}
class Frame1_button5_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button5_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button5_actionPerformed(e);
}
}
class Frame1_button1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_button1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}
}
</code>
Thank you.