如何让这个 JButton 工作?
How to get this JButton to work?
我在 JButtons 上遇到了一些严重的问题。我的问题是无论我尝试什么,JButton 都不会出现。我已经尝试了一个多小时的所有方法,但没有任何效果,所以我认为是时候在这里提问了。这是我的代码。它只是构造函数,因为整个 class 真的很大,你不需要看到它。
public Game() {
frame = new JFrame(NAME);
canvas = new Canvas();
canvas.setMinimumSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setPreferredSize(new Dimension(width, height));
canvas.setFocusable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(width, height);
frame.add(canvas, BorderLayout.CENTER);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
frame.setFocusable(true);
frame.requestFocus();
JPanel panel = new JPanel();
JButton play = new JButton("Hi");
panel.add(play);
frame.getContentPane().add(panel);
panel.setPreferredSize(new Dimension(width, height));
}
主要问题:
frame.pack();
应在添加所有组件后完成。
frame.setVisible(true);
最好放在所有添加、打包和配置之后。
import java.awt.*;
import javax.swing.*;
public class Game {
private JFrame frame = null;
private Canvas canvas = null;
private String NAME = "Moronically Named Game";
int width = 500;
int height = 200;
public Game() {
frame = new JFrame(NAME);
canvas = new Canvas();
canvas.setMinimumSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setPreferredSize(new Dimension(width, height));
canvas.setFocusable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(width, height);
frame.add(canvas, BorderLayout.CENTER);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setFocusable(true);
frame.requestFocus();
JPanel panel = new JPanel();
JButton play = new JButton("Hi");
panel.add(play);
frame.getContentPane().add(panel);
panel.setPreferredSize(new Dimension(width, height));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
Game o = new Game();
}
};
SwingUtilities.invokeLater(r);
}
}
其他提示:
- 为了尽快获得更好的帮助,post Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example。例如。如我的回答所示。你post在问题中编辑了一个不可编译的代码片段,我把它变成了一个 MCVE 作为答案。
- 参见 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是。)
- 如技巧 (2) 中所述,使用填充布局 GUI。提供最小尺寸的 GUI 预期 布局的 ASCII 艺术或简单绘图,如果可调整大小,则提供更大的宽度和高度。
根据您提供的代码,我相信 canvas = new Canvas()
组件覆盖了整个 JFrame,因为您已将其尺寸设置为与 jframe 相同,这使得其他组件看起来没有显示。
- 我建议你在组合 swing(轻量级) 和 awt(重量级) 组件时要更加小心,请看这个 Mixing heavyweight and lightweight components
我在 JButtons 上遇到了一些严重的问题。我的问题是无论我尝试什么,JButton 都不会出现。我已经尝试了一个多小时的所有方法,但没有任何效果,所以我认为是时候在这里提问了。这是我的代码。它只是构造函数,因为整个 class 真的很大,你不需要看到它。
public Game() {
frame = new JFrame(NAME);
canvas = new Canvas();
canvas.setMinimumSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setPreferredSize(new Dimension(width, height));
canvas.setFocusable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(width, height);
frame.add(canvas, BorderLayout.CENTER);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
frame.setFocusable(true);
frame.requestFocus();
JPanel panel = new JPanel();
JButton play = new JButton("Hi");
panel.add(play);
frame.getContentPane().add(panel);
panel.setPreferredSize(new Dimension(width, height));
}
主要问题:
frame.pack();
应在添加所有组件后完成。frame.setVisible(true);
最好放在所有添加、打包和配置之后。
import java.awt.*;
import javax.swing.*;
public class Game {
private JFrame frame = null;
private Canvas canvas = null;
private String NAME = "Moronically Named Game";
int width = 500;
int height = 200;
public Game() {
frame = new JFrame(NAME);
canvas = new Canvas();
canvas.setMinimumSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setPreferredSize(new Dimension(width, height));
canvas.setFocusable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setSize(width, height);
frame.add(canvas, BorderLayout.CENTER);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setFocusable(true);
frame.requestFocus();
JPanel panel = new JPanel();
JButton play = new JButton("Hi");
panel.add(play);
frame.getContentPane().add(panel);
panel.setPreferredSize(new Dimension(width, height));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
Game o = new Game();
}
};
SwingUtilities.invokeLater(r);
}
}
其他提示:
- 为了尽快获得更好的帮助,post Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example。例如。如我的回答所示。你post在问题中编辑了一个不可编译的代码片段,我把它变成了一个 MCVE 作为答案。
- 参见 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是。)
- 如技巧 (2) 中所述,使用填充布局 GUI。提供最小尺寸的 GUI 预期 布局的 ASCII 艺术或简单绘图,如果可调整大小,则提供更大的宽度和高度。
根据您提供的代码,我相信 canvas = new Canvas()
组件覆盖了整个 JFrame,因为您已将其尺寸设置为与 jframe 相同,这使得其他组件看起来没有显示。
- 我建议你在组合 swing(轻量级) 和 awt(重量级) 组件时要更加小心,请看这个 Mixing heavyweight and lightweight components