English 中文(简体)
框架中面板的固定大小
原标题:Fixed size of panel in frame

我已经在设置为网格布局的 < code> JPannel 中添加了一些组件, 我将将其添加到设置为边框布局的 < code> JFrame 中。 但我想要修正面板的大小。 当我的窗口最大化时, 所有组件的大小都在增加。 我希望窗口的 < strong> 中心 上的面板能够设置为 < enger *em > fixed size , 即使窗口最大化 。

问题回答

将面板设为 GridLayout ,作为 GridBagLayout 的单元件,不设限制 -- -- 以该面板为中心。 将面板与GBL加到 Center 的《边界地标》 。

“https://i.sstatic.net/QsSEU.png/> https://i.sstatic.net/QsSEU.png/>

以上图像的这个例子 。


"https://stackoverflow.com/a/5630271/4185556" 也使用GBL将图像置于卷轴右侧的下方。

“https://i.sstatic.net/jaqap.png/> https://i.sstatic.net/jaqap.png/>

那么您就不应该使用BraotLayout, 因为这符合儿童组件。 如果您仍然想在 JFrame 上使用 BoderLayout (因为您需要一些侧面面面板或类似的东西), 您可以将您的喷嘴( 与 GridLayout 一起) 包成另一个配有 GredBagLayout 或 BoxLayout 或类似东西的热门, 然后将其他喷嘴放进 JFrame 。

JPanel innerPanel = new JPanel();
innerPanel.setLayout(new GridLayout());
// fill and set your innerPanel

JPanel middlePanel = new JPanel();
middlePanel.setLayout(new GridBagLayout());
middlePanel.add(innerPanel, constraintsThatPlaceItWhereYouWantIt);

JFrame yourFrame = new JFrame();
yourFrame.setLayout(new BorderLayout());
yourFrame.add(middlePanel, BorderLayout.CENTER);
    JFrame frame = new JFrame();
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout());

    JPanel rootPanel = new JPanel();
    frame.getContentPane().add(rootPanel, BorderLayout.CENTER);
    rootPanel.setLayout(new GridBagLayout());

    JPanel contentPanel = new JPanel();

    Dimension dimension = new Dimension(300, 300);
    contentPanel.setMaximumSize(dimension);
    contentPanel.setMinimumSize(dimension);
    contentPanel.setPreferredSize(dimension);
    contentPanel.setBackground(Color.YELLOW);

    GridBagConstraints g = new GridBagConstraints();
    g.gridx = 0;
    g.gridy = 0;
    g.anchor = GridBagConstraints.CENTER;
    rootPanel.add(contentPanel, g);

    frame.setVisible(true);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签