使用三个面板构建了一个小型测试SWING应用程序。一个主要的JPanel,包含两个较小的JPanel。一个在左侧(板面板),另一个在右侧(侧面板)。通过调用min,preferred和max size方法将两个内部面板设置为请求的特定大小。右侧面板中的按钮将父JFrame设置为不可重新调整大小。一切看起来都很棒,直到父框架设置为不可调整大小,这会导致(?!)框架和主面板的调整大小变大。
寻找调整大小的原因,以及可以采取哪些措施来保持主面板和框架的大小仅包围两个内部面板,而不是更多,一旦框架被冻结。
突出显示了面板边框并添加了绘制事件记录以调试调整大小问题。
感谢您的帮助和见解!
编辑: 这个问题出现在JRE 8中。在JRE 9中它消失了,但引入了扩展问题。整个应用程序在我的4k显示器上放大,运行Windows 10。
package org.fubar;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PanelTest {
private static final String appTitle = "JPanel Test by Fubar.org(c)";
public static JFrame myFrame = null;
public static JPanel mainPanel = null;
public static JPanel boardPanel = null;
public static JPanel sidePanel = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
System.out.println("Create and show GUI");
JFrame myFrame = new JFrame(appTitle);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new MainPanel();
myFrame.add(mainPanel);
myFrame.pack();
centreWindow(myFrame);
myFrame.setVisible(true);
}
public static void centreWindow(Window frame) {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
}
public static void barfSize(JPanel panel, String panelName) {
Dimension panelSize = panel.getSize();
System.out.println(String.format("%s size:(%d,%d);",panelName,panelSize.width,panelSize.height));
}
}
class MainPanel extends JPanel {
static final long serialVersionUID = 1;
public MainPanel() {
setBorder(BorderFactory.createLineBorder(Color.green));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
PanelTest.boardPanel = new BoardPanel();
PanelTest.boardPanel.setAlignmentY(TOP_ALIGNMENT);
add(PanelTest.boardPanel);
PanelTest.sidePanel = new SidePanel();
PanelTest.sidePanel.setAlignmentY(TOP_ALIGNMENT);
add(PanelTest.sidePanel);
}
public void paintComponent(Graphics gooey) {
super.paintComponent(gooey);
PanelTest.barfSize(this, "Main Panel");
}
}
class BoardPanel extends JPanel {
static final long serialVersionUID = 1;
public BoardPanel() {
setBorder(BorderFactory.createLineBorder(Color.red));
Dimension mySize = new Dimension(400,200);
setMinimumSize(mySize);
setPreferredSize(mySize);
setMaximumSize(mySize);
JLabel lblText = new JLabel("Board Panel");
add(lblText);
}
public void paintComponent(Graphics gooey) {
super.paintComponent(gooey);
PanelTest.barfSize(this, "Board Panel");
}
}
class SidePanel extends JPanel implements ActionListener {
static final long serialVersionUID = 1;
public SidePanel() {
setBorder(BorderFactory.createLineBorder(Color.blue));
Dimension mySize = new Dimension(200,200);
setMinimumSize(mySize);
setPreferredSize(mySize);
setMaximumSize(mySize);
JLabel lblText = new JLabel("Side Panel");
add(lblText);
JButton btnFreeze = new JButton("Freeze");
btnFreeze.setActionCommand("Freeze");
btnFreeze.setToolTipText("Click me to freeze frame");
btnFreeze.setBorderPainted(true);
btnFreeze.setEnabled(true);
btnFreeze.addActionListener(this);
add(btnFreeze);
}
public void actionPerformed(ActionEvent action) {
String command = action.getActionCommand();
if ( command == "Freeze") {
freezeFrame();
System.out.println("Froze frame.");
}
Dimension panelSize = getSize();
System.out.println(String.format("Side Panel size:(%d,%d);",panelSize.width,panelSize.height));
}
public void paintComponent(Graphics gooey) {
super.paintComponent(gooey);
PanelTest.barfSize(this, "Side Panel");
}
public void freezeFrame() {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
Dimension parentSize = this.getParent().getSize();
topFrame.setMaximumSize(parentSize);
topFrame.setResizable(false);
}
}
屏幕上限1:
App before frame resize disabled
屏幕截图2:
App after frame resize disabled
运行日志
Create and show GUI
Main Panel size:(602,202);
Side Panel size:(200,200);
Board Panel size:(400,200);
Main Panel size:(602,202);
Side Panel size:(200,200);
Board Panel size:(400,200);
Froze frame.
Side Panel size:(200,200);
Main Panel size:(618,218);
Side Panel size:(200,200);
Board Panel size:(400,200);
Main Panel size:(618,218);
Side Panel size:(200,200);
Board Panel size:(400,200);
答案 0 :(得分:0)
如果在首次创建时将setFesizable(false)设置为JFrame,则不会出现问题。
JFrame myFrame = new JFrame(appTitle);
myFrame.setResizable(false);