Hallo Leute ich hoffe mir kann geholfen werden!
Ich habe gerade mit meiner Ausbildung begonnen und hatte leider keine Vorkenntnisse im Programmieren daher bin ich viel am lesen und lernen. Nun soll ich ich als erste Aufgabe von meinem Ausbilder ein Programm entwickeln, das den Benzinverbrauch auf 100 Km berechnet. Dafür sollte ich Jigloo als GUI verwenden. Ich habe den Code fürs Layout fertig jedoch weiss ich einfach nicht wie das anstellen soll, wenn man zahlen wie zB Liter und Strecke in km in die Textfelder einträgt im 3ten Textfeld das Ergebniss bekommt. Die Formel hierfür lautet ja Benzin*gefahrene KM/100. Ich hoffe mir kann jemand helfen.
Hier der Code
[Java]
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit Cloud Garden (Java Resources) for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame {
private JTextField jTextField1;
private JLabel jLabel2;
private JLabel jLabel3;
private JButton jButton1;
private AbstractAction berechnenAction;
private JLabel jLabel4;
private JLabel jLabel1;
private JTextField jTextField3;
private JTextField jTextField2;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NewJFrame() {
super();
initGUI();
}
private void initGUI() {
try {
GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jTextField1 = new JTextField();
jTextField1.setText("0");
}
{
jTextField2 = new JTextField();
jTextField2.setText("0");
}
{
jTextField3 = new JTextField();
jTextField3.setText ("");
}
{
jLabel1 = new JLabel();
jLabel1.setText("Benzin in Liter");
}
{
jLabel2 = new JLabel();
jLabel2.setText("Strecke in Km");
}
{
jLabel3 = new JLabel();
jLabel3.setText("Verbrauch auf 100 Km");
}
{
jButton1 = new JButton();
jButton1.setText("Berechnen");
jButton1.setAction(getBerechnenAction());
}
{
jLabel4 = new JLabel();
jLabel4.setText("Wieviel verbraucht ihr Auto auf 100 Km ?");
}
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap(59, 59)
.addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(44)
.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(23)
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(58, Short.MAX_VALUE));
thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
.addContainerGap(38, 38)
.addGroup(thisLayout.createParallelGroup()
.addGroup(thisLayout.createSequentialGroup()
.addGroup(thisLayout.createParallelGroup()
.addComponent(jTextField1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(44)
.addGroup(thisLayout.createParallelGroup()
.addComponent(jTextField2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
.addGroup(thisLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(thisLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(39)
.addGroup(thisLayout.createParallelGroup()
.addComponent(jTextField3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
.addGap(59)
.addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(0, 65, Short.MAX_VALUE)))
.addContainerGap(34, 34));
pack();
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private AbstractAction getBerechnenAction() {
if(berechnenAction == null) {
berechnenAction = new AbstractAction("berechnen", null) {
public void actionPerformed(ActionEvent evt) {
}
};
}
return berechnenAction;
}
}
[/code]
Vielen Dank
Ich habe gerade mit meiner Ausbildung begonnen und hatte leider keine Vorkenntnisse im Programmieren daher bin ich viel am lesen und lernen. Nun soll ich ich als erste Aufgabe von meinem Ausbilder ein Programm entwickeln, das den Benzinverbrauch auf 100 Km berechnet. Dafür sollte ich Jigloo als GUI verwenden. Ich habe den Code fürs Layout fertig jedoch weiss ich einfach nicht wie das anstellen soll, wenn man zahlen wie zB Liter und Strecke in km in die Textfelder einträgt im 3ten Textfeld das Ergebniss bekommt. Die Formel hierfür lautet ja Benzin*gefahrene KM/100. Ich hoffe mir kann jemand helfen.
Hier der Code
[Java]
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit Cloud Garden (Java Resources) for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame {
private JTextField jTextField1;
private JLabel jLabel2;
private JLabel jLabel3;
private JButton jButton1;
private AbstractAction berechnenAction;
private JLabel jLabel4;
private JLabel jLabel1;
private JTextField jTextField3;
private JTextField jTextField2;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NewJFrame() {
super();
initGUI();
}
private void initGUI() {
try {
GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jTextField1 = new JTextField();
jTextField1.setText("0");
}
{
jTextField2 = new JTextField();
jTextField2.setText("0");
}
{
jTextField3 = new JTextField();
jTextField3.setText ("");
}
{
jLabel1 = new JLabel();
jLabel1.setText("Benzin in Liter");
}
{
jLabel2 = new JLabel();
jLabel2.setText("Strecke in Km");
}
{
jLabel3 = new JLabel();
jLabel3.setText("Verbrauch auf 100 Km");
}
{
jButton1 = new JButton();
jButton1.setText("Berechnen");
jButton1.setAction(getBerechnenAction());
}
{
jLabel4 = new JLabel();
jLabel4.setText("Wieviel verbraucht ihr Auto auf 100 Km ?");
}
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap(59, 59)
.addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(44)
.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(23)
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(58, Short.MAX_VALUE));
thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
.addContainerGap(38, 38)
.addGroup(thisLayout.createParallelGroup()
.addGroup(thisLayout.createSequentialGroup()
.addGroup(thisLayout.createParallelGroup()
.addComponent(jTextField1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(44)
.addGroup(thisLayout.createParallelGroup()
.addComponent(jTextField2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
.addGroup(thisLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(thisLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(39)
.addGroup(thisLayout.createParallelGroup()
.addComponent(jTextField3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
.addGap(59)
.addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(0, 65, Short.MAX_VALUE)))
.addContainerGap(34, 34));
pack();
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private AbstractAction getBerechnenAction() {
if(berechnenAction == null) {
berechnenAction = new AbstractAction("berechnen", null) {
public void actionPerformed(ActionEvent evt) {
}
};
}
return berechnenAction;
}
}
[/code]
Vielen Dank
Zuletzt bearbeitet von einem Moderator: