我开始使用WindowsBuilder构建琐事游戏。首先,将窗口屏幕链接在一起并放置一些按钮和菜单。除了菜单栏中的“主菜单”按钮以外,所有按钮似乎都可以正常工作。它应该带您再次进入初始页面,即启动器中的页面。但是,它只是显示一个空白框。从“难点”窗口的菜单栏访问时为什么不加载原始数据的任何想法?
这是Launcher
类
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class Launcher extends JFrame{
private JFrame frmLibraryTrivaCenter;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Launcher window = new Launcher();
window.frmLibraryTrivaCenter.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Launcher() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmLibraryTrivaCenter = new JFrame();
frmLibraryTrivaCenter.setTitle("Main Menu");
frmLibraryTrivaCenter.setBounds(200, 200, 450, 300);
frmLibraryTrivaCenter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmLibraryTrivaCenter.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Welcome to the Library Trivia Center!");
lblNewLabel.setBounds(53, 5, 328, 24);
lblNewLabel.setForeground(Color.BLACK);
lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 20));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
frmLibraryTrivaCenter.getContentPane().add(lblNewLabel);
JButton btnNewGame = new JButton("New Game");
btnNewGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Difficulty difficulty = new Difficulty();
difficulty.setLocation( 200, 200 );
difficulty.setSize( 450, 300 );
difficulty.setVisible(true);
frmLibraryTrivaCenter.dispose();
}
});
btnNewGame.setBounds(169, 84, 95, 23);
frmLibraryTrivaCenter.getContentPane().add(btnNewGame);
JButton btnHowToPlay = new JButton("How To Play");
btnHowToPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
HowToPlay howTo = new HowToPlay();
howTo.setLocation( 250, 150 );
howTo.setSize( 450, 300 );
howTo.setVisible(true);
}
});
btnHowToPlay.setBounds(162, 119, 110, 23);
frmLibraryTrivaCenter.getContentPane().add(btnHowToPlay);
JButton btnQuitGame = new JButton("Quit Game");
btnQuitGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frmLibraryTrivaCenter.dispose();
}
});
btnQuitGame.setBounds(169, 154, 95, 23);
frmLibraryTrivaCenter.getContentPane().add(btnQuitGame);
}
}
这是Difficulty
类
import java.awt.Font;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
@SuppressWarnings("serial")
public class Difficulty extends JFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Difficulty window = new Difficulty();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Difficulty() {
setTitle("Difficulty Selection");
getContentPane().setLayout(null);
JLabel lblDifficulty = new JLabel("What grade are you in?");
lblDifficulty.setFont(new Font("Arial", Font.BOLD, 12));
lblDifficulty.setBounds(24, 54, 147, 24);
getContentPane().add(lblDifficulty);
JButton btnEasy = new JButton("1st Grade");
btnEasy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
TopicSelect topic = new TopicSelect();
topic.setLocation( 200, 200 );
topic.setSize( 450, 300 );
topic.setVisible(true);
dispose();
}
});
btnEasy.setFont(new Font("Arial", Font.BOLD, 12));
btnEasy.setBounds(247, 90, 108, 39);
getContentPane().add(btnEasy);
JButton btnMedium = new JButton("Kindergarten");
btnMedium.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TopicSelect topic = new TopicSelect();
topic.setLocation( 200, 200 );
topic.setSize( 450, 300 );
topic.setVisible(true);
dispose();
}
});
btnMedium.setFont(new Font("Arial", Font.BOLD, 12));
btnMedium.setBounds(105, 90, 108, 39);
getContentPane().add(btnMedium);
JButton btnHard = new JButton("2nd Grade");
btnHard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TopicSelect topic = new TopicSelect();
topic.setLocation( 200, 200 );
topic.setSize( 450, 300 );
topic.setVisible(true);
dispose();
}
});
btnHard.setFont(new Font("Arial", Font.BOLD, 12));
btnHard.setBounds(105, 141, 108, 38);
getContentPane().add(btnHard);
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 434, 23);
getContentPane().add(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmMainMenu = new JMenuItem("Main Menu");
mntmMainMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Launcher launch = new Launcher();
launch.setLocation( 200, 200 );
launch.setSize( 450, 300 );
launch.setVisible(true);
dispose();
}
});
mnFile.add(mntmMainMenu);
JMenuItem mntmQuit = new JMenuItem("Quit");
mntmQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
mnFile.add(mntmQuit);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
JMenuItem mntmHowToPlay = new JMenuItem("How to Play");
mntmHowToPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
HowToPlay howTo = new HowToPlay();
howTo.setLocation( 250, 150 );
howTo.setSize( 450, 300 );
howTo.setVisible(true);
}
});
mnHelp.add(mntmHowToPlay);
JButton btnrdGrade = new JButton("3rd Grade");
btnrdGrade.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TopicSelect topic = new TopicSelect();
topic.setLocation( 200, 200 );
topic.setSize( 450, 300 );
topic.setVisible(true);
dispose();
}
});
btnrdGrade.setFont(new Font("Arial", Font.BOLD, 12));
btnrdGrade.setBounds(247, 142, 108, 39);
getContentPane().add(btnrdGrade);
JButton btnthGrade = new JButton("4th Grade");
btnthGrade.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TopicSelect topic = new TopicSelect();
topic.setLocation( 200, 200 );
topic.setSize( 450, 300 );
topic.setVisible(true);
dispose();
}
});
btnthGrade.setFont(new Font("Arial", Font.BOLD, 12));
btnthGrade.setBounds(105, 191, 108, 39);
getContentPane().add(btnthGrade);
JButton btnthGrade_1 = new JButton("5th Grade");
btnthGrade_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TopicSelect topic = new TopicSelect();
topic.setLocation( 200, 200 );
topic.setSize( 450, 300 );
topic.setVisible(true);
dispose();
}
});
btnthGrade_1.setFont(new Font("Arial", Font.BOLD, 12));
btnthGrade_1.setBounds(247, 192, 108, 39);
getContentPane().add(btnthGrade_1);
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(200, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}