GUI-Problem, finde meinen Fehler nicht!

Zitrus

Bekanntes Mitglied
Hallo zusammen,
Bin relativ neu in der Java Szene ca. 1 Woche und wollte eine GUI programmieren (Um genuaer zu sein einen Taschenrechner). Aber irgendwie komm ich beim Design schon durcheinander ich hoffe ihr könnt mir helfen 🙂

Code:
super("Rechner");
        Container c = getContentPane();
        setSize(250,240);
        setLocation(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        
        panel = new JPanel();
        panel1 = new JPanel();
        c.add(panel);
        c.add(panel1);
        c.setLayout(null);
        ta = new JTextArea();
        ta.setBounds(0,0,220,50);
        ta.setEditable(false);

        button1 = new JButton ("1");
        button1.setBounds(10,10,40,25);
        button1.setFont(new Font("Arial", 10, 10));
        button2 = new JButton ("2");
        button2.setBounds(10,10,40,25);
        button2.setFont(new Font("Arial", 10, 10));
        button3 = new JButton ("3");
        button3.setBounds(10,10,40,25);
        button3.setFont(new Font("Arial",10, 10));
        button4 = new JButton ("4");
        button4.setBounds(10,10,40,25);
        button4.setFont(new Font("Arial", 10, 10));
        button5 = new JButton ("5");
        button5.setBounds(10,10,40,25);
        button5.setFont(new Font("Arial", 10, 10));
        button6 = new JButton ("6");
        button6.setBounds(10,10,40,25);
        button6.setFont(new Font("Arial", 10, 10));
        button7 = new JButton ("7");
        button7.setBounds(10,10,40,25);
        button7.setFont(new Font("Arial", 10, 10));
        button8 = new JButton ("8");
        button8.setBounds(10,10,40,25);
        button8.setFont(new Font("Arial", 10, 10));
        button9 = new JButton ("9");
        button9.setBounds(10,10,40,25);
        button9.setFont(new Font("Arial", 10, 10));
        button10 = new JButton ("+");
        button10.setBounds(10,10,40,25);
        button10.setFont(new Font("Arial", 10, 10));
        button11 = new JButton ("-");
        button11.setBounds(10,10,40,25);
        button11.setFont(new Font("Arial", 10, 10));
        button12 = new JButton ("x");
        button12.setBounds(10,10,40,25);
        button12.setFont(new Font("Arial", 10, 10));
        button13 = new JButton ("=");
        button13.setBounds(10,10,40,100);
        button13.setFont(new Font("Arial", 10, 10));
        
        panel1.add(ta);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button10);
        panel.add(button13);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(button11);
        panel.add(button7);
        panel.add(button8);
        panel.add(button9);
        panel.add(button12);
        
        setVisible(true);

Ich habe das layout mit setlayout auf null gesetzt. Jedoch passiert nun rein garnichts wenn ich das Programm starte.. Hilfe!
 
Zuletzt bearbeitet:
Ich kann dir deinen Fehler schon aufzeigen:

Man verwendet LayoutManager und kein null-Layout!

Vor allem aber erbt man nicht von JFrame sondern verwendet diesen nur (außer man verändert die Funktionalität dessen)!

Wenn du mehr Hilfe erwartest, dann poste auch mehr Code.
 
