我没有完全使应用程序正常运行。但是直到现在,在我编译并运行它之后,什么都没有出现。
(稍后会有一个名为“ UnitCircle”的类。我希望用户单击菜单将面板切换到该游戏面板)
public class TrigCrashCourse extends JFrame implements ActionListener
{
//labels for main page
private JFrame frame;
private CardLayout cardlayout;
private Container c; // contains the cards for different games
//for menus
private JMenu menu;
private JMenuBar menubar;
private JMenuItem item1;
private JMenuItem item2;
private JMenuItem item3;
//for labels
private JLabel label1;
private JLabel label2;
private JLabel label3;
public static void main(String[]args)
{
TrigCrashCourse tcc = new TrigCrashCourse();
tcc.runIt();
}
public void runIt()
{
frame = new JFrame("Trig Crash Course");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(10,20);
frame.setResizable(true);
frame.setVisible(true);
//card layouts
c = getContentPane();
cardlayout = new CardLayout();
c.setLayout(cardlayout);
UnitCircle uc = new UnitCircle();
c.add("Unit Circle Game",uc);
//menus
item1 = new JMenuItem("Unit Circle");
item2 = new JMenuItem("Pythagorean theorem");
item3 = new JMenuItem("Graphing");
menu = new JMenu("Choose a game");
menubar = new JMenuBar();
menu.add(item1);
menu.add(item2);
menu.add(item3);
menubar.add(menu);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
c.add(menubar);
//labels
setLayout(new FlowLayout());
label1 = new JLabel("Trig Crash Course");
label2 = new JLabel("-Alg2 Trig");
label3 = new JLabel("You have 3 games to choose from");
label1.setFont(new Font("serif",Font.BOLD,20));
label2.setFont(new Font("serif",Font.BOLD,15));
label3.setFont(new Font("serif",Font.BOLD,15));
setLayout(null);
c.add(label1);
c.add(label2);
c.add(label3);
label1.setBounds(150,20,300,80);
label2.setBounds(250,70,150,30);
label3.setBounds(100,180,400,20);
}
public void actionPerformed(ActionEvent evt)
{
String getGame = evt.getActionCommand();
if( getGame == "Unit Circle")
cardlayout.show(c,"Unit Circle Game");
}
我至少要显示菜单/组合框和标签,而不是空白框。...