1

So Im trying to make a little program to calculate the area of a specific shape. The user should be able to make a input via a textfield (Like the height and stuff of the shapes). The he should press a button and the price should get printed. But it doesnt show up.

Code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;




public class Rechner extends JFrame implements ActionListener{
private static JButton button1;
private static JButton button2;
private static JButton button3;
private static JButton button4;
private static JTextField numberField;

private JPanel jpanel;

public Rechner(String titel){

    super(titel);

    jpanel = new JPanel();

    numberField = new JTextField(1500);
    add(numberField, BorderLayout.CENTER);

    button1 = new JButton("Rechteck");
    button1.setBounds(10, 10, 150, 30);
    button1.addActionListener(this);
    add(button1);

    button2 = new JButton("Dreieck");
    button2.setBounds(170, 10, 150, 30);
    button2.addActionListener(this);
    add(button2);

    button3 = new JButton("Trapez");
    button3.setBounds(330, 10, 150, 30);
    button3.addActionListener(this);
    add(button3);

    button4 = new JButton("Parallelogramm");
    button4.setBounds(490, 10, 150, 30);
    button4.addActionListener(this);
    add(button4);
    setResizable(false);


}


public static void main(String[] args) {

    Rechner frame = new Rechner("Menu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(660, 400);
    frame.setLayout(null);

    frame.setVisible(true);
}


public void actionPerformed(ActionEvent e) {

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

        System.out.println("fff");

    }
    String numberStr = numberField.getText();


}

}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
Kevin lel
  • 21
  • 4
  • I'm going to link to [ask] and the [tour] to get a hat. – Nissa Dec 20 '16 at 15:36
  • I haven't working with `swing`, but since this class implements `ActionListener`, I assume `actionPerformed` is part of that listener class. Shouldn't it be `@Override`? – Hypnic Jerk Dec 20 '16 at 15:39
  • 1
    What should show up exactly? Your `actionPerformed` just prints "fff" to the standard output (and also assigns a value to a local variable that is discarded immediately). What do you expect that to do? – RealSkeptic Dec 20 '16 at 15:41
  • @MichaelPickett `@Override` annotation helps us, but is not mandatory. More info about it at [When do you use Java's @Override annotation and why?](https://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) – Pshemo Dec 20 '16 at 15:46
  • @Pshemo Interesting. Thank you. – Hypnic Jerk Dec 20 '16 at 15:47

2 Answers2

2

The default layout manager of a JFrame (well, of its content pane in fact) is BorderLayout.

Without specified constraints , the component is added to BorderLayout.CENTER, so

add(component);

is the same as

add(component, BorderLayout.CENTER);

and each component added this way will replace the last component added to the center.

Also note that setBounds will have no effect if there is a layout manager, and that you create a JPanel that you never use.

Finally, you may want to have a look at this guide : A Visual Guide to Layout Managers

Arnaud
  • 17,229
  • 3
  • 31
  • 44
2

This line is mainly the problem:

add(numberField, BorderLayout.CENTER);

Is causing the TextField to fill the entire space. Then, the next time you add a component to the JFrame with BorderLayout.CENTER, the JTextField gets replaced. To fix this:

super(titel);
jpanel = new JPanel();
add(jpanel, BorderLayout.NORTH); //adding the jpanel

button1 = new JButton("Rechteck");
jpanel.add(button1);
button1.setBounds(10, 10, 150, 30);
//adding the other buttons to the JPanel...
//...
//...
button4.addActionListener(this);
button3.addActionListener(this);
button2.addActionListener(this);
button1.addActionListener(this);

numberField = new JTextField(1500);
add(numberField);//this will cause it to fill the remaining space
setResizable(false);

Explanation:

The buttons should go into the JPanel you created, and the JPanel should go into the JFrame's NORTH. That way they don't cover the JFrame

ItamarG3
  • 4,092
  • 6
  • 31
  • 44