问题
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?
Edit: 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 class:
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);
}
}
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));
}
}
回答1:
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.
Important points of the code are:
- 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);
}
}
回答2:
Playing around....
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));
}
}
来源:https://stackoverflow.com/questions/21555968/java-how-can-i-modify-the-size-of-a-jpanel-in-runtime