java - GUI编程-定制框架

标签 java user-interface addition frame gif

我刚刚开始使用 GUI 编程。我正在尝试自定义框架,但到目前为止,当我将其添加到 gameView 时,什么也没有出现,例如按钮或猫 gif,所以我在运行它后得到的只是框架窗口。到目前为止,这是我的代码:

package experiment10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class mainFrame extends JFrame 
{
    public static int MAX = 500;    // maximum number to guess
    private JButton submitBtn;      // Submit button
    private JButton clearBtn;  // Clear Input button
    private JTextField inputNumber;  //input guessed number
    private int magicNumber;  //randomly generated number

    public static void main(String[] arg)    {
        //Show frame
        JFrame frame = new JFrame();
        frame.setVisible(true);
    }

    public mainFrame()  {

        setSize(400, 400);
        setResizable (false);
        setTitle("Let's Play Hi Lo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        magicNumber = (int)(Math.random()*MAX);

        Container gameView  = getContentPane();
        gameView.setLayout(new FlowLayout());
        //Customizations
        submitBtn = new JButton("Submit"); //create submit button
        clearBtn = new JButton("Clear"); //create clear button
        gameView.add(submitBtn); //adds submit button
        gameView.add(clearBtn); //adds clear button

        JLabel imageLabel = new JLabel(new ImageIcon("cat.gif")); //creates image
        gameView.add(imageLabel); //adds image

        JLabel textLabel = new JLabel("Please enter your guess"); //creates JLabel
        gameView.add(textLabel); //adds JLabel

        JTextField input = new JTextField(); //creates JTextField
        gameView.add(input); //adds JTextField

    }
}

非常感谢任何帮助/提示。谢谢。

最佳答案

您需要从 main 方法中调用 mainFrame() :

mainFrame frame = new mainFrame();

您还可以从主文件中删除以下代码:

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

然后,您需要将 setVisible(true); 添加到 mainFrame() 构造函数中。

关于java - GUI编程-定制框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61263468/

相关文章:

java - JFrame Eclipse 'add' 命令错误

java - 使用 JPA 持久保存表单数据

java - 写入文件时如何考虑JSON结构

java - 集合的通用类型子集合

c# - 使用 WIA 控制相机

css - 手写笔 – 添加百分比

java - 如何在 Java Graphics 中正确渲染颜色

android - 将不同颜色的边框添加到android View 的顶部和底部边缘的方法

user-interface - 声明式与编程式 UI(对象实例、XAML、MXML、XUL 等)

c# - 如何创建一个仅使用加法和 2 个变量进行除法的递归函数