GUI Aufruf

Panda9296

Bekanntes Mitglied
Hi wie der Name schon sagt, hab ich ein Problem mit dem Gui.. Eigentlich sollte es so sein, wenn ein Feld ausgewählt ist, dass die aufgabe ausgeführt wird und im Label angezeigt wird. Nur irgendwie ruft es die Methode auf sobald ich ein Hacken setzte oder wegmache und das Ergebnis wird nicht sichtbar auf dem Label.

Code:
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import java.awt.Font;

public class GUI extends JFrame {

    private JPanel contentPane;
    private JCheckBox chckbxTaskEight;
    private JCheckBox chckbxTaskFive;
    private JButton btnStart;
    private JCheckBox chckbxTaskOne;
    private JCheckBox chckbxTaskThree;
    private JCheckBox chckbxTaskSix;
    private JCheckBox chckbxTaskFour;
    private JCheckBox chckbxTaskTwo;
    private JButton btnExit;
    private JRadioButton rdbtnTaskBOOK;
    private JRadioButton rdbtnWord;
    private JLabel lblOutput;
    private JCheckBox chckbxTaskNine;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI frame = new GUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 600, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnExit = new JButton("Beenden");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnExit.setBounds(6, 268, 89, 23);
        contentPane.add(btnExit);

        btnStart = new JButton("Start"); // Startbutton
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String qualification[] = new String[9];
                if (rdbtnTaskBOOK.isSelected()) {
                    qualification[0] = rdbtnTaskBOOK.getText();
                }
                if (rdbtnWord.isSelected()) {
                    qualification[1]=rdbtnWord.getText();
                }
               
            }
        });
        btnStart.setBounds(171, 266, 89, 23);
        contentPane.add(btnStart);

        lblOutput = new JLabel("Immer nur eine Aufgabe bitte");
        lblOutput.setFont(new Font("Yu Gothic UI Semilight", Font.PLAIN, 16));
        lblOutput.setBounds(328, 47, 246, 303);
        contentPane.add(lblOutput);

        rdbtnWord = new JRadioButton("Aufgaben Word");
        rdbtnWord.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String qualification[] = new String[9];
                qualification[0]=chckbxTaskOne.getText();
                qualification[1]=chckbxTaskTwo.getText();
                qualification[2]=chckbxTaskThree.getText();
                qualification[3]=chckbxTaskFour.getText();
                qualification[4]=chckbxTaskNine.getText();
                for(int i=0;i<qualification.length;i++) {
                if(chckbxTaskOne.isSelected()&&!chckbxTaskTwo.isSelected()&&!chckbxTaskThree.isSelected()&&!chckbxTaskFour.isSelected()&&!chckbxTaskNine.isSelected()) {
                    lblOutput.setText(chckbxTaskOne.getText());
                }
                if(chckbxTaskTwo.isSelected()&&!chckbxTaskOne.isSelected()&&!chckbxTaskThree.isSelected()&&!chckbxTaskFour.isSelected()&&!chckbxTaskNine.isSelected()) {
                    lblOutput.setText(chckbxTaskTwo.getText());
                }
                if(chckbxTaskThree.isSelected()&&!chckbxTaskOne.isSelected()&&!chckbxTaskTwo.isSelected()&&!chckbxTaskFour.isSelected()&&!chckbxTaskNine.isSelected()){
                    lblOutput.setText(chckbxTaskThree.getText());
                }
                if(chckbxTaskFour.isSelected()&&!chckbxTaskOne.isSelected()&&!chckbxTaskTwo.isSelected()&&!chckbxTaskThree.isSelected()&&!chckbxTaskNine.isSelected()) {
                    lblOutput.setText(chckbxTaskFour.getText());
                }
                if(chckbxTaskNine.isSelected()&&!chckbxTaskOne.isSelected()&&!chckbxTaskTwo.isSelected()&&!chckbxTaskThree.isSelected()&&!chckbxTaskFour.isSelected()) {
                    lblOutput.setText(chckbxTaskNine.getText());
                }
                else {
                    lblOutput.setText("Es darf nur eine Aufgabe ausgewählt sein.");
                }
                }
            }
        });
        rdbtnWord.setBounds(116, 45, 109, 23);
        contentPane.add(rdbtnWord);

        rdbtnTaskBOOK = new JRadioButton("Aufgaben Buch");
        rdbtnTaskBOOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String qualification[] = new String[4];

                qualification[0] = chckbxTaskFour.getText();
                qualification[1] = chckbxTaskFive.getText();
                qualification[2] = chckbxTaskSix.getText();
                qualification[3] = chckbxTaskEight.getText();
                for (int i = 0; i < qualification.length; i++) {
                    if (chckbxTaskFour.isSelected() && !chckbxTaskFive.isSelected() && !chckbxTaskSix.isSelected()
                            && !chckbxTaskEight.isSelected()) {
                        lblOutput.setText(chckbxTaskFour.getText());
                    }
                    if (chckbxTaskFive.isSelected() && !chckbxTaskFour.isSelected() && !chckbxTaskSix.isSelected()
                            && !chckbxTaskEight.isSelected()) {
                        lblOutput.setText(chckbxTaskFive.getText());
                    }
                    if (chckbxTaskSix.isSelected() && !chckbxTaskFour.isSelected() && !chckbxTaskFive.isSelected()
                            && !chckbxTaskEight.isSelected()) {
                        lblOutput.setText(chckbxTaskSix.getText());
                    }
                    if (chckbxTaskEight.isSelected() && !chckbxTaskFour.isSelected() && !chckbxTaskFive.isSelected()
                            && !chckbxTaskSix.isSelected()) {
                        lblOutput.setText(chckbxTaskEight.getText());
                    }else {
                        lblOutput.setText("Es darf nur eine Aufgabe ausgewählt sein.");
                    }

                }

            }
        });
        rdbtnTaskBOOK.setBounds(5, 45, 109, 23);
        contentPane.add(rdbtnTaskBOOK);

        chckbxTaskOne = new JCheckBox("Aufgabe 1");
        chckbxTaskOne.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TaskWord taskWord=new TaskWord();
                taskWord.taskOne();
            }
        });
        chckbxTaskOne.setBounds(5, 69, 97, 23);
        contentPane.add(chckbxTaskOne);
       

        chckbxTaskTwo = new JCheckBox("Aufabe 2");
        chckbxTaskTwo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TaskWord taskWord=new TaskWord();
                taskWord.taskTwo();
            }
        });
        chckbxTaskTwo.setBounds(116, 69, 97, 23);
        contentPane.add(chckbxTaskTwo);
       

        chckbxTaskThree = new JCheckBox("Aufgabe 3");
        chckbxTaskThree.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TaskWord taskWord=new TaskWord();
                taskWord.taskThree();
            }
        });
        chckbxTaskThree.setBounds(5, 95, 97, 23);
        contentPane.add(chckbxTaskThree);

        chckbxTaskFour = new JCheckBox("Aufgabe 4");
        chckbxTaskFour.addActionListener(new ActionListener() {
           
            public void actionPerformed(ActionEvent e) {
                if(rdbtnTaskBOOK.isSelected()) {
                TaskBook taskBook = new TaskBook();
                taskBook.Site220TaskFour();
                }
                if(rdbtnWord.isSelected()) {
                    TaskWord taskWord = new TaskWord();
                    taskWord.taskFour();
                }
            }
        });
        chckbxTaskFour.setBounds(116, 95, 97, 23);
        contentPane.add(chckbxTaskFour);

        chckbxTaskFive = new JCheckBox("Aufgabe 5");
        chckbxTaskFive.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TaskBook taskBook = new TaskBook();
                taskBook.Site220TaskFive();
            }
        });
        chckbxTaskFive.setBounds(5, 124, 97, 23);
        contentPane.add(chckbxTaskFive);

        chckbxTaskSix = new JCheckBox("Aufgabe 6");
        chckbxTaskSix.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TaskBook taskBook = new TaskBook();
                taskBook.Site220TaskSix();
            }
        });
        chckbxTaskSix.setBounds(116, 124, 97, 23);
        contentPane.add(chckbxTaskSix);

        chckbxTaskEight = new JCheckBox("Aufgabe 8");
        chckbxTaskEight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TaskBook taskBook = new TaskBook();
                taskBook.Site226TaskEight();
            }
        });
        chckbxTaskEight.setBounds(5, 150, 97, 23);
        contentPane.add(chckbxTaskEight);

        JLabel lblNewLabel = new JLabel("Aufgaben Grundlagen Java");
        lblNewLabel.setFont(new Font("Stencil", Font.PLAIN, 20));
        lblNewLabel.setBounds(6, 11, 291, 29);
        contentPane.add(lblNewLabel);
       
        chckbxTaskNine = new JCheckBox("Aufgabe 9");
        chckbxTaskNine.setBounds(116, 150, 97, 23);
        contentPane.add(chckbxTaskNine);
        TaskBook taskBook= new TaskBook();
        taskBook.Site226TaskNine();
    }
}
Code:
import java.math.BigInteger;
import java.util.Arrays;

