简体   繁体   中英

How to reposition JButton in a JPanel?

So I'm trying to put my JButton directly under my JLabel instead of next to it, which is the default position. I can't seem to figure out how to reposition things, though. I've tried the setLocation() method and that doesn't do anything. Here's my code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestClass
{
    public int timesPressed;

    public static void main (String[] args)
    {
        new TestClass();
    }

    public TestClass()
    {
        JPanel jpanel = new JPanel();

        JLabel jlabel = new JLabel ("You've clicked the button " + timesPressed + " times.");

        JButton jbutton = new JButton ("Button");
        jbutton.addActionListener (new ActionListener()
        {
            public void actionPerformed (ActionEvent event)
            {
                timesPressed++;
                jlabel.setText ("You've clicked the button " + timesPressed + " times.");
            }
        });

        jpanel.add (jlabel);
        jpanel.add (jbutton);

        JFrame jframe = new JFrame ("Test Frame");
        jframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        jframe.setSize (400, 100);
        jframe.setResizable (false);
        jframe.add (jpanel);
        jframe.setVisible (true);
    }
}

Am I missing something? Thanks.

You are not using a Layout Manager or rather using the default one (FlowLayout), which is useful only in rare cases. Here is a guide to layout managers by Oracle.

In short Layout Managers determine the location of your components(JButton, JLabel, etc). FlowLayout just places them next to each other until it runs out of space. Generally you never use setLocation() but rather add(component) and you let the layout manager handle its position.

Try using a BoxLayout . Given the y axis parameter, it will naturally flow in the direction you require, and not expand the child components to fit the parent. BoxLayout respects the preferred size of JComponents.

Try this:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestClass
{
    public int timesPressed;
    JLabel jlabel;

    public static void main (String[] args)
    {
        new TestClass();
    }

    public TestClass()
    {
        JPanel jpanel = new JPanel();

        jlabel = new JLabel ("You've clicked the button " + timesPressed + " times.");

        JButton jbutton = new JButton ("Button");
        jbutton.addActionListener (new ActionListener()
        {
            public void actionPerformed (ActionEvent event)
            {
                timesPressed++;
                jlabel.setText ("You've clicked the button " + timesPressed + " times.");
            }
        });



        JFrame jframe = new JFrame ("Test Frame");

        jbutton.setSize(10,10);
        jpanel.setLayout(new GridLayout(0, 1));

        jpanel.add (jlabel);
        jpanel.add (jbutton);

        jframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        jframe.setSize (400, 100);
        jframe.setResizable (false);
        jframe.add (jpanel);
        jframe.setVisible (true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM