我想在程序运行时修改JPanel的大小,例如使用菜单项。我该怎么办?或者我应该如何修改我的程序以访问内容窗格中的实时JPanel?
修改 如果我为默认情况下开始时为400x400的游戏区域制作内容窗格。但是,如果我想在菜单中添加一个选项,将大小更改为500x500,但不会丢失播放器中正在进行的所有内容,该怎么办呢。
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:
package me.test;
import java.awt.Dimension;
import javax.swing.JPanel;
public class MyJPanel extends JPanel {
public MyJPanel() {
setPreferredSize(new Dimension(400,400));
}
}
答案 0 :(得分:2)
查看此代码以获取一些想法。它可以根据放置在组件本身的工具栏中的操作或框架的菜单项来更改大小。
代码的重点是:
pack()
顶级容器。
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);
}
}
答案 1 :(得分:0)
玩耍......
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class PanelResize extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new PanelResize();
}
private JPanel panel;
public PanelResize() {
panel = new MyJPanel();
getContentPane().add(panel);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Resize");
JMenuItem menuItem = new JMenuItem("Resize");
menuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
panel.setPreferredSize(new Dimension(500,500));
pack();
}
});
menu.add(menuItem);
menuBar.add(menu);
this.setJMenuBar(menuBar);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(100,50);
setResizable(false);
setVisible(true);
}
}
class MyJPanel extends JPanel {
public MyJPanel() {
setPreferredSize(new Dimension(400,400));
}
}