import javax.swing.JOptionPane;

public class TaskBook {

    String Site220TaskFour() {
        String input = JOptionPane.showInputDialog("Buch S.220 Nr 4:Geben Sie bitte eine possitive ganze Zahl ein:");
        try {
            int inputInt = Integer.parseInt(input);
            if (inputInt % 2 == 0) {
                return"Die Zahl " + inputInt + " ist eine gerade Zahl!";
            } else {
                return"Die Zahl " + inputInt + " ist ungerade Zahl!";
            }
        } catch (Exception e) {
            return "Sie haben eine falsche Eingabe getätigt";
        }
    }

    String Site220TaskFive() {
        String inputOne;
        String inputTwo;
        String inputThree;

        int inputOneInt;
        int inputTwoInt;
        int inputThreeInt;

        inputOne = JOptionPane.showInputDialog("Geben Sie bitte Ihre erste Zahl ein:");
        inputTwo = JOptionPane.showInputDialog("Geben Sie bitte Ihre zweite Zahl ein:");
        inputThree = JOptionPane.showInputDialog("Geben Sie bitte Ihre dritte Zahl ein:");
        try {
           
            inputOneInt = Integer.parseInt(inputOne);
            if (inputOneInt != 0) {
                inputTwoInt = Integer.parseInt(inputTwo);
                if (inputTwoInt != 0) {
                    inputThreeInt = Integer.parseInt(inputThree);
                    if (inputThreeInt != 0) {
                       
                        if(inputOneInt>inputTwoInt&&inputOneInt>inputThreeInt) {
                            return"Die Zahl "+inputOneInt+" ist die größte der 3 eingegebenen Zahlen";
                        }else if(inputTwoInt>inputOneInt&&inputTwoInt>inputThreeInt) {
                            return"Die Zahl "+inputTwoInt+" ist die größte der 3 eingegebenen Zahlen";
                        }else {
                            return"Die Zahl "+inputThreeInt+" ist die größte der 3 eingegebenen Zahlen";
                        }

                    } else {
                       
                        inputOne = JOptionPane.showInputDialog("Falsche Eingabe.Geben Sie bitte Ihre erste Zahl ein:");
                        inputThreeInt = Integer.parseInt(inputThree);

                    }
                } else {
                    inputOne = JOptionPane.showInputDialog("Falsche Eingabe.Geben Sie bitte Ihre erste Zahl ein:");
                    inputTwoInt = Integer.parseInt(inputTwo);
                }

            } else {
                System.err.println("Sie haben eine falsche Eingabe getätigt");
                inputOne = JOptionPane.showInputDialog("Geben Sie bitte Ihre erste Zahl ein:");
                inputOneInt = Integer.parseInt(inputOne);
            }

        } catch (Exception e) {
            return"Sie haben eine falsche Eingabe getätigt";
        }
        return "Sie haben eine falsche Eingabe getätigt";

    }

