簡體   English   中英

Java JLabel不會重新定位

[英]java JLabel won't reposition

我想將我的jLabel設置在jButton之上。 實際上,如果它們只是“漂浮”在彼此之上會是理想的。

Jet,我無法顯示標簽,因為它停留在按鈕下方。 我一直在谷歌搜索和嘗試諸如setLocation之類的事情,但無濟於事。 我確定這是我所缺少的超級基礎...

    public class TranceExperiment extends JFrame {

        public TranceExperiment() {

            setTitle("Simple example");
            setSize(600, 400);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }


        public static void main(String[] args) {

            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    TranceExperiment ex = new TranceExperiment();
                    ex.setVisible(true);

                    //set startLabel
                    JLabel expText = new JLabel("Welcome to the experiment");
                    ex.add(expText);
                    expText.setLocation(200,200);
                    //expText.setSize(100, 100);
                   expText.setVisible(true);
//                    expText.setLayout(null);
                    ex.add(expText);


                    //start Button
                    final JButton startButton = new JButton("Start");
                    startButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent event) {
                            //do stuff

                            startButton.setText("Continue");

                        }

                    });

                    ex.add(startButton);

                }
            });
        }
    }

如果需要整齊的布局,則必須使用布局管理器。 其中包括FlowLayout,BorderLayout和GridLayout 我建議針對此特定問題使用GridLayout,例如:

ex.setLayout(new GridLayout(3,1));

3是行,1是列。 在將按鈕和標簽添加到框架之前,請插入此行。

編輯:

您還可以將JPanel添加到框架中,然后將面板的布局設置為GridLayout,如下所示:

ex.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1));
panel.add(startButton);
ex.add(panel, BorderLayout.NORTH);

通過使用BorderLayout ,可以將面板放置在框架中的任何位置。 只需記住設置面板和/或框架的尺寸,並在面板上添加其他標簽即可。 BorderLayout有一些設置可供您使用,包括SOUTH,CENTER,EAST和WEST。

實現目標的最簡單方法是使用WindowBuilder ,這是Eclipse的插件( https://eclipse.org/windowbuilder/ )。

首先從可用布局列表中選擇GroupLayout

然后將jLabel放在jLabel上方,然后選擇“ jButton Bottom ”。 該插件將為您生成代碼。

在此處輸入圖片說明

使用布局,盧克! :)我喜歡GridBagLayout,因為它非常靈活。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

public static void main(String[] args) {
    System.out.println("Hello World!");
    final JFrame frame = new JFrame();
    frame.setTitle("Simple example");
    frame.setSize(600, 400);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(new BorderLayout());

    JPanel contentPanel = new JPanel(new GridBagLayout());
    frame.add(contentPanel);

    JLabel expText = new JLabel("Welcome to the experiment");
    expText.setPreferredSize(new Dimension(150, 40));
    expText.setVisible(true);

    final JButton startButton = new JButton("Start");
    startButton.setPreferredSize(new Dimension(450, 40));
    startButton.setPreferredSize(new Dimension(450, 40));
    startButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            startButton.setText("Continue");
        }

    });

    contentPanel.add(expText, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 200, 0, 100), 0, 0));
    contentPanel.add(startButton, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    contentPanel.add(Box.createVerticalGlue(), new GridBagConstraints(0, 1, 2, 2, 1, 1,
            GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    }
}

setLocationsetSize並非由框架的用戶您調用。 它們由與組件的容器關聯的布局管理器調用。

您可以在Java網站上找到有關布局管理如何工作的全面而詳細的信息: http : //docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

要獲得所需的內容, SpringLayout似乎更合適。

或者,如果您要直接調用setLocationsetSize ,則應通過將布局管理器設置為null來刪除它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM