【发布时间】:2014-04-25 16:30:44
【问题描述】:
我是 Swing 新手,在替换现有 JFrame 时遇到了麻烦。我初始化第一个 JFrame 没有问题...
主要:
public static JFrame jf;
public static int state = 0;
public static void main(String args[]) {
jf = new MainMenu();
jf.setVisible(true);
while (state != -1) {}
}
主菜单:
public MainMenu() {
setTitle("Battleship");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton btnSinglePlayer = new JButton("Single Player - Easy");
btnSinglePlayer.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
GameDriver gd = new GameDriver(1);
}
});
JButton btnSplitScreenMultiplayer = new JButton("Split Screen Multiplayer");
btnSplitScreenMultiplayer.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
GameDriver gd = new GameDriver(0);
}
});
JButton btnOnlineMultiplayer = new JButton("Online Multiplayer");
btnOnlineMultiplayer.setEnabled(false);
JButton btnHowToPlay = new JButton("How to Play");
btnHowToPlay.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (Desktop.isDesktopSupported()) {
try {
//Set your page url in this string. For eg, I m using URL for Google Search engine
String url = "http://www.hasbro.com/common/instruct/battleship.pdf";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException e2) {
System.out.println(e2.getMessage());
}
}
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addContainerGap(128, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
.addComponent(btnHowToPlay, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnOnlineMultiplayer, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSinglePlayer, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnSplitScreenMultiplayer, Alignment.LEADING))
.addGap(121))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addContainerGap(89, Short.MAX_VALUE)
.addComponent(btnSinglePlayer)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnSplitScreenMultiplayer)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnOnlineMultiplayer)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnHowToPlay)
.addGap(45))
);
contentPane.setLayout(gl_contentPane);
}
但是一旦我进入 GameDriver 并尝试初始化一个不同的 JFRame...
游戏驱动:
//Stuff Happens
ProgramShell.jf.setVisible(false);
ProgramShell.jf = new PlacementWindow(currentPlayer, otherPlayer, curShip);
ProgramShell.jf.setVisible(true);
我得到一个没有所有组件的空白 JFrame(当我使用 PlacementWindow 的 main 初始化时它被正确创建)
放置窗口:
private JPanel contentPane;
private GameBoard gbCur;
private GameBoard gbOth;
private int curPlacement;
private String title;
private int sqSize = 21;
private int xOffset = 27;
private int yOffset = 27;
private int divOffset = 283;
public PlacementWindow(GameBoard cur, GameBoard oth, int curShip) {
this.gbCur = cur;
this.gbOth = oth;
this.curPlacement = curShip;
title = "Battleship! " + cur.getName() + ", place your ships";
setTitle(title);
setBounds(100, 100, 518, 389);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
//Stuff added to Content Pane
ImagePanel leftBoard = new ImagePanel("graphicsAssets/boardGraphic.png");
leftBoard.setBounds(6, 6, 250, 250);
getContentPane().add(leftBoard);
//More stuff added to Content Pane
contentPane.setLayout(null);
}
我承认我的方法有点不合常规,但我正在尝试在并非为 swing 设计的代码的限制范围内工作(并且必须在大多数情况下保持完整)。任何帮助将不胜感激!
编辑
我已经修改了我的代码以模仿 Vince 的代码以及以下功能:
public static void update(/*New Values*/) {
frame.remove(pp);
secondPanel = new SecondPanel(/*New Values*/);
frame.add(pp, "secondPanel");
switchPanel("secondPanel");
不幸的是,在调用 update 时,框架不会从原始 MainMenu 框架切换。有什么想法吗?
【问题讨论】:
-
你的非传统方法行不通。为您的游戏创建proper Swing view,然后将现有代码移动到模型中。换句话说,model - view - controller pattern.
标签: java swing jframe initialization