[英]Resize JPanel from JFrame
I want to create a little game in Java using Netbeans.我想使用 Netbeans 在 Java 中创建一个小游戏。 For now I have a JFrame and two JPanels.
现在我有一个 JFrame 和两个 JPanel。
The JFrame contains both JPanels and a button. JFrame 包含 JPanel 和一个按钮。 My intent is to click on this button and resize one of the JPanels (from 0 to >0 width).
我的意图是单击此按钮并调整其中一个 JPanel 的大小(从 0 到 >0 宽度)。
Till now I menaged to resize the frame but I can't figure out how to resize the JPanel.直到现在我设法调整框架的大小,但我不知道如何调整 JPanel 的大小。
This is what I've done so far:这是我到目前为止所做的:
Structure
frame
|_ panel 1
|_ panel 2
|_ button
__________________
| _ _ |
| | | | | _|
| | | | | | |
| | | | | |>|
| | | | | |_|
| |_| |_| |
|__________________|
on click should expand frame and panel
______________________
| _ _____ |
| | | | | _|
| | | | | | |
| | | | | |>| ->
| | | | | |_|
| |_| |_____| |
|______________________|
This is the JPanel to resize这是要调整大小的 JPanel
public class ToResize extends javax.swing.JPanel {
...
public void resize(int width) {
this.setSize(new Dimension(this.getWidth() + width, this.getHeight()));
}
}
This is the JFrame with the button这是带按钮的 JFrame
public class MyFrame extends javax.swing.JFrame {
...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (panelToResize.getWidth() == 0) {
panelToResize.resize(100);
} else {
panelToResize.resize(-100);
}
validate();
}
}
1) if you'll resize JFrame too, then you have to call 1)如果你也要调整 JFrame 的大小,那么你必须打电话
a/ setPrefferedSize(getPrefferedSize()+-)
for JFrame.pack();
a/
setPrefferedSize(getPrefferedSize()+-)
for JFrame.pack();
b/ setPrefferedSize(getPrefferedSize()+-)
for panelToResize
and then call JFrame.pack();
b/
setPrefferedSize(getPrefferedSize()+-)
用于panelToResize
然后调用JFrame.pack();
2/ if you only change size betweens JPanels
and JFrame
size stays remained, then you have to call revalidate()
plus repaint()
to the panelToResize
, 2/如果你只改变
JPanels
和JFrame
之间的大小,那么你必须调用revalidate()
加上repaint()
到panelToResize
,
3/ but everything depends of used LayoutManager 3/ 但一切都取决于使用的LayoutManager
My intent is to click on this button and resize one of the JPanels (from 0 to >0 width).
我的意图是单击此按钮并调整其中一个 JPanel 的大小(从 0 到 >0 宽度)。
Use a CardLayout
, or a JSplitPane
, or call panel.setVisible(boolean)
.使用
CardLayout
或JSplitPane
,或调用panel.setVisible(boolean)
。
I would do that same thing in a different way.我会以不同的方式做同样的事情。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
panelToResize.setVisible(!panelToResize.isVisible());
if(panelToResize.isVisible()) {
jButton1.setText("<<");
} else {
jButton1.setText(">>");
}
}
Why?为什么?
1 - Hidden panel still work just like a visible one. 1 - 隐藏面板仍然像可见面板一样工作。
2 - Need to know rather the panel is extended or not? 2 - 需要知道面板是否扩展?
panelToResize.isVisible();
3 - The layout on your JFrame still works as intended. 3 - JFrame 上的布局仍按预期工作。
Change setSize()
to setPreferredSize()
将
setSize()
更改为setPreferredSize()
You can just use:您可以使用:
mainPanel.setResizeHorizontal(true);
mainPanel.setResizeVertical(true)
and your problem will be solved.你的问题就会得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.