How to put a JButton at a specific location?

后端 未结 3 713
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 06:12

I know that there are already very similar questions to my question on stackoverflow, but neither one of them answers my question. I want to put my JButton on a specific locatio

相关标签:
3条回答
  • 2021-01-27 06:24

    Make use of appropriate layout managers to achieve your goals...

    Layout

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

    Layout

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

    Take a look at Laying Out Components Within a Container for more ideas

    0 讨论(0)
  • 2021-01-27 06:24

    Your best bet is to use a Java Drag-and-Drop GUI builder like NetBeans. Particularly if you're interested in very specific positioning, nothing really beats a GUI builder.

    0 讨论(0)
  • 2021-01-27 06:46

    If you want to put the item into a specific coordinate you have to use null Layout:

    Example:

    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)

    0 讨论(0)
提交回复
热议问题