1.8.3 Swing Rechner
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPanel; public class SwingRechner extends javax.swing.JFrame { private javax.swing.ButtonGroup buttonGroup1; private javax.swing.JPanel jPanel1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextFieldOut; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private void initComponents() { jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jTextFieldOut = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Swing Rechner"); jTextField1.setText("0"); jTextField1.setColumns(6); jTextField2.setText("0"); jTextField2.setColumns(6); jTextFieldOut.setText("0"); jTextFieldOut.setEditable(false); jButton1.setText("+"); jButton2.setText("-"); jButton3.setText("*"); JPanel radioPanel = new JPanel(new GridLayout(1, 0)); radioPanel.add(jButton1); radioPanel.add(jButton2); radioPanel.add(jButton3); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource() == jButton1) jTextFieldOut.setText( executeOperation1(jTextField1.getText(), jTextField2.getText())); } } ); jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource() == jButton2) jTextFieldOut.setText( executeOperation2(jTextField1.getText(), jTextField2.getText())); } } ); jButton3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(e.getSource() == jButton3) jTextFieldOut.setText( executeOperation3(jTextField1.getText(), jTextField2.getText())); } } ); this.setBounds(300, 300, 200, 30); setMinimumSize(new Dimension(200,30)); getContentPane().add(jTextField1, BorderLayout.WEST); getContentPane().add(jTextField2, BorderLayout.EAST); getContentPane().add(radioPanel, BorderLayout.NORTH); getContentPane().add(jTextFieldOut, BorderLayout.SOUTH); pack(); } public SwingRechner() { initComponents(); } public static void main(String[] args) { SwingRechner f1 = new SwingRechner(); f1.setVisible(true); } public String executeOperation1(String s1,String s2) { int op1= Integer.parseInt(s1); int op2= Integer.parseInt(s2); // Add Application logic here: int resultInt = op1+op2; return (Integer.toString(resultInt)) ; } public String executeOperation2(String s1,String s2) { int op1= Integer.parseInt(s1); int op2= Integer.parseInt(s2); // Add Application logic here: int resultInt = op1-op2; return (Integer.toString(resultInt)) ; } public String executeOperation3(String s1,String s2) { int op1= Integer.parseInt(s1); int op2= Integer.parseInt(s2); int resultInt = op1*op2; return (Integer.toString(resultInt)) ; } }
Beispiel
1.7.8 Airline Block1
Klasse Flugzeug
package AirlineSolution.Block1;
/**
*
* @author stsch
*/
public class Flugzeug {
String kennzeichen;
}
Klasse Flughafen
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package AirlineSolution.Block1;
/**
*
* @author stsch
*/
public class Flughafen {
String name;
Flugzeug gate1;
Flugzeug gate2;
Flugzeug gate3;
Flugzeug gate4;
Flugzeug gate5;
Flugzeug gate6;
double treibstoffLager;
public static void main(String[] args) {
Flughafen pad = new Flughafen();
pad.name="Paderborn";
pad.treibstoffLager = 1000000;
System.out.println("*** Unser Flughafen ***");
System.out.println("Flughafen " + pad.name);
System.out.println("Am Gate 1: " + pad.gate1);
System.out.println("Am Gate 2: " + pad.gate2);
System.out.println("Am Gate 3: " + pad.gate3);
System.out.println("Treibstoff: " + pad.treibstoffLager);
System.out.println("***********************");
// Boeing 747, https://de.wikipedia.org/wiki/Boeing_747#747-400
Flugzeug lh1 = new Flugzeug();
lh1.kennzeichen ="D-ABTL";
pad.gate1 = lh1;
// Airbus A380 https://de.wikipedia.org/wiki/Airbus_A380#A380-800
Flugzeug lh2 = new Flugzeug();
lh2.kennzeichen = "D-AIMA";
pad.gate2 = lh2;
System.out.println("*** Unser Flughafen ***");
System.out.println("Flughafen " + pad.name);
System.out.println("Am Gate 1: " + pad.gate1.kennzeichen);
System.out.println("Am Gate 2: " + pad.gate2.kennzeichen);
System.out.println("Am Gate 3: " + pad.gate3);
System.out.println("Treibstoff: " + pad.treibstoffLager);
System.out.println("***********************");
// Hänge Flugzeug um. mover bewegt Flugzeug
// von Gate 1 nach Gate 3
Flugzeug mover = pad.gate1;
pad.gate1=null;
System.out.println("*** Unser Flughafen ***");
System.out.println("Flughafen " + pad.name);
System.out.println("Am Gate 1: " + pad.gate1);
System.out.println("Am Gate 2: " + pad.gate2.kennzeichen);
System.out.println("Am Gate 3: " + pad.gate3);
System.out.println("Treibstoff: " + pad.treibstoffLager);
System.out.println("***********************");
pad.gate3= mover;
mover=null;
System.out.println("*** Unser Flughafen ***");
System.out.println("Flughafen " + pad.name);
System.out.println("Am Gate 1: " + pad.gate1);
System.out.println("Am Gate 2: " + pad.gate2.kennzeichen);
System.out.println("Am Gate 3: " + pad.gate3.kennzeichen);
System.out.println("Treibstoff: " + pad.treibstoffLager);
System.out.println("***********************");
}
}
- Printer-friendly version
- Log in to post comments
- 5722 views