首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GUI编程-定制框架

GUI编程-定制框架
EN

Stack Overflow用户
提问于 2020-04-17 11:04:00
回答 1查看 25关注 0票数 0

我刚刚开始使用GUI编程。我正在尝试自定义框架,但到目前为止,当我将它添加到gameView中时,没有任何东西出现,比如按钮或cat gif,所以在我运行它之后,我得到的只是框架窗口。到目前为止,我的代码如下:

代码语言:javascript
复制
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

    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-17 11:11:15

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

代码语言:javascript
复制
mainFrame frame = new mainFrame();

还可以从main中删除以下代码:

代码语言:javascript
复制
JFrame frame = new JFrame();
frame.setVisible(true);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61263468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档