简体   繁体   English

添加时不显示 JButton

[英]JButtons not appearing when added

Jbutton will not display on screen, and if I try to add from main frame I get a null pointer exception. Jbutton 不会显示在屏幕上,如果我尝试从主框架添加,我会收到 null 指针异常。

I have followed steps from other examples and problems but none seem to display the JButton, I have tried to add it directly to the main JFrame (which is where I am trying to add it) with Display.getFrame().add(buttons.get(id));我已经按照其他示例和问题的步骤进行操作,但似乎没有显示 JButton,我尝试使用 Display.getFrame().add(buttons.得到(身份证)); but that just gives me a nullpointer exception for some reason但这只是出于某种原因给了我一个空指针异常

this is my Display class这是我的显示器 class

https://pastebin.com/Bq0VVL9v the method which I think might be why I cant add the buttons properly v https://pastebin.com/Bq0VVL9v我认为这可能是我无法正确添加按钮的原因 v

private void createDisplay() {

    frame = new JFrame(title);

    frame.setSize(width, height);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setResizable(false);

    frame.setLocationRelativeTo(null);

    frame.setVisible(true);

    canvas = new Canvas();

    canvas.setPreferredSize(new Dimension(width, height));

    canvas.setMaximumSize(new Dimension(width, height));

    canvas.setMinimumSize(new Dimension(width, height));

    canvas.setFocusable(false);
    
    frame.add(canvas);
    frame.pack();
}

and this is the class I am trying to add buttons with, the button manager which is being called by one of my menu classes这是我正在尝试添加按钮的 class,按钮管理器由我的一个菜单类调用

https://pastebin.com/bUMvnmFf https://pastebin.com/bUMvnmFf

public ButtonManager(Icon image,Icon image2, int positionX, int positionY,int width, int height, int id, String name) {

    buttons.add(id, new JButton(name, image));

    buttons.get(id).setRolloverIcon(image2);

    buttons.get(id).setLocation(positionX, positionY);

    buttons.get(id).setSize(width, height);

    System.out.println(buttons.get(0));

    add(buttons.get(id));
}

I would have expected the Jbutton to be displayed on screen but that does not happen, the most annoying thing is the nullpointer if I add() to the the main JFrame in Display class.我原以为 Jbutton 会显示在屏幕上,但这并没有发生,如果我 add() 到显示 class 中的主要 JFrame 中,最烦人的就是空指针。

EDIT: another major problem was the canvas was being rendered over the buttons, so none would show up编辑:另一个主要问题是 canvas 被呈现在按钮上,所以不会出现

ButtonManager never initializes Display , so: display.getFrame().add(buttons.get(id)); ButtonManager 从不初始化Display ,所以: display.getFrame().add(buttons.get(id)); will NPE as display is null.将 NPE 作为显示是 null。

It seems ButtonManager does not need the reference to Display.看来 ButtonManager 不需要对 Display 的引用。

On another level, adding the button to the frame conflicts with the Canvas object.在另一个层面上,将按钮添加到框架与 Canvas object 冲突。

I wrote this main method and it displays both the canvas and button:我编写了这个主要方法,它同时显示了 canvas 和按钮:

  public static void main(String[] args) {
    Display display = new Display("title", 200, 200);

    ButtonManager buttonManager = new ButtonManager(... , 0, 0, 100, 100, 0, "name");

    display.getFrame().getContentPane().add(buttonManager, BorderLayout.SOUTH);
    display.getFrame().pack();
  }

And the Canvas adding code was modified to (note the last two lines):并将 Canvas 添加代码修改为(注意最后两行):

    canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setMinimumSize(new Dimension(width, height));
    canvas.setFocusable(false);

    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(canvas, BorderLayout.NORTH);

In the class Display which is your main frame, it appears you did not add an instance of your panel, that is ButtonManager class.在作为主框架的 class Display中,您似乎没有添加面板实例,即ButtonManager class。 Or was that done in another separate class that was not included here?还是在此处未包含的另一个单独的 class 中完成?

Here is a solution for you.这是为您提供的解决方案。 I codified only the basics for the example, you can continue with the program as you need.我只编写了示例的基础知识,您可以根据需要继续使用该程序。

/* Button Manager Class */
package mainpackage.display;    

import java.util.ArrayList;    

import javax.swing.JButton;
import javax.swing.JPanel;    

public class ButtonManager extends JPanel {
    private static final long serialVersionUID = 1L;    

    public ArrayList<JButton> buttons = new ArrayList<JButton>();
    Display display;    

    public static JButton getJButton(String name) {
        //Put all your stuff of images and ids here
        JButton aButton = new JButton(name);
        return aButton;
    }    

    public static void putButton(JPanel panel, JButton button, int posX, int posY) {
        button.setLocation(posX, posY);
        panel.add(button);
    }    

    public void remove(int id) {
        buttons.remove(id);
        display.getFrame().remove(buttons.get(id));
    }    

    public void tick() {    

    }    

    public void render() {    

    }    

}

Display class:显示 class:

package mainpackage.display;
import java.awt.Canvas;    

import java.awt.Component;
import java.awt.Dimension;    

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;    

import static mainpackage.display.ButtonManager.*;

public class Display {

    private JFrame frame;
    private Canvas canvas;

    private String title;
    private int width, height;

    public Display(String title, int width, int height) {
        this.title = title;
        this.width = width;
        this.height = height;
        createDisplay();
    }

    private void createDisplay() {
        frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        canvas = new Canvas();

        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));
        canvas.setFocusable(false);
        frame.add(canvas);

        JPanel panel = new JPanel();
        //You can add many buttons as you need
        putButton(panel, getJButton("Button 1"), 100, 200);
        putButton(panel, getJButton("Button 2"), 800, 800);

        frame.setContentPane(panel);

        frame.pack();
    }

    public void postCreate(Component a) {
        frame.add(a);
    }
    public void postCreate(JButton j) {
        frame.add(j);
    }
    public Canvas getCanvas() {
        return canvas;
    }
    public JFrame getFrame() {
        return frame;
    }

    public static void main(String[] args) {
        Display display = new Display("Hello", 1000, 1000);
    }
}

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM