gpt4 book ai didi

java - 编译并运行但不存在

转载 作者:行者123 更新时间:2023-12-01 07:13:49 25 4
gpt4 key购买 nike

我正在尝试从包文件夹运行我的 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();
}
}

}

最佳答案

您从未将框架设置为可见,也不会对其调用 pack() 来告诉其调整其子组件的大小。

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

关于java - 编译并运行但不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7940960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com