0

What i want is my timer buttons to be on the bottom with minimal space for them, i dont want them to take half of the frame, but i dont know how to resize the panel within the frame. Help would be appreciated and if someone could proof read it too that would be nice.

import java.awt.GridLayout; 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.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; public class SudokuPanel extends JFrame { public final int SQUARE_COUNT = 9; public Squares [] squares = new Squares[SQUARE_COUNT]; public SudokuPanel(){ super("Sudoku"); setSize(600,600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(2,1)); JPanel panel = new JPanel(new GridLayout(3,3)); for(int i=0; i<SQUARE_COUNT; i++){ squares[i] = new Squares(); panel.add(squares[i]); } JPanel panel2 = new JPanel(); JButton start = new JButton(); JButton stop = new JButton(); start = new JButton("Start Timer"); stop = new JButton("Stop Timer"); panel2.add(start); panel2.add(stop); add(panel); add(panel2); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("Menu"); menuBar.add(menu); JMenuItem newDifficulty = new JMenuItem("Select New Difficulty"); menu.add(newDifficulty); JMenuItem reset = new JMenuItem("Reset"); menu.add(reset); JMenuItem exit = new JMenuItem("Exit"); menu.add(exit); class newDifficultyaction implements ActionListener{ public void actionPerformed (ActionEvent e){ dispose(); Level select = new Level(); } } class exitaction implements ActionListener{ public void actionPerformed (ActionEvent e){ System.exit(0); } } newDifficulty.addActionListener(new newDifficultyaction()); exit.addActionListener(new exitaction()); setVisible(true); setLocationRelativeTo(null); } } 

1 Answer 1

1

I can't run your code since you're using some classes not included. To put your buttons taking up minimal space at the bottom of the panel, I'd suggest using a BorderLayout for the Frame.

So instead of GridLayout, use:

setLayout(new BorderLayout()); 

Then add panel2 with the buttons to the bottom of the frame:

add(panel2, BorderLayout.PAGE_END); 

Since panel is the main component, you'll want that to take up most of the space, so position it center:

add(panel, BorderLayout.CENTER); 
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but the default layout manager for a JFrame is the BorderLayout, so just delete the setLayout( new GridLayout(...) ) statement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.