我想從包文件夾(它的工作原理和設置正確)運行我的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使用了這個關鍵字,並且需要getContentPane()來代替。 – aStackofQueues