-1

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 {

   }







   }
Community
  • 1
  • 1

1 Answers1

2

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...

enter image description here

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

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Okey, Thank you so much, I agree with you until main class. In main class I am getting the errors. the error is contains } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } – Muhammed Yalçın Kuru Jun 27 '15 at 00:09
  • I am using JavaSe 1.7 – Muhammed Yalçın Kuru Jun 27 '15 at 00:32
  • Well, multi-catch is a feature of 1.7. What's the exact error – MadProgrammer Jun 27 '15 at 00:33
  • Can you explain this block? EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } – Muhammed Yalçın Kuru Jun 27 '15 at 00:48
  • Sounds like either your not importing the right exceptions or you're compiling to a version of Java below 1.7. The `invokeLater` block is associated with [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) – MadProgrammer Jun 27 '15 at 00:56