    String Site220TaskSix() {
        String inputYear;
        int inputYearInt;
        inputYear= JOptionPane.showInputDialog("Geben Sie bitte das gewünschte Jahr ein ein:");
        inputYearInt= Integer.parseInt(inputYear);
        if(inputYearInt%4==0) {
            if(inputYearInt%100==0&&inputYearInt%400==0) {
                return"Das Jahr "+inputYearInt+" ist ein Schaltjahr";
            }else {
                if( inputYearInt%100==0&&inputYearInt%400!=0) {
                    return"Das Jahr "+inputYearInt+" ist kein Schaltjahr";
                }
            }return"Das Jahr "+inputYearInt+" ist ein Schaltjahr";
        }return"Das Jahr "+inputYearInt+" ist kein Schaltjahr";
       
       
    }

    String Site226TaskEight() {
       

        final int weight= 17;
        final int halfWeight=(weight-1)/2;
        final int height= halfWeight;
        final int lastLine= height-1;
        for(int i=0;i<height;i++) {
            for(int j=0;j<weight;j++) {
                switch(i) {
                case 0:
                    if((j==0||j==halfWeight||j==weight-1)) {
                        return "*";
                    }
                    else{
                        return " ";
                    }
                case lastLine:
                    return"*";
                    default:
                        if(((j<i||j>halfWeight-i&&j<halfWeight+i)||j>=weight-i)) {
                            return "*";
                        }
                        else{
                            return" ";
                        }
                }
            }return "       ";
        }
        return "Fehler";
           
   
    }
    String Site226TaskNine(){
       
        String input = JOptionPane.showInputDialog("Bitte übergeben Sie Ihre Zahl");
        int inputInt=Integer.parseInt(input);
        BigInteger[] array = new BigInteger[inputInt];
        array[0] = array[1] = BigInteger.ONE;

        for (int i = 2; i < inputInt; i++) {
         array[i] = array[i - 2].add(array[i - 1]);
        }
        return ""+array[inputInt - 3] + " " + array[inputInt - 2]+ // verify sum
        array[inputInt - 1]+"";
    }

}
Code:
import javax.swing.JOptionPane;

