【问题标题】:Is it possible to position a JButton using coordinates?是否可以使用坐标定位 JButton?
【发布时间】:2016-02-15 03:07:12
【问题描述】:

我一直在尝试定位 JButton,但遇到了很多麻烦。我需要他们去一个非常精确的位置,但不知道如何实现。我见过一种叫做 FlowLayout 的东西,但我很困惑。几天前我问了另一个关于如何定位 JButton 的问题,他们给了我这个固定代码:

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame extends JFrame
{
private JButton button1;
private JButton button2;

public Frame()
{
    button1 = new JButton("Hello button1");
    button2 = new JButton("Hello button2");
    button2.setPreferredSize(new Dimension(100,100));
    button1.setPreferredSize(new Dimension(100,100));
    add(button2);
    add(button1);
}

}

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Panel extends JPanel
{
public static void main(String args [])
{
    Frame frame = new Frame();
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
}
}

【问题讨论】:

    标签: java jframe jpanel jbutton


    【解决方案1】:

    首先,不要在从左到右的流中使用FlowLayout()类FlowLayout组件,使Layout(null)。

    终于按钮可以用这个了

    button1.setBounds(XPosition, YPosition, WidthOFButton, HeightOFButton);
    

    【讨论】:

    • 所以我做 frame.setLayout(null); ?
    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 2013-04-10
    • 2015-06-07
    • 2019-07-30
    • 1970-01-01
    相关资源
    最近更新 更多