Java Swing Pack方法无法按预期工作
我正在尝试使用Java秋千创建GUI,并使用pack()
方法遇到问题。如您所见,我创建并将所有元素添加到createTeteText()
方法和createboxes()
方法中的框架中。只有在调用这两种方法之后,我才会调用pack()
方法,但是当我运行程序时,我会得到一个小的GUI,并最小化按钮并最小化按钮。但是,如果我扩展它,它会向我展示适当的布局。
我在网上发现的唯一问题的原因是,在将所有元素添加到框架中之前,我调用pack()
,但事实并非如此。我该如何解决?
顺便说一句,如果不清楚,我正在尝试不使用框架上的setSize()
方法,并使其大小适合添加到它的元素的内容。
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
private LetterBox[][] boxes;
public Frame() {
boxes = new LetterBox[6][5];
// frame styling
setTitle("My Frame");
getContentPane().setBackground(new Color(0x121213));
createTitleText();
createBoxes();
// boilerplate frame configuration
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}
public void createTitleText() {
JLabel label = new JLabel("Test");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.TOP);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.TOP);
label.setBounds(0, 0, 400, 100);
label.setForeground(Color.WHITE);
label.setFont(new Font("Arial", Font.BOLD, 36));
add(label);
}
public void createBoxes() {
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 5; c++) {
LetterBox box = new LetterBox();
boxes[r][c] = box;
box.setLetter(r + " " + c);
add(box.getBox()); // box.getBox() returns a JLabel
}
}
}
public static void main(String[] args) {
new Frame();
}
}
I am trying to create a GUI using Java Swing, and am having a problem using the pack()
method. As you can see, I create and add all of the elements to the frame in the createTitleText()
method and the createBoxes()
method. Only after those two methods are called do I call the pack()
method, yet when I run the program, I get a small GUI with the close and minimize buttons. But if I expand it, it shows me the proper layout.
The only cause for such a problem that I have found online would be that I call pack()
before adding all of my elements to the frame, but that is not the case. How can I fix this?
By the way, if it wasn't clear, I am trying to not use the setSize()
method on the frame, and make it so that the frame's size fits the contents of the elements added to it.
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
private LetterBox[][] boxes;
public Frame() {
boxes = new LetterBox[6][5];
// frame styling
setTitle("My Frame");
getContentPane().setBackground(new Color(0x121213));
createTitleText();
createBoxes();
// boilerplate frame configuration
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}
public void createTitleText() {
JLabel label = new JLabel("Test");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.TOP);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.TOP);
label.setBounds(0, 0, 400, 100);
label.setForeground(Color.WHITE);
label.setFont(new Font("Arial", Font.BOLD, 36));
add(label);
}
public void createBoxes() {
for (int r = 0; r < 6; r++) {
for (int c = 0; c < 5; c++) {
LetterBox box = new LetterBox();
boxes[r][c] = box;
box.setLetter(r + " " + c);
add(box.getBox()); // box.getBox() returns a JLabel
}
}
}
public static void main(String[] args) {
new Frame();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论