gpt4 book ai didi

java - 如何动态调整 JFrame 的大小以包含具有不同内容大小的 JPanel?

转载 作者:行者123 更新时间:2023-12-01 22:38:28 25 4
gpt4 key购买 nike

如何使我的 JFrame 与保存动态大小内容的 JPanel 的大小相匹配?

我创建了两个简单的类片段,如果有人可以指导我完成它们。

类1:CanvasExample.java

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CanvasExample {
public static void main(String[] args) {
JPanelEx dataOut = new JPanelEx();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("Title!");
f.setMinimumSize(new Dimension(200,100));
f.getContentPane().add(dataOut);
f.pack();
f.setVisible(true);
}
});
}
}

类2:JPanelEx.java

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JPanelEx extends JPanel {
public JPanelEx() {
super(new FlowLayout());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello1", 10, 10);
g.drawString("Hello2", 10, 30);
g.drawString("Hello3", 10, 50);
g.drawString("Hello4", 10, 70);
g.drawString("Hello5", 10, 90);
g.drawString("Hello6", 10, 110);
g.drawString("Hello7", 10, 130);
g.drawString("Hello8", 10, 150);
g.drawString("Hello9", 10, 170);
g.drawString("Hello10", 10, 190);
g.dispose();
}
}

当前输出:

Current Output

预期输出:

Expected Output

我知道类似的问题可能已经被问过很多次了,但我浏览了一下,但找不到明确的解决方案。任何帮助表示赞赏。

最佳答案

您的JPanelEx将需要提供大小提示,以允许布局管理系统确定组件的最佳布局方式以及内容所需的空间量

此外,请勿对您未创建的 Graphics 上下文调用 dispose!

举个例子...

enter image description here

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JPanelEx());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class JPanelEx extends JPanel {

private int numElements = 10;

public JPanelEx() {
super(new FlowLayout());
}

@Override
public Dimension getPreferredSize() {

FontMetrics fm = getFontMetrics(getFont());
int width = 0;
int height = fm.getHeight() * numElements;
for (int index = 0; index < numElements; index++) {
width = Math.max(width, fm.stringWidth("Hello" + index));
}

return new Dimension(width, height);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 0;
int y = 0;
FontMetrics fm = g.getFontMetrics();
for (int index = 0; index < numElements; index++) {
g.drawString("Hello" + index, x, y + fm.getAscent());
y += fm.getHeight();
}
}
}

}

另请参阅Laying Out Components Within a Container有关布局管理器 API 的更多详细信息

关于java - 如何动态调整 JFrame 的大小以包含具有不同内容大小的 JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26538726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com