gpt4 book ai didi

Java:如何在运行时修改 JPanel 的大小?

转载 作者:行者123 更新时间:2023-12-02 06:15:17 26 4
gpt4 key购买 nike

我想在程序运行时修改 JPanel 的大小,例如使用菜单项。我怎样才能达到它?或者我应该如何修改我的程序才能访问内容 Pane 中的实时 JPanel?

编辑:如果我为游戏场创建一个默认大小为 400x400 的内容 Pane 。但是,如果我想在菜单中添加一个选项以将大小更改为 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));
}
}

最佳答案

查看此代码以获取一些想法。它可以根据组件本身的工具栏中或框架的菜单项中放置的操作来更改大小。

代码的要点是:

  • 为相关组件设置首选尺寸。
  • 获取对顶级容器的引用。
  • 最后pack()顶级容器。

enter image description here

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 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21555968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com