使用JPanel Java自动调整JFrame的大小
我正在对扫雷游戏进行修改。其中之一就是困难。我已经成功地做到了这一点,并且工作正常,但是随着游戏板(在它自己的Jpanel中)变得越来越大和越来越小(取决于难度),我无法让JFrame自动调整大小。我正在使用:使用JPanel Java自动调整JFrame的大小,java,swing,resize,jframe,jpanel,Java,Swing,Resize,Jframe,Jpanel,我正在对扫雷游戏进行修改。其中之一就是困难。我已经成功地做到了这一点,并且工作正常,但是随着游戏板(在它自己的Jpanel中)变得越来越大和越来越小(取决于难度),我无法让JFrame自动调整大小。我正在使用: setPreferredSize(new Dimension(WIDTH, HEIGHT)); 设置窗口的初始大小,但这使得它非常小,就像只显示JMenuBar中的“File”一词一样。我必须手动调整它的大小 我在ActionListener事件上尝试了setSize()和frame.
setPreferredSize(new Dimension(WIDTH, HEIGHT));
设置窗口的初始大小,但这使得它非常小,就像只显示JMenuBar中的“File”一词一样。我必须手动调整它的大小
我在ActionListener事件上尝试了setSize()和frame.pack()之类的东西,但似乎无法调整它的大小
关于使用什么代码/方法的任何提示
编辑:代码发布
package mines;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SetupFrame extends JFrame {
private final int WIDTH = 600;
private final int HEIGHT = 500;
public JFrame frame;
public JMenuBar menubar;
public JMenu file;
public JMenu levels;
public JMenu help;
public JMenuItem login;
public JMenuItem save;
public JMenuItem resume;
public JMenuItem exit;
public JMenuItem easy;
public JMenuItem medium;
public JMenuItem hard;
private JLabel statusbar;
public JPanel main;
public JPanel buttonPanel;
public JPanel saved;
public JPanel game;
public Board mineGame;
public JButton ngButton;
public JButton undoButton;
public JButton redoButton;
public JTabbedPane tp;
public String[] levelPicker;
public JComboBox levelSelect;
public JFileChooser chooser;
public String filename;
public int difficulty;
public SetupFrame(){
frame = new JFrame();
String filename = JOptionPane.showInputDialog(frame, "Enter Your Name.");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Minesweeper");
//menubar, menus, menu items
menubar = new JMenuBar();
setJMenuBar(menubar);
file = new JMenu("File");
help = new JMenu("Help");
menubar.add(file);
menubar.add(help);
login = new JMenuItem("Login..");
save = new JMenuItem("Save..");
resume = new JMenuItem("Resume..");
exit = new JMenuItem("Exit");
file.add(login);
file.add(save);
file.add(resume);
file.addSeparator();
file.add(exit);
statusbar = new JLabel("");
chooser = new JFileChooser(); // new File Chooser for saved tab
undoButton = new JButton(" Undo "); //undo Button for game panel
ngButton = new JButton(" New Game ");//new game Button for game panel
redoButton = new JButton(" Redo");//redo Button for game panel
main = new JPanel(new BorderLayout()); //new panel for main game
//main.add(mineGame, BorderLayout.CENTER); //add instance mineGame to main panel
game = new JPanel(new BorderLayout());// new panel for game tab
main.add(game, BorderLayout.CENTER); //add the mineGames panel to game panel
game.add(statusbar, BorderLayout.SOUTH); //add statusbar to bottom of game panel
//game.add(button, BorderLayout.NORTH); // add buttons (eventually be redo, undo, new game)
saved = new JPanel(); // create new panel for the saved tab
saved.add(chooser);//add the File Chooser to the saved tab
String[] levelPicker = {"Easy", "Medium", "Hard"};
levelSelect = new JComboBox(levelPicker);
levelSelect.setSelectedIndex(0);
//levelSelect.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.add(undoButton);
buttonPanel.add(ngButton);
buttonPanel.add(redoButton);
buttonPanel.add(levelSelect);
main.add(buttonPanel, BorderLayout.NORTH);
//create & add the tabs
tp = new JTabbedPane();
tp.addTab ("Game", main);
tp.addTab ("Saved", saved);
tp.addTab ("Statistics", null);
add(tp);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setResizable(true);
setVisible(true);
frame.pack();
class listener implements ActionListener{
public void actionPerformed (ActionEvent e)
{
if(e.getSource() == ngButton){
//JOptionPane.showInputDialog(frame, "Do You want To Save");
newMineGame();
}
JComboBox cb = (JComboBox)e.getSource();
String picker = (String)cb.getSelectedItem();
if (picker == "Easy"){
difficulty = 0;
newMineGame();
}
if (picker == "Medium"){
difficulty = 1;
newMineGame();
frame.pack();
}
if (picker == "Hard"){
difficulty = 2;
newMineGame();
frame.pack();
}
}
private void newMineGame() {
game.removeAll();
mineGame = new Board(statusbar, difficulty);
game.add(mineGame, BorderLayout.CENTER);
game.add(statusbar, BorderLayout.SOUTH);
repaint();
}
}
ngButton.addActionListener(new listener());
undoButton.addActionListener(new listener());
redoButton.addActionListener(new listener());
levelSelect.addActionListener(new listener());
}
public static void main(String[] args) {
new SetupFrame();
}
其中之一就是困难。我已经设法做到了这一点
这是可行的,但随着游戏板(在它自己的Jpanel中)变得越来越大&
更小(取决于难度),我无法使JFrame运行
自动调整大小
及
在ActionListener上尝试了setSize()和frame.pack()之类的功能
事件,但我似乎无法使其调整大小
JFrame.pack()
- 表示矿山的所有
(有最好的方法使用JComponents
)正确地JToggleButton
返回到其父级(PreferredSize
)JPanel
- 由GridLayout铺设的父级(JPanel)(非常简单)
- 还有两种方法,即如何、何时、何地
JFrame.pack()
- 使用
,刷卡后的下一个代码行是CardLayout
JFrame.pack()
- 删除旧的
(从JPanel
)并替换为新的,然后需要调用JFrame
,JFrame.(重新)验证()
和JFrame.repaint()
,作为最后一行代码JFrame.pack()
- 表示矿山的所有
- 可能还有另一个问题,重要的是在有
JFrame.setresizeable(false)设置的情况下,代码顺序代码>
编辑后
- 使用
- 在这里,您错过了代码行(不要扩展
,将此对象创建为JFrame
)Local variable
,JFrame.(重新)验证()
和JFrame.repaint()
作为JFrame.pack()
private void newMineGame(){
但我不明白你说的“你错过了代码行”是什么意思 (不要扩展JFrame,将此对象创建为局部变量) 代码可能是
import javax.swing.*;
public class SetupFrame {
private JFrame frame;
private JMenuBar menubar = new JMenuBar();
private Board mineGame;
public SetupFrame() {
//there add required JComponents
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setTitle("Minesweeper");
frame.setJMenuBar(menubar);
frame.add(mineGame);
//frame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
//frame.setResizable(true);//not neccessary
frame.pack();
frame.setVisible(true);
}
private void newMineGame() {
//remove old Board
//add a new Board
frame.validate();
frame.repaint();
frame.pack();
}
private static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SetupFrame();
}
});
}
}
这是你的错误:
frame.pack();
如果您的SetupFrame
实际上是从JFrame
扩展而来,为什么需要frame
?只需pack()
更改这一行即可
@mKorbel已经发布了关于pack()
行为的完整且非常有用的解释(谢谢)
更新
同样,在您的侦听器
类中,当按下JButton
时,您会遇到此异常:
java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JComboBox
您需要进行以下小更改以避免此问题:
class listener implements ActionListener{
public void actionPerformed (ActionEvent e) {
if(e.getSource() == ngButton){
//JOptionPane.showInputDialog(frame, "Do You want To Save");
newMineGame();
} else if(e.getSource() instanceof JComboBox){ // add this else-if block
JComboBox cb = (JComboBox)e.getSource();
String picker = (String)cb.getSelectedItem();
if (picker.equals("Easy")){ // <-- picker == "Easy" is not the proper way to compare string, use equals() method instead
difficulty = 0;
newMineGame();
}
if (picker.equals("Medium")){
difficulty = 1;
newMineGame();
//frame.pack(); <--- again, just use pack();
pack();
}
if (picker.equals("Hard")){
difficulty = 2;
newMineGame();
//frame.pack(); <--- again, just use pack();
pack();
}
}
}
类侦听器实现ActionListener{
已执行的公共无效操作(操作事件e){
如果(如getSource()==ngButton){
//showInputDialog(框“您想保存吗”);
newMineGame();
}else if(e.getSource()instanceof JComboBox){//添加这个else if块
JComboBox cb=(JComboBox)e.getSource();
字符串选择器=(字符串)cb.getSelectedItem();
如果(picker.equals(“Easy”){//您似乎使用了正确的方法,但可能不正确。发布您的代码。此外,您是否在使用JPanel或JFrame上的方法?使用validate();
或revalidate();
倾向于“刷新”JFrame/JPanel-您是否尝试过使用这些工具?使用CardLayout作为最简单的方法。为了更快地提供帮助,请发布一个。有关更改PLAF大小的工作示例,请参阅。谢谢您的回答,但我不明白您的意思:“您错过了代码行。”(不要扩展JFrame,将此对象创建为局部变量);从公共类SetupFrame扩展JFrame中删除扩展JFrame
{
,因为有两个JFrame
s,其中一个永远不可见,并且对这个JFrame
进行了更改,您需要在每个setXxxXxx
和add()之前添加frame.
,在IDE
中有红色警告谢谢。我做了更改,但不幸的是,JFrame仍然没有调整大小:-(。所有内容都来自电路板,删除H&W的所有设置,覆盖set/getPreferredSize作为第一个元素。网格中的元素,注释必须按网格(包)放置Layout,则setSize和setPreferredSize的任何父项都不需要任何设置,例如JPanel、JFrame等。谢谢。我实际上尝试了pack(),但它本身不起作用。正如我所说,我尝试了很多方法,frame.pack();只是我最后尝试的:-)@AngeKing你确定吗?我刚刚做了更改,它对我来说很有吸引力。我没有板
类来测试难度级别更改时的行为。请查看更新的答案,我发现了一个小错误。谢谢你的提示。我更改了代码,并且看到了添加另一个帧的错误。它仍然不会根据f难度级别,因此我将研究ItemListener。