我是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) {}
}
的MainMenu:
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 ......
GameDriver:
//Stuff Happens
ProgramShell.jf.setVisible(false);
ProgramShell.jf = new PlacementWindow(currentPlayer, otherPlayer, curShip);
ProgramShell.jf.setVisible(true);
我得到一个没有所有组件的空白JFrame(当我使用PlacementWindow的main进行初始化时它是正确创建的)
PlacementWindow:
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);
}
我认识到我的方法有点不同寻常,但我正在尝试在没有为摇摆设计的代码的限制内工作(并且必须保留,在大多数情况下,完好无损)。任何帮助将不胜感激!
修改
我修改了我的代码以模仿Vince和以下函数:
public static void update(/*New Values*/) {
frame.remove(pp);
secondPanel = new SecondPanel(/*New Values*/);
frame.add(pp, "secondPanel");
switchPanel("secondPanel");
不幸的是,在调用update时,框架不会从原始MainMenu框架切换。任何想法?
答案 0 :(得分:1)
不要初始化新帧。如果要从一个面板切换到另一个面板,请将框架的布局设置为CardLayout,然后添加JPanel。
class MyFrame {
static CardLayout cl = new CardLayout(); //handles panel switching
static JFrame frame; //init swing on EDT
static FirstPanel firstPanel;
static SecondPanel secondPanel;
public static void main(String[] args) {
initFrame();
}
public static void initFrame() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame = new JFrame();
frame.setDefaultCloseOperation(3);
frame.setLayout(cl);
firstPanel = new FirstPanel();
secondPanel = new SecondPanel();
//first panel added to frame will always show first
frame.add(firstPanel, "first");
frame.add(secondPanel, "second");
frame.pack(); //sizes frame to fit the panel being shown
setVisible(true);
}
});
}
public static void switchPanel(String name) {
cl.show(frame.getContentPane(), name);
frame.pack();
}
}
class FirstPanel extends JPanel { }
class SecondPanel extends JPanel { }
您可以使用面板,如果需要,可以切换/处理/创建面板而不是整个框架。
但是,对于游戏,他们不使用面板方法。游戏使用1个面板,而是。如果这对你没有意义,我用简单的游戏设计回答了另一个问题:JFrame, JPanel, KeyListener questions
我也建议调查游戏州。对于游戏来说,这是另一个大问题。它允许您根据游戏的状态(菜单,游戏,暂停等等)进行渲染/更新。