Download source codes

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
// Example: taking square of a real number
// Ilustrating control-boundary-entity design
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BoundaryControl {
public static void main(String args[]) {
ControlClass control = new ControlClass();
} // end method main
} // end class BoundaryControl
class ControlClass {
JFrameBoundary boundary;
double value;
public ControlClass() {
boundary = new JFrameBoundary(this);
} // end constructor
public void buttonPressed() {
value = boundary.getValue();
value = Math.sqrt(value) ;
boundary.setResult(value);
} // end method buttonPressed
} // end class ControlClass
class JFrameBoundary extends JFrame {
JTextField enterValue;
JLabel result;
JButton squareRoot;
ControlClass controlObj;
public JFrameBounary(ControlClass ctrlObj) {
controlObj = ctrlObj;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLayout( new FlowLayout());
enterValue = new JTextField(10);
result = new JLabel(“”);
squareRoot = new JButton(“Square Root");
add(enterValue);
add(result);
add(squareRoot);
Handler handler = new Handler();
squareRoot.addActionListener(handler);
setVisible(true);
} // end constructor
private class Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
controlObj.buttonPressed();
} // end method actionPerformed
} // end inner class Handler
public double getValue() {
return Double.parseDouble(enterValue.getText());
} // end method getValue
public void setResult(double x) {
String s = String.format(“square root: %10.3f”,x);
result.setText(s);
} // end method setResult
} // end class
class ControlClass {
JFrameBoundary boundary;
double value;
public ControlClass() {
boundary = new JFrameBoundary(this);
} // end constructor
public void getValue(double x) {
value = x;
value = Math.sqrt(value) ;
boundary.setResult(value);
} // end method buttonPressed
} // end class ControlClass
class JFrameBoundary extends JFrame {
JTextField enterValue;
JLabel result;
JButton squareRoot;
ControlClass controlObj;
public JFrameBounary(ControlClass ctrlObj) {
controlObj = ctrlObj;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLayout( new FlowLayout());
enterValue = new JTextField(10);
result = new JLabel(“”);
squareRoot = new JButton(“Square Root");
add(enterValue);
add(result);
add(squareRoot);
Handler handler = new Handler();
squareRoot.addActionListener(handler);
setVisible(true);
} // end constructor
private class Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
double x =
Double.parseDouble(.getText());
contrlObj.getValue(x);
} // end method actionPerformed
} // end inner class Handler
public void setResult(doube x) {
String s = String.format(“square root: %10.3f”,x);
jTextFieldResult.setText(s);
} // end method setResult
} // end class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BoundaryControl1 {
public static void main(String args[]) {
ControlClass cntrlObj= new ControlClass();
} // end method main
} // end class BoundaryContro
class ControlClass {
private String products[] = {"cheese","salami","tomata","ovel"};
private double prices[] = {10.5,12.25,20.75,15};
JFrameBoundary boundary;
public ControlClass() {
boundary = new JFrameBoundary(this,products);
boundary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boundary.setSize(400, 200);
boundary.setVisible(true);
} // end constructor
public void itemsSelected() {
double total = 5.0;
int i;
int selectedIng[] =
boundary.getSelectedItems();
for(i=0;i<selectedIng.length;i++)
total += prices[selectedIng[i]];
boundary.setResult(total);
} // end method buttonPressed
} // end class ControClass
class JFrameBoundary extends JFrame {
ControlClass controlObj;
private JList list;
private JButton button;
private JLabel totalPrice;
public JFrameBoundary(ControlClass ctrlObj, String[] ingrad) {
controlObj = ctrlObj;
;
setLayout( new FlowLayout());
list = new JList(ingrad);
list.setVisibleRowCount(2);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
button = new JButton("Selected");
totalPrice = new JLabel("Total price:");
add(new JScrollPane(list));
add(button);
add(totalPrice);
Handler handler = new Handler();
button.addActionListener(handler);
} // end constructor
private class Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
controlObj.itemsSelected();
} // end method actionPerformed
} // end inner class Handler
public int[] getSelectedItems() {
return list.getSelectedIndices();
} // end method getSelectedItems
public void setResult(double x) {
String s = String.format("Total price:%10.3f“,x);
totalPrice.setText(s);
} // end method setResult
} // end class JFrameBoundary
class ControlClass {
private String products[] = {"cheese","salami","tomata","ovel"};
private double prices[] = {10.5,12.25,20.75,15};
JFrameBoundary boundary;
public ControlClass() {
boundary = new JFrameBoundary(this,products);
boundary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boundary.setSize(400, 200);
boundary.setVisible(true);
} // end constructor
public void itemsSelected(int selectedIng[]) {
double total = 5.0;
for(int i=0;i<selectedIng.length;i++)
total += prices[selectedIng[i]];
boundary.setResult(total);
} // end method buttonPressed
} // end class ControlClass
class JFrameBoundary extends JFrame {
ControlClass controlObj;
private JList list;
private JButton button;
private JLabel totalPrice;
public JFrameBoundary(ControlClass ctrlObj, String[] ingrad) {
controlObj = ctrlObj;
;
setLayout( new FlowLayout());
list = new JList(ingrad);
list.setVisibleRowCount(2);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
button = new JButton("Selected");
totalPrice = new JLabel("Total price:");
add(new JScrollPane(list));
add(button);
add(totalPrice);
Handler handler = new Handler();
button.addActionListener(handler);
} // end constructor
private class Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
controlObj.itemsSelected(list.getSelectedIndices());
} // end method actionPerformed
} // end inner class Handler
public void setResult(double x) {
String s = String.format("Total price:%10.3f“,x);
totalPrice.setText(s);
} // end method setResult
} // end class JFrameBoundary
Related documents