我在JFrame上绘制JLabel时遇到问题。我已经在另一个项目中完成了它并且它正常工作,但这次我搞砸了某个地方而且不能再画了。这是我的代码:
板:
import javax.swing.*;
import java.awt.*;
public class Board extends JPanel {
public Board() {
initBoard();
}
private void initBoard() {
setPreferredSize(new Dimension(Frame.GAME_WIDTH, Frame.GAME_HEIGHT));
setMinimumSize(new Dimension(Frame.GAME_WIDTH, Frame.GAME_HEIGHT));
setMaximumSize(new Dimension(Frame.GAME_WIDTH, Frame.GAME_HEIGHT));
setBackground(Color.GRAY);
setDoubleBuffered(true);
}
}
框:
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class Frame extends JFrame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
private final String title = "title here";
private Image backgroundIMG;
public Frame() {
initUI();
}
private void initUI() {
add(new Board());
pack();
setTitle(title);
setSize(GAME_WIDTH, GAME_HEIGHT);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setLayout(null);
/*
* set background image
*/
/*try {
this.backgroundIMG = ImageIO.read(new File("src/to/image"));
} catch (IOException e) {
e.printStackTrace();
}*/
Border emptyBorder = BorderFactory.createEmptyBorder();
int titleWidth = 270;
int titleHeight = 55;
int titleX = 24;
int titleY = 30;
int titleSize = 47;
String titleFont = "Ravie";
/*
* Customize the startscreen
*/
JLabel title = new JLabel("text");
title.setBounds(0, 0, titleWidth, titleHeight);
title.setFont(new Font(titleFont, Font.BOLD, titleSize));
title.setForeground(new Color(251,102,8));
title.setLocation(titleX, titleY);
add(title);
System.out.println("title should be printed");
}
}
发射器:
import java.awt.*;
public class Launcher {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Frame main = new Frame();
main.setVisible(true);
});
}
}
当我启动程序时,框架正在加载,但不显示JLabel。它的印刷品也应该打印出#34;在控制台上。我已经做了一些研究,但是找不到任何帮助我的东西。也许这只是一个微不足道的错误,有人可以快速帮助我。
提前致谢
答案 0 :(得分:1)
过去所有的,有趣的"暂时的问题,你的基本问题归结为......
add(new Board());
//...
add(title);
Java / Swing以LIFO顺序绘制它的组件,因此添加的最后一个组件是绘制的第一个组件。
最合理的解决方法可能是将title
添加到Board
,但您可能希望先解决其他几个问题......
JFrame
等顶级容器延伸。很多原因,但大多数情况下,您并没有在课程中添加新功能,而是将您锁定在一个可通过其他方式/组件更好地管理的用例中setPreferred/Minimum/MaximumSize
。这些更麻烦,然后它们是值得的。相反,根据需要,覆盖getPreferredSize
setDoubleBuffered(true);
毫无意义,因为默认情况下Swing组件是双缓冲的null
布局,严重的是,这是人们在SO上发布的大多数问题的首要原因。布局管理API是有原因的,学会利用它。如果我们考虑到所有这些,你可能最终会更喜欢......
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
private final String title = "title here";
public Test() {
EventQueue.invokeLater(() -> {
JFrame main = new JFrame(title);
main.add(new MainPane());
main.pack();
main.setLocationRelativeTo(null);
main.setVisible(true);
});
}
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
public static class MainPane extends JPanel {
private Image backgroundIMG;
public MainPane() {
setLayout(new BorderLayout());
setBackground(Color.GRAY);
add(new Board());
String titleFont = "Ravie";
int titleSize = 47;
JLabel title = new JLabel("text");
title.setHorizontalAlignment(JLabel.CENTER);
title.setFont(new Font(titleFont, Font.BOLD, titleSize));
title.setForeground(new Color(251, 102, 8));
add(title, BorderLayout.NORTH);
}
}
public static class Board extends JPanel {
public Board() {
initBoard();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(GAME_WIDTH, GAME_HEIGHT);
}
private void initBoard() {
setBackground(Color.GRAY);
}
}
}