编译并运行,但它不存在

时间:2011-10-29 18:53:17

标签: java swing user-interface package jframe

我正在尝试从包文件夹中运行我的JFrame(它已正常工作并已正确设置)。问题是它编译并运行但我看不到任何东西,因为没有错误我不知道要修复什么。我可能忽略了一些非常小的东西,但我无法指出它。这段代码是我的Main类和我将使用的第一个JFrame类。关于如何最有效地从包中实现JFrame的任何想法都将非常感激。

import GroupProject.GUI.Package.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;


public class TestGuiApp1
{

    public static void main(String[] Args)
    {
      // Gets screen dimensions to be used to center JFrame
      Toolkit tk = Toolkit.getDefaultToolkit();
      Dimension d = tk.getScreenSize();

      //creates new Main Menu Frame from GUI packages
      new MainMenuFrame();

      //constraints and unlocking/locking features
      //setLocation((d.width/2)-350, (d.height/2)-350);
      //setResizable(false);


    }

}

// 从包

开始JFrame类
    package GroupProject.GUI.Package;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import com.sun.java.swing.plaf.nimbus.*;

    public class MainMenuFrame extends JFrame
    {
        private JButton guess_word,
                        guess_number,
                        player_stats;
        private JLabel pic_label = new JLabel(new ImageIcon("Question-Mark-Man.jpg"));

        public MainMenuFrame()
        {     

            try {

                UIManager.setLookAndFeel(new NimbusLookAndFeel()
                {
                    public UIDefaults getDefaults()
                    { 
                        UIDefaults ret = super.getDefaults();
                        ret.put("defaultFont",
                        new Font(Font.MONOSPACED, Font.BOLD, 16));
                        return ret;
                }
                });

            JPanel p1 = new JPanel();
            JPanel p2 = new JPanel();

            ButtonListener listener = new ButtonListener();
            // Panel Size
            setPreferredSize(new Dimension(550, 300));
            // Background Color
            setBackground(new Color(127, 157, 217));
            p1.setBackground(new Color(127, 157, 217));


            // -------------------- Buttons -------------------

            // Guess a word Button
            guess_word = new JButton("Guess a word", new ImageIcon("word game.png"));
            guess_word.setFont(new Font("ariel narrow",Font.BOLD,24));
            guess_word.addActionListener(listener);
            p1.add(guess_word);

            // Guess a number Button
            guess_number = new JButton("Guess a number", new ImageIcon("number game.png"));
            guess_number.setFont(new Font("ariel narrow",Font.BOLD,24));
            guess_number.addActionListener(listener);
            p1.add(guess_number);

            // View player stats button
            player_stats = new JButton("Player Stats", new ImageIcon("stats2.png"));
            player_stats.setFont(new Font("ariel narrow",Font.BOLD,24));
            player_stats.addActionListener(listener);
            p1.add(player_stats);
            // ---------------------------------------------------



            // ============ Layouts using group layout ============
            GroupLayout layout = new GroupLayout(this);

            this.setLayout(layout);
             layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                    .addComponent(guess_word, GroupLayout.PREFERRED_SIZE, 280,
                          GroupLayout.PREFERRED_SIZE)
                    .addComponent(guess_number, GroupLayout.PREFERRED_SIZE, 280,
                          GroupLayout.PREFERRED_SIZE)
                    .addComponent(player_stats, GroupLayout.PREFERRED_SIZE, 280,
                          GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(p2, GroupLayout.DEFAULT_SIZE,
                          GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()));

            layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                    .addComponent(p2, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 
                              GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                    .addComponent(guess_word, GroupLayout.PREFERRED_SIZE, 60,
                         GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(guess_number, GroupLayout.PREFERRED_SIZE, 60,
                          GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(player_stats, GroupLayout.PREFERRED_SIZE, 60,
                          GroupLayout.PREFERRED_SIZE)))

                    .addContainerGap(239, Short.MAX_VALUE)));
            // ===================================================
            p2.add(pic_label);
            } catch (Exception ex)
            {
                throw new Error(ex);
            }
        }  


        private class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {   
               //need to set values to remember what game the user wants to play
               // before it goes to the SelectPlayerTypeFrame
               if (e.getSource() == guess_word)
                   ;//new MainMenu();

               if (e.getSource() == guess_number)
                   ; // new MainMenu();

               if (e.getSource() == player_stats)
               ;//new MainMenu(); 
            } 
        }

    }

2 个答案:

答案 0 :(得分:4)

你在哪里调用JFrame上的setVisible(true)?

即,

myFrame.setVisible(true);

或在你的情况下:

this.setVisible(true); // this is not necessary but here for clarity

另请注意,应在将所有组件添加到GUI并调用pack后调用此方法(如本页另一个答案中所述)。

您将需要阅读Java Swing教程,了解如何创建Swing程序,因为这些知识对您非常有用:Swing Tutorials

另外,我强烈建议您不要使用NetBeans为您生成Swing代码,至少在游戏的这个阶段不会,直到您更多地了解Swing。代码生成器非常适合快速和脏代码创建,但如果您不了解Swing的细节,如果您想创建除最基本的GUI以外的任何东西,通常会遇到困难。我从经验中知道这一点。

答案 1 :(得分:4)

您永远不会将框架设置为可见,并且您不会在其上调用pack()来告诉它对其子组件的大小。

JFrame frame = new MainMenuFrame();
frame.pack();
frame.setVisible(true);