[英]Changing JPanel Size in my JFrame
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);
}
}
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.
为了使您的按钮在面板的底部占据最小的空间,我建议为Frame使用BorderLayout 。
So instead of GridLayout, use: 因此,请使用:
setLayout(new BorderLayout());
Then add panel2 with the buttons to the bottom of the frame: 然后将带有按钮的panel2添加到框架的底部:
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: 由于
panel
是主要组成部分,因此您希望它占据大部分空间,因此将其居中放置:
add(panel, BorderLayout.CENTER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.