簡體   English   中英

如何在JPanel中重新放置JButton?

[英]How to reposition JButton in a JPanel?

因此,我試圖將我的JButton直接放在JLabel的下面而不是它的旁邊,這是默認位置。 我似乎無法弄清楚如何重新定位。 我已經嘗試過setLocation()方法,但是它什么也沒做。 這是我的代碼:

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);
    }
}

我想念什么嗎? 謝謝。

您不使用布局管理器,而是使用默認管理器(FlowLayout),這僅在極少數情況下有用。 是Oracle的布局管理器指南。

簡而言之,布局管理器確定組件(JButton,JLabel等)的位置。 FlowLayout只是將它們彼此相鄰放置,直到空間用完。 通常,您從不使用setLocation()而是使用add(component)並讓布局管理器處理其位置。

嘗試使用BoxLayout 給定y軸參數,它自然會沿您所需的方向流動,並且不會擴展子組件以適合父組件。 BoxLayout遵循JComponents的首選大小。

嘗試這個:

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