Hallo ich fange gerade an mit dem GUI erstellen. Jedoch versteh ich nicht wieso mein Code nicht funktioniert, hab ihn schon mehrfach umgeschrieben. Es sollen die beiden Werte aus den jTextfeldern addiert werden und im 3. Textfeld ausgegeben werden. Berechnung startet via jButton Hier der Code
Danke !
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.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.xml.bind.ParseConversionEvent;
public class NewJFrame extends javax.swing.JFrame {
private JTextField jTextField1;
private JButton jButton1;
private AbstractAction abstractAction1;
private JPanel jPanel1;
private JTextField jTextField3;
private JTextField jTextField2;
public int zahl1=0; //Variable für das Speichern des Inhalts von jTextField1
public int zahl2=0; //Variable für das Speichern des Inhalts von jTextField2
public int ergebnis = zahl1 + zahl2;
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);
{
jPanel1 = new JPanel();
GroupLayout jPanel1Layout = new GroupLayout((JComponent)jPanel1);
jPanel1.setLayout(jPanel1Layout);
{
jTextField3 = new JTextField();
GroupLayout jTextField3Layout = new GroupLayout((JComponent)jTextField3);
jTextField3.setLayout(jTextField3Layout);
jTextField3.setText("Ergebnis"); //Ergebnisausgabefeld
jTextField3Layout.setVerticalGroup(jTextField3Layout.createParallelGroup());
jTextField3Layout.setHorizontalGroup(jTextField3Layout.createParallelGroup());
}
{
jTextField1 = new JTextField();
GroupLayout jTextField1Layout = new GroupLayout((JComponent)jTextField1);
jTextField1.setLayout(jTextField1Layout);
jTextField1.setText("Zahl eingeben");
jTextField1Layout.setVerticalGroup(jTextField1Layout.createParallelGroup());
jTextField1Layout.setHorizontalGroup(jTextField1Layout.createParallelGroup());
zahl1 = Integer.parseInt(jTextField3.getText()); //Wandelt den Inhalt in einen Integer um (berechnung)
}
{
jTextField2 = new JTextField();
GroupLayout jTextField2Layout = new GroupLayout((JComponent)jTextField2);
jTextField2.setLayout(jTextField2Layout);
jTextField2.setText("Zahl eingeben");
jTextField2Layout.setVerticalGroup(jTextField2Layout.createParallelGroup());
jTextField2Layout.setHorizontalGroup(jTextField2Layout.createParallelGroup());
zahl2 = Integer.parseInt(jTextField3.getText()); //Wandelt den Inhalt in einen Integer um (berechnung)
}
{
jButton1 = new JButton();
GroupLayout jButton1Layout = new GroupLayout((JComponent)jButton1);
jButton1.setLayout(jButton1Layout);
jButton1.setText("Berechne");
jButton1.setAction(getAbstractAction1());
jButton1Layout.setHorizontalGroup(jButton1Layout.createParallelGroup());
jButton1Layout.setVerticalGroup(jButton1Layout.createParallelGroup());
}
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup()
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, 228, GroupLayout.PREFERRED_SIZE))
.addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(30)
.addComponent(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(25)
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(92, Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup()
.addGap(8)
.addGroup(jPanel1Layout.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(jButton1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(jTextField3, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
.addContainerGap(86, Short.MAX_VALUE));
}
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap(31, 31)
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
.addContainerGap(81, Short.MAX_VALUE));
thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
.addContainerGap(22, 22)
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 320, GroupLayout.PREFERRED_SIZE)
.addContainerGap(50, Short.MAX_VALUE));
pack();
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private AbstractAction getAbstractAction1() {
if(abstractAction1 == null) {
abstractAction1 = new AbstractAction("abstractAction1", null) {
public void actionPerformed(ActionEvent evt) {
jTextField3.setText(Integer.toString(ergebnis));
}
};
}
return abstractAction1;
}
}
Danke !
Zuletzt bearbeitet: