我想要的是我的计时器按钮位于底部,空间最小,我不希望它们占用一半的框架,但我不知道如何调整框架内的面板大小。帮助将不胜感激,如果有人可以证明阅读它也会很好。
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);
}
}
答案 0 :(得分:1)
我无法运行您的代码,因为您正在使用未包含的某些类。为了让按钮占据面板底部的最小空间,我建议使用BorderLayout作为框架。
因此,使用:
而不是GridLayoutsetLayout(new BorderLayout());
然后将带有按钮的panel2添加到框架的底部:
add(panel2, BorderLayout.PAGE_END);
由于panel
是主要组件,因此您希望占用大部分空间,因此将其置于中心位置:
add(panel, BorderLayout.CENTER);