Jbutton将不会显示在屏幕上,如果我尝试从主框架中添加,则会得到空指针异常。
我遵循了其他示例和问题的步骤,但是似乎都没有显示JButton,我尝试使用Display.getFrame()。add()将其直接添加到主JFrame(这是我尝试添加的位置)中。 button.get(id));但这出于某种原因只给了我一个nullpointer异常
这是我的Display班级
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();
}
这是我要添加按钮的类,按钮管理器正由我的菜单类之一调用
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));
}
我原本希望Jbutton会显示在屏幕上,但那不会发生,最烦人的是如果我将Display()添加到Display类的主JFrame中,则为nullpointer。
在作为主框架的Display
类中,您似乎没有添加面板的实例,即ButtonManager
类。还是在另一个单独的类中完成了此操作?
ButtonManager从不初始化Display
,因此:display.getFrame().add(buttons.get(id));
将NPE显示为空。
似乎ButtonManager不需要对Display的引用。
在另一个级别上,将按钮添加到框架与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();
}
并且将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);
这里是您的解决方案。我仅整理了示例的基础知识,您可以根据需要继续执行该程序。
/* 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() {
}
}
显示类别:
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);
}
}
输出: