【发布时间】:2011-12-17 23:08:24
【问题描述】:
我正在尝试从包文件夹中运行我的 JFrames(该文件夹有效且已正确设置)。问题是它可以编译并运行,但我什么都看不到,而且我不知道要修复什么,因为没有错误。我可能忽略了一些非常小的东西,但我无法确定它。这段代码是我的 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();
}
}
}
【问题讨论】:
-
啊,我发现 GroupLayout 使用了 this 关键字,需要 getContentPane() 代替。
标签: java swing user-interface package jframe