如何将 JButton 放在特定位置?

How to put a JButton at a specific location?

我知道在 Whosebug 上已经有与我的问题非常相似的问题,但没有一个能回答我的问题。 我想将 JButton 放在 JAVA 中 JFrame 的特定位置。这是我的代码:

public class DetectMotion extends JFrame {

private JLabel label = null;

public DetectMotionExample() {

    JPanel pnlButton = new JPanel();
    label = new JLabel(nothing);
    JButton btn = new JButton("Close");

    pnlButton.setLayout(new BorderLayout());

    setTitle("Motion Detector");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    btn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("Close")) {
                //TO-DO
            }
        }
    });

    WebcamPanel panel = new WebcamPanel(webcam);

    add(panel);
    pnlButton.add(btn,BorderLayout.SOUTH);
    pnlButton.add(label,BorderLayout.NORTH);
    add(pnlButton);

    pack();
    setVisible(true);
}

}

我的 JFrame (webcamPanel) 上有一个面板和一个名为 pnlButton 的 JPanel,其中包括 JButton (btn) 和 JLabel (label)。

现在,我如何才能具体更改按钮的位置而不是仅使用 BorderLayout.NORTH?

谢谢!

如果您想将项目放入特定坐标,则必须使用 null 布局:

示例:

public void newClass(){

  public newClass(){

    JPanel panel = new JPanel();
    JButton button = new JButton("button1");


   panel.setLayout(null);
   button.setBounds(x,y,X,Y);
   panel.add(button);
}

public static void main(String[] args){
      newClass();
 }

}

Doing Without a Layout Manager (Absolute Positioning)

您最好的选择是使用 Java 拖放式 GUI 生成器,例如 NetBeans。特别是如果您对非常具体的定位感兴趣,没有什么比 GUI 构建器更好的了。

使用适当的布局管理器来实现您的目标...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DetectMotion extends JFrame {

    private JLabel label = null;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DetectMotion frame = new DetectMotion();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public DetectMotion() {

        JPanel pnlButton = new JPanel();
        label = new JLabel("nothing");
        JButton btn = new JButton("Close");

        pnlButton.setLayout(new GridBagLayout());

        setTitle("Motion Detector");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals("Close")) {
                    //TO-DO
                }
            }
        });

        JPanel panel = new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }

        };
        panel.setBackground(Color.RED);

        add(panel);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weighty = 1;

        pnlButton.add(label, gbc);

        gbc.weighty = 0;
        pnlButton.add(btn, gbc);
        add(pnlButton, BorderLayout.EAST);

        pack();
        setVisible(true);
    }

}

package javaapplication647;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DetectMotion extends JFrame {

    private JLabel label = null;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DetectMotion frame = new DetectMotion();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public DetectMotion() {

        JPanel pnlButton = new JPanel();
        label = new JLabel("nothing");
        JButton btn = new JButton("Close");

        pnlButton.setLayout(new FlowLayout(FlowLayout.RIGHT));

        setTitle("Motion Detector");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals("Close")) {
                    //TO-DO
                }
            }
        });

        JPanel panel = new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }

        };
        panel.setBackground(Color.RED);

        add(panel);
        pnlButton.add(label);
        pnlButton.add(btn);
        add(pnlButton, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }

}

查看 Laying Out Components Within a Container 了解更多想法