简体   繁体   中英

How to fix JButton Location

Previously I have asked a question about this program and I fixed it with you people. Now I have created new panel on the first panel which is containing the game of shapes. So I tried to set JButton on the new panel. But I cannot change the location of Jbuttons. I am trying to set center of new panel.I have tried already FLowLayout(), BorderLayout() and setBounds(); Something is going wrong.

Previous questions; How to move JFrame shape

public class myshapestry extends JFrame implements ActionListener {

JFrame frame=new JFrame("Deneme");

 Startthegame panelstart= new Startthegame();
Container l ;

JLabel statusbar = new JLabel("default");
static JButton start;
static JButton exit;

 myshapestry() {

     l=this.getContentPane();
     this.setLayout(null);

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.pack();
     frame.setLocationRelativeTo(null);

     frame.add(panelstart);
     frame.add(statusbar, BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.setSize(getPreferredSize());

       start = new JButton("Start");
       exit = new JButton("Exit");


       panelstart.add(start);
       panelstart.add(exit);




    }

 public Dimension getPreferredSize() {
       return new  Dimension(500,600);
   }

   public static void main (String args[]){
       myshapestry tr=new myshapestry();
       tr.setTitle("Game of Shapes");

    } 

   public class Startthegame extends JPanel {

   }







   }

You're extending from JFrame , but creating a new instance of JFrame within the class and interacting with both, which is just confusing the issues. Start by getting rid of extends JFrame , you're not adding any new functionality to the class and it's just locking you into a single use case

static is not your friend and you should avoid using it where possible. In this case, you can move the buttons to the Startthegame . This is basic OO principle of isolation responsibility to units of work.

To get the buttons to center horizontally and vertically within the container, you can use a GridBagLayout

public class Startthegame extends JPanel {

    private JButton start;
    private JButton exit;

    public Startthegame() {
        setLayout(new GridBagLayout());

        start = new JButton("Start");
        exit = new JButton("Exit");

        add(start);
        add(exit);
    }

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

}

You should also call setVisible AFTER you've established the basic UI, otherwise you could end up within components not been displayed

For example...

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
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 MyShapesTry {

    JFrame frame = new JFrame("Deneme");

    Startthegame panelstart = new Startthegame();
    Container l;

    JLabel statusbar = new JLabel("default");

    public MyShapesTry() {

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(panelstart);
        frame.add(statusbar, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

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

                MyShapesTry tr = new MyShapesTry();
            }
        });
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM