简体   繁体   English

如何在JPanel中重新放置JButton?

[英]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. 因此,我试图将我的JButton直接放在JLabel的下面而不是它的旁边,这是默认位置。 I can't seem to figure out how to reposition things, though. 我似乎无法弄清楚如何重新定位。 I've tried the setLocation() method and that doesn't do anything. 我已经尝试过setLocation()方法,但是它什么也没做。 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. 您不使用布局管理器,而是使用默认管理器(FlowLayout),这仅在极少数情况下有用。 Here is a guide to layout managers by Oracle. 是Oracle的布局管理器指南。

In short Layout Managers determine the location of your components(JButton, JLabel, etc). 简而言之,布局管理器确定组件(JButton,JLabel等)的位置。 FlowLayout just places them next to each other until it runs out of space. FlowLayout只是将它们彼此相邻放置,直到空间用完。 Generally you never use setLocation() but rather add(component) and you let the layout manager handle its position. 通常,您从不使用setLocation()而是使用add(component)并让布局管理器处理其位置。

Try using a BoxLayout . 尝试使用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. 给定y轴参数,它自然会沿您所需的方向流动,并且不会扩展子组件以适合父组件。 BoxLayout respects the preferred size of JComponents. BoxLayout遵循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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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