Zuletzt bearbeitet:
Ich habe zuerst einen Layout Manager benutzt, aber dann kann ich ja kein setBounds mehr benutzen..
Trotzdem schonmal Vielen Dank für deine Antwort! 🙂
Und hier nochmal der ganze Code:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class FirstGUI extends JFrame
{
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton button10;
    private JButton button11;
    private JButton button12;
    private JButton button13;
    private JPanel panel;
    private JPanel panel1;
    private JTextArea ta;
    
    public FirstGUI()
    {
        super("Rechner");
        Container c = getContentPane();
        setSize(250,240);
        setLocation(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        c.setLayout(null);
        
        panel = new JPanel(new BorderLayout());
        panel1 = new JPanel(new BorderLayout());
        ta = new JTextArea();
        ta.setBounds(0,0,220,50);
        ta.setEditable(false);

        button1 = new JButton ("1");
        button1.setBounds(10,10,40,25);
        button1.setFont(new Font("Arial", 10, 10));
        button2 = new JButton ("2");
        button2.setBounds(10,10,40,25);
        button2.setFont(new Font("Arial", 10, 10));
        button3 = new JButton ("3");
        button3.setBounds(10,10,40,25);
        button3.setFont(new Font("Arial",10, 10));
        button4 = new JButton ("4");
        button4.setBounds(10,10,40,25);
        button4.setFont(new Font("Arial", 10, 10));
        button5 = new JButton ("5");
        button5.setBounds(10,10,40,25);
        button5.setFont(new Font("Arial", 10, 10));
        button6 = new JButton ("6");
        button6.setBounds(10,10,40,25);
        button6.setFont(new Font("Arial", 10, 10));
        button7 = new JButton ("7");
        button7.setBounds(10,10,40,25);
        button7.setFont(new Font("Arial", 10, 10));
        button8 = new JButton ("8");
        button8.setBounds(10,10,40,25);
        button8.setFont(new Font("Arial", 10, 10));
        button9 = new JButton ("9");
        button9.setBounds(10,10,40,25);
        button9.setFont(new Font("Arial", 10, 10));
        button10 = new JButton ("+");
        button10.setBounds(10,10,40,25);
        button10.setFont(new Font("Arial", 10, 10));
        button11 = new JButton ("-");
        button11.setBounds(10,10,40,25);
        button11.setFont(new Font("Arial", 10, 10));
        button12 = new JButton ("x");
        button12.setBounds(10,10,40,25);
        button12.setFont(new Font("Arial", 10, 10));
        button13 = new JButton ("=");
        button13.setBounds(10,10,40,100);
        button13.setFont(new Font("Arial", 10, 10));
        
        panel1.add(ta);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button10);
        panel.add(button13);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(button11);
        panel.add(button7);
        panel.add(button8);
        panel.add(button9);
        panel.add(button12);
        
        c.add(panel);
        c.add(panel1);
        
        setVisible(true);
       
    }
    
    public static void main (String[] args)
    {
        FirstGUI g = new FirstGUI();
    }
    
}
 
Auch wenn du von JFrame erbst, musst du die Größe etc. festlegen
Denn extends JFrame besagt, dass deine Klasse ein JFrame ist.

Aber trotzdem hast du keine Größe gesetzt und andere Einstellungen vorgenommen
wie:
Java:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // besagt,wax passiert wenn du das x drückst
setSize(1024,786); // setzt die Größe in Pixel
setVisible(true); // IMMER zuletzt
oder so ähnlich
 
Zuletzt bearbeitet:
Hab ich alles Drei gemacht 🙂
setSize(250,240);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
 
Sorry hab ich über sehen. Verwende lieber Java tags.

Habs mal getestet und bei mir wirds angezeigt..

kannst natürlich auch das Layout null setzen und mit den Bounds ganz genau so hinbasteln wie es
aussehen soll...
deine Buttons liegen nämlich laut Bounds alle auf der gleichen Stelle (10,10,x,y)

gruß

P.S. die Bounds der Panels haben gefehlt

Java:
package Programm;
       import javax.swing.*;

import java.awt.*;
import java.awt.event.*;


public class FirstGUI extends JFrame
{
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton button10;
    private JButton button11;
    private JButton button12;
    private JButton button13;
    private JPanel panel;
    private JPanel panel1;
    private JTextArea ta;
    
    public FirstGUI()
    {
        super("Rechner");
        
        setLayout(null);
        setSize(300,200);
        setLocation(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        
        panel = new JPanel(new FlowLayout());
        panel.setSize(300, 200);
        panel1 = new JPanel(new FlowLayout());
        panel1.setSize(300, 200);
        
        ta = new JTextArea();
        ta.setBounds(0,0,220,50);
        ta.setEditable(false);

        button1 = new JButton ("1");
        button1.setBounds(10,10,40,25);
        button1.setFont(new Font("Arial", 10, 10));
        button2 = new JButton ("2");
        button2.setBounds(10,10,40,25);
        button2.setFont(new Font("Arial", 10, 10));
        button3 = new JButton ("3");
        button3.setBounds(10,10,40,25);
        button3.setFont(new Font("Arial",10, 10));
        button4 = new JButton ("4");
        button4.setBounds(10,10,40,25);
        button4.setFont(new Font("Arial", 10, 10));
        button5 = new JButton ("5");
        button5.setBounds(10,10,40,25);
        button5.setFont(new Font("Arial", 10, 10));
        button6 = new JButton ("6");
        button6.setBounds(10,10,40,25);
        button6.setFont(new Font("Arial", 10, 10));
        button7 = new JButton ("7");
        button7.setBounds(10,10,40,25);
        button7.setFont(new Font("Arial", 10, 10));
        button8 = new JButton ("8");
        button8.setBounds(10,10,40,25);
        button8.setFont(new Font("Arial", 10, 10));
        button9 = new JButton ("9");
        button9.setBounds(10,10,40,25);
        button9.setFont(new Font("Arial", 10, 10));
        button10 = new JButton ("+");
        button10.setBounds(10,10,40,25);
        button10.setFont(new Font("Arial", 10, 10));
        button11 = new JButton ("-");
        button11.setBounds(10,10,40,25);
        button11.setFont(new Font("Arial", 10, 10));
        button12 = new JButton ("x");
        button12.setBounds(10,10,40,25);
        button12.setFont(new Font("Arial", 10, 10));
        button13 = new JButton ("=");
        button13.setBounds(10,10,40,100);
        button13.setFont(new Font("Arial", 10, 10));
        
       
        panel1.add(ta);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button10);
        panel.add(button13);
        panel.add(button4);
        panel.add(button5);
        panel.add(button6);
        panel.add(button11);
        panel.add(button7);
        panel.add(button8);
        panel.add(button9);
        panel.add(button12);
        
        add(panel);
        add(panel1);
        
        setVisible(true);
       
    }
    
    public static void main (String[] args)
    {
        new FirstGUI();
    }
    
}
 
Zuletzt bearbeitet:
Vielen Herzlichen Dank!! 🙂 So ein dummer Leichtsinnsfehler wie das vergessen der Panel größen wird mir bestimmt nicht nochmal passieren! 🙂
 
das wird dir nicht das letzte Mal passieren ... und man findet die nicht mal beim debuggen, weil man ja meint etwas falsch gemacht zu haben aber tatsächlich hat mans NUR vergessen.:lol:
 
Also mein Rat an dich. Lerne GUI Programmierung gleich richtig und lass es dir von einem LayoutManager (oder am besten von vielen, die geschachtelt sind) machen. Fixe Werte sind immer gefährlich und in den wenigsten Fällen sinnvoll. Wenn ich nacher noch Zeit habe, dann zeig ich dir ein schönes Beispiel wie man soetwas normalerweise löst.
 
Ja ich hatte das Problem vorher auch mit Layout Managern gelöscht nur hatte ich dabei das Problem, dass ich es nicht hinbekommen habe den "=" Button größer als die anderen zu machen(bzw. hab ihn größer gemacht aber da war der Abstand zwischen der TextArea zu groß) .. Deswegen hab ich mich jetzt einfach für das null-Layout entschieden.
 
So ich hab dir jetzt einmal den Windowstaschenrechner nachgebildet auf die einfachste Art. Logik zu implementieren ist dein Part.

Java:
package calc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator {
  
  private JFrame frame;
  
  public static void main(String[] args) {
    Calculator calc = new Calculator();
    calc.show();
  }
  
  public Calculator() {
    init();
  }
  
  private void init() {
    frame = new JFrame("Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationByPlatform(true);
    frame.setPreferredSize(new Dimension(300, 400));
    frame.setResizable(false);
    JPanel mainPanel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
    mainPanel.setLayout(boxLayout);
    /* Anzeige für Rechner */
    JTextField calcArea = new JTextField(15);
    calcArea.setEditable(false);
    calcArea.setBackground(Color.WHITE);
    mainPanel.add(calcArea);
    
    /* Steuerelemente */
    JPanel controlPanel = new JPanel(new GridBagLayout());
    String[][] commands = { { "MC", "MR", "MS", "M+", "M-" }, { "\u2190", "CE", "C", "\u00B1", "\u221A" }, { "7", "8", "9", "/", "%" },
        { "4", "5", "6", "*", "1/x" }, { "1", "2", "3", "-", "=" }, { "0", "", ",", "+" } };
    for (int y = 0; y < commands.length; y++) {
      for (int x = 0; x < commands[y].length; x++) {
        if (commands[y][x].equals("0")) {
          addButton(commands[y][x], controlPanel, x, y, 1, 2);
        } else if (commands[y][x].equals("=")) {
          addButton(commands[y][x], controlPanel, x, y, 2, 1);
        } else if (!commands[y][x].isEmpty()) {
          addButton(commands[y][x], controlPanel, x, y, 1, 1);
        }
      }
    }
    mainPanel.add(controlPanel);
    
    frame.add(mainPanel);
    frame.pack();
  }
  
  private void addButton(String name, JPanel controlPanel, int x, int y, int gridheight, int gridwidth) {
    GridBagConstraints gbc = new GridBagConstraints();
    JButton curButton = new JButton(name);
    curButton.addActionListener(event -> System.out.println(name));
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.insets = new Insets(0, 0, 2, 2);
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridheight = gridheight;
    gbc.gridwidth = gridwidth;
    controlPanel.add(curButton, gbc);
  }
  
  public void show() {
    frame.setVisible(true);
  }
}
 

Zurück
Oben