Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将对象放置在JFrame上的特定位置(x,y)?_Java_Swing_User Interface_Jframe - Fatal编程技术网

Java 如何将对象放置在JFrame上的特定位置(x,y)?

Java 如何将对象放置在JFrame上的特定位置(x,y)?,java,swing,user-interface,jframe,Java,Swing,User Interface,Jframe,如何将对象放置在JFrame上的特定位置(x,y)?在此处查找。请仔细阅读,了解为什么过度使用这种方法会受到阻碍 要在JPanel中添加一个JButton,可以使用以下方法: JButton button = new JButton("Click Me"); button.setBounds(5, 5, 50, 30); panel.add(button); 请尝试以下示例程序: import java.awt.*; import javax.swing.*; public class Ab

如何将对象放置在JFrame上的特定位置(x,y)?

在此处查找。请仔细阅读,了解为什么过度使用这种方法会受到阻碍

要在JPanel中添加一个JButton,可以使用以下方法:

JButton button = new JButton("Click Me");
button.setBounds(5, 5, 50, 30);
panel.add(button);
请尝试以下示例程序:

import java.awt.*;
import javax.swing.*;

public class AbsoluteLayoutExample
{
    private void displayGUI()
    {
        JFrame frame = new JFrame("Absolute Layout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(null);

        JLabel label = new JLabel(
            "This JPanel uses Absolute Positioning"
                                    , JLabel.CENTER);
        label.setSize(300, 30);
        label.setLocation(5, 5);

        JButton button = new JButton("USELESS");
        button.setSize(100, 30);
        button.setLocation(95, 45);

        contentPane.add(label);
        contentPane.add(button);

        frame.setContentPane(contentPane);
        frame.setSize(310, 125);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new AbsoluteLayoutExample().displayGUI();
            }
        });
    }
}

查看此绝对布局代码示例:


在继承框架的类中:

setLayout(null); 
在您的组件中:

setLocation(x,y);

试试这两个。。。相互结合

setLocation()
setBounds()


使用2005年由NetBeans团队开发的GroupLayout更好。WindowsBuilder Pro是用java构建Gui的好工具

这个“窗格”是什么?我可以改用JPanel吗?@user1441845:请看一下这篇最新的编辑,如何将它与
JPanel
:-)我以前忘了告诉你,你必须编写
panel.setLayout(null)
,尽管我现在在这个代码示例中提到:-)对于AbsoluteLayout,必须使用Insets,不确定边界是否正确+1@mKorbel:IMHO,setBounds()被用来代替setSize()/setLocation()作为快捷方式,我们可以使用setBounds(),而不是使用它们,因为第一个参数代表X轴,第二个参数代表Y轴,第三个参数代表宽度,第四个参数代表高度。@nIcE cOw作为快捷方式:-):-:-:-:-:-:-:-):-)+1,我在其他地方看过本教程,只是不记得在哪里:-)+1,希望我能掌握GroupLayout,毫无疑问是最好的版面经理之一around@nIcEcOw:在NetBeans GUI编辑器中进行修补并检查生成的代码是试验
GroupLayout
@trashgood:有趣的想法,在IDE上拖放,试验和玩代码,通常的方式。这是一个很好的建议,谢谢:-)关注点:1)您需要设置JFrame的contentPane的布局,而不是JFrame本身。2) 如果容器的布局为空,那么编码人员完全负责添加的组件的位置和大小。3) 您应该添加一条关于避免使用空布局和绝对定位的警告。@HovercraftFullOfEels:BULL'S EYE!我错过了JFrame的那部分,你很容易就抓住了:-)