Java 定心图形用户界面';s
我用GUI创建了一个简单的猜谜游戏。问题是,每当我最大化我的窗口时,整个GUI都卡在了中间的顶部。我想知道如何把它放在中间,如何使它变大。代码如下:Java 定心图形用户界面';s,java,swing,layout-manager,Java,Swing,Layout Manager,我用GUI创建了一个简单的猜谜游戏。问题是,每当我最大化我的窗口时,整个GUI都卡在了中间的顶部。我想知道如何把它放在中间,如何使它变大。代码如下: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GuessingGameNew implements ActionListener { private final double VERSION
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuessingGameNew implements ActionListener {
private final double VERSION = 2.3;
//Initializing Main Window and Difficulty Window
JFrame window = new JFrame("Guess The Number " + VERSION);
JFrame DiffFrame = new JFrame("Difficulty");
JButton btnNewGame = new JButton("New Game");
JButton btnInstruction = new JButton("Instructions");
JButton btnDifficulty = new JButton("Change Difficulty");
JButton btnAbout = new JButton("About");
JButton btnExit = new JButton("Exit");
JButton btnOK = new JButton("Ok");
JButton btnDiff[] = new JButton[6];
//Making Panel for Main Menu Buttons
JPanel pnlMainMenu = new JPanel();
//Making Panel for Difficulty Buttons
JPanel pnlDifficulty = new JPanel();
int diff = 10;
int tries;
int Secret;
int Guess;
int option = 0;
boolean Cancel = false;
GuessingGameNew() { //constructor
//Setting Main Window properties
window.setSize(400, 300);
window.setLocation(500, 260);
window.setLayout(new FlowLayout(FlowLayout.CENTER));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DiffFrame.setSize(230, 210);
DiffFrame.setLocation(530, 230);
DiffFrame.setLayout(new BorderLayout());
//MainMenu Panel Layout and adding Main Menu Buttons
// GridLayout(int rows, int columns, int Horizontal_Gap, intVertical_Gap)
pnlMainMenu.setLayout(new GridLayout(5, 1, 2, 8));
pnlMainMenu.add(btnNewGame);
pnlMainMenu.add(btnInstruction);
pnlMainMenu.add(btnDifficulty);
pnlMainMenu.add(btnAbout);
pnlMainMenu.add(btnExit);
pnlMainMenu.setBackground(Color.red);
//Setting Layout for Difficulty Panel
pnlDifficulty.setLayout(new GridLayout(6, 1, 2, 2));
btnDiff[0] = new JButton("Very Easy (0 - 3)");
btnDiff[1] = new JButton("Easy (0 - 50)");
btnDiff[2] = new JButton("Medium (0 - 100)");
btnDiff[3] = new JButton("Hard (0 - 500)");
btnDiff[4] = new JButton("Very Hard (0 - 1000)");
btnDiff[5] = new JButton("Custom (0 - ?)");
btnNewGame.addActionListener(this);
btnInstruction.addActionListener(this);
btnDifficulty.addActionListener(this);
btnAbout.addActionListener(this);
btnExit.addActionListener(this);
btnOK.addActionListener(this);
for(int i=0; i<6; i++) {
btnDiff[i].addActionListener(this);
pnlDifficulty.add(btnDiff[i]);
}
window.add(pnlMainMenu);
window.setVisible(true);
}
public void actionPerformed(ActionEvent click) {
System.out.println("Action Performed");
if(click.getSource() == btnNewGame) {
NewGame();
}
if(click.getSource() == btnExit) {
option = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit Game" ,JOptionPane.YES_NO_OPTION);
if(option == JOptionPane.YES_OPTION)
System.exit(0);
}
if(click.getSource() == btnInstruction) {
JOptionPane.showMessageDialog(null,
"Game:" + "\nClick New Game to start a new game.\nGuess a number between 0 and the selected number. Keep Guessing until you get it correct."
+ "\n\nDifficulty:" + "\nYou can change the difficulty of the game\n in the Main Menu to a Custom range or a \npreset range."
, "Instructions", JOptionPane.INFORMATION_MESSAGE);
}
if(click.getSource() == btnAbout) {
JOptionPane.showMessageDialog(null,JOptionPane.INFORMATION_MESSAGE);
}
if(click.getSource() == btnDifficulty) {
Change_Difficulty();
}
for(int i=0; i<6; i++) {
if(click.getSource() == btnDiff[i]) {
if(click.getSource() == btnDiff[0])
diff = 3;
if(click.getSource() == btnDiff[1])
diff = 50;
if(click.getSource() == btnDiff[2])
diff = 100;
if(click.getSource() == btnDiff[3])
diff = 500;
if(click.getSource() == btnDiff[4])
diff = 1000;
if(click.getSource() == btnDiff[5])
diff = Custom();
DiffFrame.setVisible(false);
}
}
}
public void NewGame() {
tries = 1;
Guess = 101;
Secret = (int)((Math.random()) * (diff + 1));
Cancel = false;
while(Guess != Secret) {
try {
if(tries == 1) {
Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: 1" + "\nGuess a number between 0 and " + diff, "Guess?", JOptionPane.PLAIN_MESSAGE));
tries++;
} else {
if(Guess > Secret)
Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: " + tries + "\n" + Guess + "\nGuess Lower..."));
else if(Guess < Secret)
Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: " + tries + "\n" + Guess + "\nGuess Higher..."));
tries++;
}
} catch(NumberFormatException e) {
if(e.getMessage() == "null") {
option = JOptionPane.showConfirmDialog(null, "Are you sure you want to go back to the Main Menu?", "Cancel?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(option == JOptionPane.YES_OPTION) {
Cancel = true;
break;
}
}
JOptionPane.showMessageDialog(null, "Error: " + e.getMessage() + "\nEnter whole numbers only!");
}
}
if(!Cancel) {
tries--;
JOptionPane.showMessageDialog(null, Guess + " is Correct!!\nYou WON in " + tries + " tries.", "Winner", JOptionPane.INFORMATION_MESSAGE);
option = JOptionPane.showConfirmDialog(null, "Do you want to try again?", "Try Again?", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
if(option == JOptionPane.YES_OPTION)
NewGame();
}
}
public void Change_Difficulty() {
DiffFrame.add(pnlDifficulty, BorderLayout.CENTER);
DiffFrame.setVisible(true);
}
public int Custom() {
try {
diff = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number that you want to be the range (0 to ?)", diff));
} catch(NumberFormatException e) {
}
return diff;
}
public static void main(String[] args) {
new GuessingGameNew();
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类GuessingGameNew实现ActionListener{
私人最终双版本=2.3;
//初始化主窗口和难度窗口
JFrame窗口=新JFrame(“猜数字”+版本);
JFrame DiffFrame=新JFrame(“难度”);
JButton btnNewGame=新JButton(“新游戏”);
JButton btn指令=新JButton(“指令”);
JButton btndicculty=新JButton(“变更难度”);
JButton btnAbout=新JButton(“关于”);
JButton btnExit=新JButton(“退出”);
JButton btnOK=新JButton(“Ok”);
JButton btnDiff[]=新JButton[6];
//制作主菜单按钮的面板
JPanel pnlMainMenu=新的JPanel();
//难度按钮面板的制作
JPanel pnldifculty=新的JPanel();
int-diff=10;
智力测验;
int秘密;
智力猜测;
int选项=0;
布尔取消=假;
GuessingGameNew(){//构造函数
//设置主窗口属性
设置窗口大小(400300);
窗口设置位置(500260);
setLayout(新的FlowLayout(FlowLayout.CENTER));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DiffFrame.setSize(230、210);
DiffFrame.setLocation(530230);
setLayout(新的BorderLayout());
//主菜单面板布局和添加主菜单按钮
//GridLayout(整数行、整数列、整数水平间距、整数垂直间距)
setLayout(新的GridLayout(5,1,2,8));
pnlMainMenu.add(btnewgame);
pnlMainMenu.add(BTN构造);
pnlMainMenu.add(b困难);
pnlMainMenu.add(btnAbout);
pnlMainMenu.add(btnExit);
pnlMainMenu.setBackground(颜色:红色);
//设置难度面板的布局
pnldifculty.setLayout(新网格布局(6,1,2,2));
btnDiff[0]=新的JButton(“非常简单(0-3)”;
btnDiff[1]=新的JButton(“容易(0-50)”;
btnDiff[2]=新的JButton(“中等(0-100)”;
btnDiff[3]=新的JButton(“硬(0-500)”;
btnDiff[4]=新的JButton(“非常硬(0-1000)”;
btnDiff[5]=新的JButton(“自定义(0-?)”;
btnNewGame.addActionListener(此);
btnConstruction.addActionListener(此);
btndicculty.addActionListener(此);
btnAbout.addActionListener(此);
btnExit.addActionListener(这个);
btnOK.addActionListener(这个);
对于(int i=0;i您正在设置:
此布局没有垂直对齐的概念,它仅在超出水平空间时包装其内容。设置对齐仅适用于水平行为
我想知道如何把它放在中间,如何使它变大。
如果删除该行,则将使用的默认居中
位置,该位置将水平和垂直居中并拉伸零部件:
JFrame的默认布局管理器是BorderLayout
。调用window.getContentPane().add(组件);
或者如果您想键入更多,则调用window.getContentPane().add(组件,BorderLayout.CENTER)
将您的组件添加到窗口的中心。另外,作为提示,请深入研究布局管理器。正确理解它们的工作方式、它们的功能以及哪一个更适合于哪种情况,您就可以构建真正酷的东西。此链接对我很有帮助:您应该阅读关于布局管理器的内容-现在,问题非常复杂d、 如果答案解决了您的问题,请勾选其左侧的绿色复选标记,将其标记为已接受。
window.setLayout(new FlowLayout(FlowLayout.CENTER));