0

i have this code and im trying to move the button however, it stays as a big block that takes the entire top of the frame.

    JFrame f = new JFrame();
    f.setTitle("hi");
    f.setSize(600,600);

    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    JButton b1 = new JButton("Click me!");
    b1.setBounds(50, 50, 50, 50);
    p.add(b1, BorderLayout.NORTH);

    JTextArea l =new JTextArea();
    l.setSize(100, 100);

    p.add(l,BorderLayout.SOUTH);

    ActionListener as = new ActionListener() {

        public void actionPerformed(ActionEvent a) {
            System.out.println("Husam");
        }
    };

    b1.addActionListener(as);
    f.add(p);
    f.setVisible(true);

How can i control the position of the button?

khbq
  • 13
  • 5
  • 1
    By using the appropriate layout manager. https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – JB Nizet Apr 07 '18 at 14:58
  • `im trying to move the button` - where? Should it be centered, left aligned, right aligned. A layout should be done with general terms so it adjusts as the frame size changes. You should not be using pixel locations. `it stays as a big block that takes the entire top of the frame.` - that is the rule of the BorderLayout when you add a component to the "NORTH" of the BorderLayout. If you want more control, then you can use a wrapper panel for the button. Set the layout of the wrapper panel to something you want. Then add the button to the wrapper panel and the wrapper panel to the "NORTH". – camickr Apr 07 '18 at 15:06
  • Also use meaningful variable names. Name like "f, p, l" mean absolutely nothing to us. – camickr Apr 07 '18 at 15:09

0 Answers0