添加时 JButtons 不出现
JButtons not appearing when added
Jbutton 不会显示在屏幕上,如果我尝试从主框架添加,我会得到一个空指针异常。
我已经按照其他示例和问题的步骤进行操作,但是 none 似乎显示了 JButton,我试图将它直接添加到主 JFrame(这是我尝试添加它的地方) Display.getFrame().add(buttons.get(id));但出于某种原因,这只是给了我一个空指针异常
这是我的显示器class
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();
}
这是我正在尝试添加按钮的 class,按钮管理器被我的菜单之一调用 classes
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 会显示在屏幕上,但这并没有发生,最烦人的事情是如果我将 add() 添加到 Display 中的主 JFrame class.
编辑:
另一个主要问题是 canvas 在按钮上呈现,所以 none 会显示
在您的主框架 class Display
中,您似乎没有添加面板实例,即 ButtonManager
class。或者是在此处未包含的另一个单独的 class 中完成的?
ButtonManager 从不初始化 Display
,因此:
display.getFrame().add(buttons.get(id));
将 NPE 显示为空。
ButtonManager 似乎不需要对 Display 的引用。
在另一个层面上,将按钮添加到框架与 Canvas 对象冲突。
我写了这个主要方法,它显示了 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() {
}
}
显示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);
}
}
输出:
Jbutton 不会显示在屏幕上,如果我尝试从主框架添加,我会得到一个空指针异常。
我已经按照其他示例和问题的步骤进行操作,但是 none 似乎显示了 JButton,我试图将它直接添加到主 JFrame(这是我尝试添加它的地方) Display.getFrame().add(buttons.get(id));但出于某种原因,这只是给了我一个空指针异常
这是我的显示器class
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();
}
这是我正在尝试添加按钮的 class,按钮管理器被我的菜单之一调用 classes
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 会显示在屏幕上,但这并没有发生,最烦人的事情是如果我将 add() 添加到 Display 中的主 JFrame class.
编辑: 另一个主要问题是 canvas 在按钮上呈现,所以 none 会显示
在您的主框架 class Display
中,您似乎没有添加面板实例,即 ButtonManager
class。或者是在此处未包含的另一个单独的 class 中完成的?
ButtonManager 从不初始化 Display
,因此:
display.getFrame().add(buttons.get(id));
将 NPE 显示为空。
ButtonManager 似乎不需要对 Display 的引用。
在另一个层面上,将按钮添加到框架与 Canvas 对象冲突。
我写了这个主要方法,它显示了 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() {
}
}
显示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);
}
}
输出: