【问题标题】:Java Button not appearing on (GUI)Java 按钮未出现在 (GUI)
【发布时间】:2018-06-13 17:09:30
【问题描述】:

我一直在寻找一种很好的方法来为一个比以前更大的项目打下良好的基础。如果我在 main 中编写所有内容,它就可以正常工作。当做这样的类时,框架可以工作,但我添加的按钮不想出现:

//主要

package taxsystem;

import java.awt.*;
import javax.swing.*;

public class Taxmain
{
    public mainFrame mf;
    public Interface gui;

    public void startApplication()
    {
        mf = new mainFrame();
        mf.startApp();
        gui = new Interface();
        gui.makeLayout();
    }

    public static void main(String[] args)
    {
        Taxmain tm = new Taxmain();
        tm.startApplication();
    }
}

//实际的Frame

package taxsystem;


import java.awt.*;
import javax.swing.*;

public class mainFrame extends JFrame
{
    public void startApp()
    {
        setResizable(false);
        setVisible(true);
        setSize(720,340);
        setLocation(0,0);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBackground(Color.WHITE);

        setTitle("Tax Handler");
    }
}

//布局(我创建的按钮没有出现)

package taxsystem;


import java.awt.*;
import javax.swing.*;

public class Interface extends JPanel
{
    Taxmain mc;
    public JButton testButton;

    public void makeLayout()
    {
        testButton = new JButton();
        testButton.setText("Printer");

        testButton.setFont(new Font("verdana", Font.ITALIC, 16));
        testButton.setForeground(Color.BLACK);

        testButton.setFocusable(false);
        testButton.setSize(new Dimension(150, 40));
        testButton.setLocation(10, 10);
        this.setLayout(null);
        this.add(testButton);   
    }
}

目前看起来是这样的:https://gyazo.com/fad5dbca6c59905faea0a8ac1fbd424a

在此先感谢,还有什么我可以改进到目前为止的代码吗?

【问题讨论】:

    标签: java class user-interface interface frame


    【解决方案1】:

    您需要将 JPanel 添加到您的 JFrame

    public void startApplication()
    {
        mf = new mainFrame();
        mf.startApp();
        gui = new Interface();
        gui.makeLayout();
        mf.add(gui); // here is get's added
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多