Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

SWING Applikation actionPerformed Problem

Empfohlene Antworten

Hallo!

Habe schon lange nichts mehr mit Java gemacht, habe es aber nun im Studium aufgebrummt bekommen und komme bei dieser Swing Applikation einfach nicht weiter:


import javax.swing.JFrame;

import javax.swing.JLabel;

import java.awt.*;

import javax.swing.JPanel;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.JButton;

import javax.swing.JOptionPane;

import javax.swing.JComboBox;

import javax.swing.JTextField;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import java.util.Vector;



public class Fenster extends JFrame implements ActionListener {

    Vector al = new Vector();

    JLabel label = new JLabel();

    JButton button = new JButton();

    JButton info = new JButton();

    JButton sort_button = new JButton();

    JButton shuffle = new JButton();

    JComboBox students = new JComboBox();

    JTextField name_text = new JTextField();

    JTextField perskz_text = new JTextField();

    JButton add_button = new JButton();


    public Fenster() {

        try {

            jbInit();

        } catch (Exception ex) {

            ex.printStackTrace();

        }


        try {

            jbInit();

        } catch (Exception exception) {

            exception.printStackTrace();

        }

    }


    private void jbInit() throws Exception {

        getContentPane().setLayout(null);

        label.setFont(new java.awt.Font("Comic Sans MS", Font.PLAIN, 16));

        label.setForeground(Color.blue);

        label.setToolTipText("HI");

        label.setText("My First SWING Application");

        label.setBounds(new Rectangle(30, 23, 232, 30));

        button.setBounds(new Rectangle(18, 269, 110, 25));

        button.setActionCommand("exit_button");

        button.setText("Exit");

        info.setBounds(new Rectangle(161, 270, 110, 25));

        info.setToolTipText("");

        info.setActionCommand("info_button");

        info.setText("Information");

        sort_button.setBounds(new Rectangle(20, 208, 110, 25));

        sort_button.setActionCommand("sort_button");

        sort_button.setText("Sort List");

        shuffle.setBounds(new Rectangle(161, 209, 111, 25));

        shuffle.setActionCommand("shuffle_button");

        shuffle.setText("Shuffle List");

        students.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 10));

        students.setActionCommand("studentscomboBoxChanged");

        students.setBounds(new Rectangle(7, 84, 276, 26));

        name_text.setText("Insert Name here");

        name_text.setBounds(new Rectangle(32, 121, 158, 25));

        perskz_text.setText("Insert Perskz here");

        perskz_text.setBounds(new Rectangle(32, 154, 157, 25));

        add_button.setBounds(new Rectangle(198, 141, 68, 22));

        add_button.setToolTipText("");

        add_button.setActionCommand("add_button");

        add_button.setText("Add");


        button.addActionListener(this);

        info.addActionListener(this);

        sort_button.addActionListener(this);

        shuffle.addActionListener(this);

        students.addActionListener(this);

        add_button.addActionListener(this);


        this.getContentPane().add(sort_button,null);

        this.getContentPane().add(shuffle,null);

        this.getContentPane().add(students,null);

        this.getContentPane().add(name_text,null);

        this.getContentPane().add(perskz_text,null);

        this.getContentPane().add(add_button,null);

        this.getContentPane().add(button,null);

        this.getContentPane().add(info,null);

        this.getContentPane().add(label,null);

    }


    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand().equalsIgnoreCase("exit_button")) {

            JOptionPane.showMessageDialog(this, "EXIT BUTTON PRESSED");

          //  System.exit(0);

        }

       if (e.getActionCommand().equalsIgnoreCase("info_button")) {

            JOptionPane.showMessageDialog(this, "JAVA Übungsbeispiel");

        }

        if (e.getActionCommand().equalsIgnoreCase("add_button")) {

            System.out.println("ADD PRESSED");

        }

        if (e.getActionCommand().equalsIgnoreCase("shuffle_button")) {

            System.out.println("Shuffling List");

        }

    }


    public static void main(String[] args) {

    Fenster f1 = new Fenster();

    f1.setSize(300, 350);

    f1.setTitle("SWING Window");

    f1.setResizable(true);

    f1.setVisible(true);

    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    f1.setLocation((screen.width - f1.getSize().width) / 2,

                   (screen.height - f1.getSize().height) / 2);

}

}

Das Problem: Bei mir wird die action performed methode immer doppelt aufgerufen!

Wenn ich den ADD Button drücke, dann ist die ausgabe:

ADD PRESSED

ADD PRESSED

Diese Ausgabe sollte aber bei einfachen Klick nur einmal kommen!!!

Arbeite mit dem JBuilder und Java 1.4

mfg

Hi!

Mach deine actionPerformed()-Methode mal folgendermaßen:

public void actionPerformed(ActionEvent e) {

	if (e.getActionCommand().equalsIgnoreCase("exit_button")) {

		JOptionPane.showMessageDialog(this, "EXIT BUTTON PRESSED");

		//  System.exit(0);

	}

	[B][I]else [/I][/B]if (e.getActionCommand().equalsIgnoreCase("info_button")) {

		JOptionPane.showMessageDialog(this, "JAVA Übungsbeispiel");

	}

	[B][I]else [/I][/B]if (e.getActionCommand().equalsIgnoreCase("add_button")) {

		System.out.println("ADD PRESSED");

	}

	[B][I]else [/I][/B]if (e.getActionCommand().equalsIgnoreCase("shuffle_button")) {

		System.out.println("Shuffling List");

	}

}
Tipp: Du kannst übrigens auch auf die Verwendung von ActionCommands verzichten, wenn du willst. Das machst du zum Beispiel wie folgt:
public void actionPerformed(ActionEvent e) {

	if (e.getSource() == sort_button ) {

		//  ...

	}

	if (e.getSource() == add_button ) {

		//  ...

	}

	// etc. ...

}

Die Methode getSource() des ActionEvent's liefert dir (wie der Methodenname schon sagt) das Quellobjekt bzw. den Auslöser des Events zurück. Das kannst du direkt mit deiner Objektreferenz vergleichen.

Aber das nur nebenbei...

Ach ja, es reicht übrigens vollkommen aus, wenn du die Methode jbInit() nur einmal im Konstruktor aufrufst... ;)

Danke sehr - Habe das glatt übersehen, dass ich die init 2 mal aufrufe.....

mfg

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.