简体   繁体   English

Java JLabel不会重新定位

[英]java JLabel won't reposition

I want to set my jLabel above the jButton. 我想将我的jLabel设置在jButton之上。 In fact, it would be ideal if they would just "float" above eachother. 实际上,如果它们只是“漂浮”在彼此之上会是理想的。

Jet, I can't get the label to display because it stays below the button. Jet,我无法显示标签,因为它停留在按钮下方。 I have been googling and trying things like setLocation, but to no avail. 我一直在谷歌搜索和尝试诸如setLocation之类的事情,但无济于事。 I'm sure this is something super basic that I'm missing... 我确定这是我所缺少的超级基础...

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

                }
            });
        }
    }

You have to use Layout managers if you want neatly spaced layouts. 如果需要整齐的布局,则必须使用布局管理器。 There are a variety of these including FlowLayout, BorderLayout and GridLayout . 其中包括FlowLayout,BorderLayout和GridLayout I would advise using GridLayout for this particular problem, for example: 我建议针对此特定问题使用GridLayout,例如:

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

The 3 is for rows and the 1 is for columns. 3是行,1是列。 Insert this line before you add the button and label to the frame. 在将按钮和标签添加到框架之前,请插入此行。

EDIT: 编辑:

You can also add a JPanel to your frame and then set the layout of the panel to GridLayout, like so: 您还可以将JPanel添加到框架中,然后将面板的布局设置为GridLayout,如下所示:

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

By using BorderLayout you can position your panel anywhere in the frame. 通过使用BorderLayout ,可以将面板放置在框架中的任何位置。 Just remember to set the size of the panel and/or the frame, and also to add the other labels to the panel. 只需记住设置面板和/或框架的尺寸,并在面板上添加其他标签即可。 BorderLayout has a couple of settings you can play around with including SOUTH, CENTER, EAST and WEST. BorderLayout有一些设置可供您使用,包括SOUTH,CENTER,EAST和WEST。

The easiest way to achieve your goal is to use WindowBuilder , a plugin for Eclipse ( https://eclipse.org/windowbuilder/ ). 实现目标的最简单方法是使用WindowBuilder ,这是Eclipse的插件( https://eclipse.org/windowbuilder/ )。

First select GroupLayout from the list of available layouts. 首先从可用布局列表中选择GroupLayout

Then put your jLabel above your jButton and select " Anchored Bottom ". 然后将jLabel放在jLabel上方,然后选择“ jButton Bottom ”。 The plugin will generate the code for you. 该插件将为您生成代码。

在此处输入图片说明

Use Layout, Luke! 使用布局,卢克! :) I like GridBagLayout, because it is very flexible. :)我喜欢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));
    }
}

setLocation and setSize are not meant to be called by you, user of the frame. setLocationsetSize并非由框架的用户您调用。 They are meant to be called by the layout manager which is associated to the container of the component. 它们由与组件的容器关联的布局管理器调用。

You can find comprehensive and detailed information about how layout management works on the Java website: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 您可以在Java网站上找到有关布局管理如何工作的全面而详细的信息: http : //docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

To obtain what you want, a SpringLayout seems more appropriate. 要获得所需的内容, SpringLayout似乎更合适。

As an alternative, if you want to directly call setLocation and setSize , you should remove the layout manager, by setting it to null . 或者,如果您要直接调用setLocationsetSize ,则应通过将布局管理器设置为null来删除它。

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

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