-2

Hi guys i'm trying to create my own calculator and i have a problem with graphic part... i create a label and a button, and i set it in a label. they appear but i can't set their location what is the problem?? thanks for help

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class calc extends JFrame {

    public calc(String string) {

        this.setResizable(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("calculator");
        this.setPreferredSize(new Dimension(300, 400));

        JButton button1 = new JButton("calculate");
        button1.setBackground(Color.cyan);
        button1.setLocation(200, 100);
        button1.setPreferredSize(new Dimension(100, 20));

        JLabel label1 = new JLabel();
        label1.setText(main.main.text); // main.main.text is a text in main class it works XD

        JPanel panel1 = new JPanel();
        panel1.add(label1);
        panel1.add(button1);
        panel1.setPreferredSize(new Dimension(300, 400));
        this.getContentPane().add(panel1);

        this.pack();
        panel1.setVisible(true);
        button1.setVisible(true);
        label1.setVisible(true);

    }
}
Braj
  • 46,415
  • 5
  • 60
  • 76
golia
  • 95
  • 1
  • 1
  • 6
  • 1
    what is `main.main.text` here? share code for that as well. use proper getter method to access it. – Braj Jul 20 '14 at 19:00
  • 2
    You're trying to set the absolute position of components added to a JPanel which uses by default a FlowLayout, and FlowLayouts do not respect absolute positioning. Null layouts, do, but you should avoid use of null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Better solution: show us a picture of what you're trying to achieve, or more fully describe your GUI's layout, and we'll be better able to help you fix things. – Hovercraft Full Of Eels Jul 20 '14 at 19:05
  • Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Jul 21 '14 at 03:57

1 Answers1

-1

Swing does not let you set the location of objects because it manages the layout of your GUI with layout managers. If you really want you set the layout yourself you can use frame.getContentPane().setLayout(null); (or in your case getContentPane().setLayout(null) because your class extends JFrame already)

Then you can use the setSize and setLocation methods. However, this is a bad idea. Here is what the java documentation says about null layoutManagers:

Although we strongly recommend that you use layout managers, you can perform layout without them. By setting a container's layout property to null, you make the container use no layoutmanager With this strategy, called absolute positioning, you must specify the size and position of every component within that container. One drawback of absolute positioning is that it does not adjust well when the top-level container is resized. It also does not adjust well to differences between users and systems, such as different font sizes and locales.

Zach
  • 4,652
  • 18
  • 22
  • I am of the opinion that mentioning `null` layouts is counter-productive. Only an expert should attempt to layout a container using their own logic, then it should be encapsulated in a custom layout manager. By the time the person has enough experience to lay out a GUI, it is likely they will have already heard of `null` layouts. – Andrew Thompson Jul 21 '14 at 03:59
  • I am just providing the information that answers the question, the op asked how to position the components exactly, I did point out in the answer that it is a bad idea and that he should use LayoutManagers. – Zach Jul 21 '14 at 11:53