public class TaskWord {

    String taskOne() {
        for(int i=1;i<=50;i++) {
            if(!(i%2==0)) {
                return"Die Zahl "+i+" ist ungerade";
            }
           
        }
        return null;
    }
    String taskTwo() {
       
        int i=2;
        int betweenSave=0;
        int numberOfPrime=0;
        boolean prime =true;
        while(i<=100) {
            for(int j =2;j<i-1;j++) {
                if(i%j==0) {
                    prime=false;
                }
            }if(prime&&i>betweenSave) {
                betweenSave=i;
                if(!(i==1)) {
                    numberOfPrime++;
                }
               
            }else {
                prime=true;
               
            }i++;
        }return"Die größte Primzahl von 1-100: "+betweenSave+
        "Die Anzahl der Primzahlen von 1-100 ist: "+(numberOfPrime);
       
    }
    String taskThree() {
       
        int output=0;
        char[]charachter;
   
   
       
        String input=JOptionPane.showInputDialog("Bitte geben Sie eine Zahl ein, von der die Quersumme"
                                                + " gebildet werden soll");
            int number = Integer.parseInt(input);
            while(number>0) {
                output += number%10;
                number/=10;
        }   return"Die Quersumme ist: "+output+"";
       
       
       
       
       
    }
    String taskFour() {
        double capital= 1500.0;
        for (int i=1;i<=3;i++) {
            capital= capital*(1.0+0.35/100);
        }return "Das Kapital beträgt:"+capital;
    }
   
   
}
Code:
import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        GUI gui= new GUI();
        gui.setVisible(true);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
       

    }

}
 
Zuletzt bearbeitet:

M.L.

Top Contributor
Die gute Nachricht: mit IntelliJ und OpenJDK 12 funktioniert das Programm. Die schlechte Nachricht: die GUI ist nicht gerade intuitiv und bei der Verarbeitungslogik (einzelne RadioButtons statt einer RadioGroup, es ist nur eine Eingabe von Zahlen möglich) fehlt wohl auch einiges. Siehe zwei Screenshots anbei.
 

Anhänge

  • GUI_IntelliJ_1.png
    GUI_IntelliJ_1.png
    101,2 KB · Aufrufe: 3
  • GUI_IntelliJ_2.png
    GUI_IntelliJ_2.png
    124,3 KB · Aufrufe: 5

Panda9296

Bekanntes Mitglied
ja es war eigentlich ein Konsolenprogramm^^ Ich wollte es in eine GUI machen. Warum wird den mir das Ergebnis der Methoden nicht auf dem Label angezeigt? Gibt es eine Möglichkeit, wenn der Actionlistener durch ist, das Kästchen wieder zurück zu setzen?
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
Juelin JavaFX Netbeans Aufruf Funktion aus Scenebuilder AWT, Swing, JavaFX & SWT 8
G Gui updated beim zweiten Aufruf nicht mehr AWT, Swing, JavaFX & SWT 15
T SWT: Aufruf des Top-Dialogfensters und sperre des Hintergrundes AWT, Swing, JavaFX & SWT 2
C Swing Aufruf der Funktion (die ein Dialog anzeigt) über Symbol anzeigen lassen AWT, Swing, JavaFX & SWT 4
N Aufruf einer anderen Klasse durch Button ActionListener AWT, Swing, JavaFX & SWT 2
MiMa Übergeben von Paramter bei FXML Aufruf? AWT, Swing, JavaFX & SWT 8
C Nach Aufruf von anderer Klasse streikt Grafik AWT, Swing, JavaFX & SWT 1
D Swing keine JLabel-Aktualisierung bei "externem" Aufruf durch Helferklasse AWT, Swing, JavaFX & SWT 10
D JLabel bei Aufruf neue Zeile AWT, Swing, JavaFX & SWT 5
M Swing Vorgehen beim Aufruf der Klassen/Methoden AWT, Swing, JavaFX & SWT 7
S 2D-Grafik repaint()-Aufruf. Und nichts geschieht. AWT, Swing, JavaFX & SWT 5
F 2D-Grafik Grafikproblem nach Aufruf von JColorChooser-Dialog AWT, Swing, JavaFX & SWT 6
D Swing JOptionPane verschwindet beim 2. Aufruf einfach so?? AWT, Swing, JavaFX & SWT 2
A Swing JFileChooser - Größenänderung nach Aufruf von showOpenDialog() AWT, Swing, JavaFX & SWT 15
Ernesto95 AnimationLoop - Problem bei Aufruf von repaint AWT, Swing, JavaFX & SWT 6
W Funktionen, sowie aufruf einiger Komponenten AWT, Swing, JavaFX & SWT 4
X Swing Panel Extra Klasse & Aufruf über Button AWT, Swing, JavaFX & SWT 18
P JPanel-Aufruf funktioniert nicht AWT, Swing, JavaFX & SWT 5
K Swing setPreferredWidth für JTable funktioniert nur beim ersten Aufruf!? AWT, Swing, JavaFX & SWT 8
P EDT Problem? Kein Aufruf der repaint Methode AWT, Swing, JavaFX & SWT 6
X Alte Frame-Größe beim wiederholten Aufruf AWT, Swing, JavaFX & SWT 5
D Aufruf neuer Klasse ohne Erzeugung eines neuen Fensters AWT, Swing, JavaFX & SWT 3
C Unterschiedlicher Aufruf der paint() Methode ? (AWT/Swing) AWT, Swing, JavaFX & SWT 2
J Swing setLocation Aufruf in JApplets 20 mal langsamer als in JFrames AWT, Swing, JavaFX & SWT 8
C SWT SWT Dialogfenster blitzen bei deren Aufruf AWT, Swing, JavaFX & SWT 2
P seltsame Performance Probleme bei 2 Guis abhängig vom Aufruf AWT, Swing, JavaFX & SWT 8
JFeel-x repaint für Aufruf von paintComponent-Klasse ungeeignet? AWT, Swing, JavaFX & SWT 11
Dragonfire ZoomImage flackert -> Fehler: repaint-Aufruf [gelöst] AWT, Swing, JavaFX & SWT 5
D Container des JFrame zeichnet sich nicht bei Aufruf von paintAll AWT, Swing, JavaFX & SWT 9
S NullPointerException bei Aufruf von updateUI() beim JFrame AWT, Swing, JavaFX & SWT 12
A preferredLayoutSize aufruf erzwingen AWT, Swing, JavaFX & SWT 4
D JTab <--> JPanel aufruf per Button AWT, Swing, JavaFX & SWT 2
G Keine Anzeige von Grafik bei externem paintComponent-Aufruf AWT, Swing, JavaFX & SWT 2
M Problem mit erneutem Aufruf AWT, Swing, JavaFX & SWT 4
K g.DrawImage unter paintComponent klappt nur beim 1. Aufruf AWT, Swing, JavaFX & SWT 3
G ActionListener und Methoden-Aufruf aus anderen Klassen AWT, Swing, JavaFX & SWT 3
G NullPointerException bei Aufruf von JTabbedPane.getSelectedI AWT, Swing, JavaFX & SWT 7
P AWT: Expliziter repaint-Aufruf AWT, Swing, JavaFX & SWT 2
H doppelter Aufruf von selectionListener für JList AWT, Swing, JavaFX & SWT 4
J Aufruf externer Quellen aus java AWT, Swing, JavaFX & SWT 3

Ähnliche Java Themen

Neue Themen


Oben