如何将JButton放在特定位置?

我知道在stackoverflow上我的问题已经有了非常类似的问题,但是他们都没有回答我的问题。 我想把我的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(标签)。

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

谢谢!

利用适当的布局管理器来实现您的目标……

布局

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

看看在容器中布置组件以获得更多想法

如果要将项目放入特定坐标,则必须使用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(); } } 

没有布局管理器(绝对定位)

最好的办法是使用像NetBeans这样的Java拖放式GUI构建器。 特别是如果您对非常具体的定位感兴趣,那么没有什么能真正击败GUI构建器。