如果我对 JPanel 和 JFrame 进行子类化,为什么我的 JFrame 仍为空?
我正在尝试为我的 Java 应用程序编写自定义 JFrame 和 JPanel。目前,我只想在屏幕中间有一个带有启动按钮的 JPanel。所以,这是我的代码:
package gui;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class SubitizingFrame extends JFrame implements KeyListener {
public SubitizingFrame() {
super("Subitizing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
add(new LaunchPanel());
pack();
setVisible(true);
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_F5)
System.out.println("F5 pressed");
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
这是我的面板:
package gui;
import instructions.Settings;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class LaunchPanel extends JPanel implements ActionListener {
private JButton startButton;
public LaunchPanel() {
int width = Settings.getScreenSizeX(), height = Settings.getScreenSizeY();
setPreferredSize(new Dimension(width, height));
setLayout(null);
startButton = new JButton("Start");
startButton.setLocation((width/2) - (startButton.getWidth()/2), (height/2) - (startButton.getHeight()/2));
add(startButton);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
但是当应用程序启动时,我看不到任何东西。只是一个大的灰色屏幕。
I'm trying to write custom JFrame and JPanel for my Java application. Currently, I just want to have a JPanel with a start button in the very middle of the screen. So, here's the code I have:
package gui;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class SubitizingFrame extends JFrame implements KeyListener {
public SubitizingFrame() {
super("Subitizing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
add(new LaunchPanel());
pack();
setVisible(true);
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_F5)
System.out.println("F5 pressed");
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
and here is my panel:
package gui;
import instructions.Settings;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class LaunchPanel extends JPanel implements ActionListener {
private JButton startButton;
public LaunchPanel() {
int width = Settings.getScreenSizeX(), height = Settings.getScreenSizeY();
setPreferredSize(new Dimension(width, height));
setLayout(null);
startButton = new JButton("Start");
startButton.setLocation((width/2) - (startButton.getWidth()/2), (height/2) - (startButton.getHeight()/2));
add(startButton);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
But when the application launches, I don't see anything. Just a big gray screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要使用空布局。如果您只是使用
JPanel
的默认布局管理器(即FlowLayout
),则具有“自动”功能的JButton
会被放置在中心。另外,为了将JFrame
放置在屏幕中间,请调用setLocationRelativeTo(null)
。由于很难说出“屏幕”的含义,因此此示例展示了如何将
JFrame
中的JPanel
中的JButton
居中,即然后以监视器为中心。Do not use a null layout. If you simply use the default layout manager of
JPanel
(i.e.FlowLayout
), theJButton
with "automagically" be placed in the center. Also, in order to place theJFrame
in the middle of the screen, invokesetLocationRelativeTo(null)
.Since it's hard to tell what you mean by "screen", this example shows how you center a
JButton
in aJPanel
in aJFrame
, that is then centered on the monitor.建议:
Recommendations:
如果您不使用任何 LayoutManager (顺便说一句您可能应该使用),那么您需要 设置面板的大小(及其位置)。
来自: http://download.oracle.com/javase/tutorial/uiswing /layout/using.html
If you don't use any LayoutManager (which btw you probably should), then you'll need to set the size of the panel as well (along with its position).
From: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html
不要使用 KeyListener。 Swing 被设计为与键绑定一起使用。阅读 Swing 教程中有关如何使用键绑定的部分了解更多信息。
本教程还有一个关于
使用布局管理器
的部分,您应该阅读。您不应该创建具有空布局的 GUI。Don't use KeyListeners. Swing was designed to be used with Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings for more information.
The tutorial also has a section on
Using Layout Manager
which you should read. You should not create GUI's with a null layout.