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: