Java:如何在运行时修改JPanel的大小? [英] Java: How can I modify the size of a JPanel in runtime?
问题描述
我想在程序运行时修改JPanel的大小,例如使用菜单项.我该如何到达?还是应该修改程序以获取内容窗格中实时JPanel的权限?
I want to modify the size of the JPanel while the program is running, for example with a menu item. How can I reach it? Or how should I modify my program to get acces to the live JPanel in the content pane?
修改: 如果我在开始时为一个400x400的游戏场创建内容窗格,则默认情况下.但是,如果我想在菜单中添加一个选项以将尺寸更改为500x500,而又不丢失播放器已经在进行的所有内容该怎么办?
If I make a contentpane for a gamefield that's 400x400 in the start as default. But what if I want to ad an option in the menu to change the size to 500x500, but without losing all of the content already in progress by the player.
JFrame类:
package me.test;
import javax.swing.JFrame;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
public Main() {
setContentPane(new MyJPanel());
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(100,50);
setResizable(false);
setVisible(true);
}
}
我修改过的JPanel:
My modified JPanel:
package me.test;
import java.awt.Dimension;
import javax.swing.JPanel;
public class MyJPanel extends JPanel {
public MyJPanel() {
setPreferredSize(new Dimension(400,400));
}
}
推荐答案
请看一下这段代码中的一些想法.它可以根据放置在组件本身的工具栏中或框架菜单项中的动作来更改大小.
Have a look over this code for some ideas. It can change sizes according to actions placed either in a toolbar in the component itself, or menu items of the frame.
该代码的重要要点是:
- 在相关组件上设置首选尺寸.
- 获取对顶级容器的引用.
- 最后
pack()
个顶级容器.
- Set a preferred size on the component in question.
- Get a reference to the top level container.
- Finally
pack()
the top level container.
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class GameSize {
// the GUI as seen by the user (without frame)
private JPanel gui = new JPanel(new GridBagLayout());
Action small = new AbstractAction("Small") {
@Override
public void actionPerformed(ActionEvent e) {
setSize(400, 100);
}
};
Action large = new AbstractAction("Large") {
@Override
public void actionPerformed(ActionEvent e) {
setSize(600, 200);
}
};
private final void setSize(int w, int h) {
gui.setPreferredSize(new Dimension(w, h));
Container c = gui.getTopLevelAncestor();
if (c instanceof JFrame) {
JFrame f = (JFrame) c;
f.pack();
}
}
GameSize() {
JToolBar tb = new JToolBar("Size");
for (Action action : getActions()) {
tb.add(action);
}
gui.add(tb);
gui.setPreferredSize(new Dimension(200, 100));
gui.setBackground(Color.RED);
}
/*
* @return the Actions
*/
public Action[] getActions() {
Action[] actions = {small, large};
return actions;
}
/**
* @return the gui
*/
public JPanel getGui() {
return gui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
GameSize gs = new GameSize();
JFrame f = new JFrame("Demo");
f.add(gs.getGui());
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
JMenuBar menuBar = new JMenuBar();
f.setJMenuBar(menuBar);
JMenu size = new JMenu("Size");
menuBar.add(size);
for (Action action : gs.getActions()) {
size.add(action);
}
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
这篇关于Java:如何在运行时修改JPanel的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!