简体   繁体   English

在swing GUI中,如何在用户单击按钮时将JPanel显示为最大化?

[英]In swing GUI how to show a JPanel as maximized when the user clicks a button?

I want to maximize a JPanel inside a JFrame when the user clicks a button.What is the best way to achieve this.The view and the data model should be in sync in both the panels,that is the panel which in the JFrame and the maximized one.Please suggest me some solution. 当用户点击按钮时,我希望在JFrame中最大化JPanel。实现此目的的最佳方法是什么。视图和数据模型应该在两个面板中同步,即JFrame中的面板和最大化一个。请给我一些解决方案。

my requirement is: i have a JFrame with 4 JPanels named as 我的要求是:我有一个名为的4个JPAnel的JFrame

  • JPanelA,JPanelB,JPanelC,JPanelD JPanelA,JPanelB,JPanelC,JPanelD
  • Here the JPanelD contains a JList and a button below it say "MAXIMIZE PANEL" button . 这里JPanelD包含一个JList,下面的按钮说“MAXIMIZE PANEL”按钮。 JList has a JTree with in it . JList里面有一个JTree。 Sometimes the JList may have huge set of data and it is not visible to the user clearly. 有时,JList可能具有大量数据,并且用户无法清楚地看到它。

So he need to maximize this JPanelD alone to see the contents of the JList clearly.For that he clicks "MAXIMIZE PANEL" button.After the click action ,the JPanelD in the JFrame remains there,also a new JPanel with the same JList data(ie.,the replica of the JPanelD say JPanelDMaximized)should be popped up.This is what i want to do .. 所以他需要单独最大化这个JPanelD才能清楚地看到JList的内容。为此他点击“MAXIMIZE PANEL”按钮。点击动作后,JFrame中的JPanelD仍然存在,也是一个具有相同JList数据的新JPanel(应该弹出JPanelD的复制品,说JPanelDMaximized。这就是我想要做的......

Of course you could do this yourself, but you should really look at JInternalFrame and consider using that for your panel. 当然你可以自己做,但你应该看看JInternalFrame并考虑将它用于你的面板。 It will save a bunch of headache. 它会省去一堆头痛。

Edit: Sun's tutorial should get you what you need. 编辑:Sun的教程应该能满足您的需求。

  1. Create a JWindow (or an undecorated JFrame) with a JPanel. 使用JPanel创建JWindow(或未修饰的JFrame)。 Leave the JWindow invisible, initially. 最初让JWindow不可见。 (The wiring of this new JPanel to the same data model used by the original JPanel is left as an exercise.) (这个新JPanel与原始JPanel使用的相同数据模型的连线留作练习。)

  2. When your maximize-panel button's ActionListener executes, it must: 当您的最大化面板按钮的ActionListener执行时,它必须:

    2.1. 2.1。 Update the (invisible) JWindow's location and size to match the (visible) JFrame's. 更新(不可见)JWindow的位置和大小以匹配(可见)JFrame。

    2.2. 2.2。 Make your JFrame invisible. 使您的JFrame不可见。

    2.3. 2.3。 Make your JWindow visible. 让你的JWindow可见。

  3. When your unmaximize-panel button's ActionListener executes, it must: 当你的unmaximize-panel按钮的ActionListener执行时,它必须:

    3.1. 3.1。 Update the (invisible) JFrame's location and size to match the (visible) JWindow's. 更新(不可见)JFrame的位置和大小以匹配(可见)JWindow。

    3.2. 3.2。 Make your JWindow invisible. 让你的JWindow隐形。

    3.3. 3.3。 Make your JFrame visible 使您的JFrame可见

Example: 例:

package stackoverflow;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MaximizingPanelApp extends JFrame {

private JPanel framePanel;
private JPanel windowPanel;
private JFrame maximizedFrame;

public static void main(String[] args) {
    JFrame appFrame = new MaximizingPanelApp();
    appFrame.setVisible( true );
}

public MaximizingPanelApp() throws HeadlessException {
    super( "Application" );
    setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    initialize();
}

private void initialize() {
    // JFrame
    {
        Container container = getContentPane();
        container.setLayout( new BorderLayout() );

        framePanel = new JPanel();
        framePanel.setBackground( Color.ORANGE );
        container.add( framePanel, BorderLayout.CENTER );

        JButton button = new JButton( new MaximizeAction() );
        container.add( button, BorderLayout.SOUTH );
        setSize( 400, 300 );
    }

    // JWindow
    {
        maximizedFrame = new JFrame();
        Container container = maximizedFrame.getContentPane();
        container.setLayout( new BorderLayout() );

        windowPanel = new JPanel();
        windowPanel.setBackground( Color.ORANGE );
        container.add( windowPanel, BorderLayout.CENTER );

        JButton button = new JButton( new UnMaximizeAction() );
        container.add( button, BorderLayout.SOUTH );
        maximizedFrame.setSize( getSize() );
        maximizedFrame.setUndecorated( true );
    }

}

private class MaximizeAction extends AbstractAction {

    private MaximizeAction() {
        super( "Maximize" );
    }

    public void actionPerformed(ActionEvent e) {
        maximizedFrame.setSize( getSize() );
        maximizedFrame.setLocation( getLocation() );
        setVisible( false );
        maximizedFrame.setVisible( true );
    }
}

private class UnMaximizeAction extends AbstractAction {

    private UnMaximizeAction() {
        super( "Un-Maximize" );
    }

    public void actionPerformed(ActionEvent e) {
        setLocation( maximizedFrame.getLocation() );
        setSize( maximizedFrame.getSize() );
        maximizedFrame.setVisible( false );
        maximizedFrame.dispose();
        setVisible( true );
    }
}
}

Follow-up to your clarification of the problem: 您对问题的澄清的后续行动:

Take my code, and remove: 拿我的代码,然后删除:

maximizedFrame.setUndecorated( true );

and size the frame bigger before you make it visible. 在使框架可见之前调整框架的尺寸。 That should satisfy the maximize-like behaviour you need. 这应该满足您所需的最大化行为。

Your other problem is that you cannot add JPanelD to the two JFrames. 您的另一个问题是您无法将JPanelD添加到两个JFrame中。 The pop-up frame must have its own unique JPanel object (let's call it JPanelE ). 弹出框架必须有自己独特的JPanel对象(让我们称之为JPanelE )。 So you need to: 所以你需要:

  1. Initialize and lay out JPanelE like you do JPanelD . JPanelD一样初始化和布局JPanelE That means giving JPanelE its own JList (and JTree, and so on). 这意味着为JPanelE提供自己的JList(和JTree等)。
  2. Share the ListModel from JPanelD's JList with JPanelE's JList, and so on. 使用JPanelE的JList从JPanelD的JList共享ListModel,依此类推。 The feasibility and details of executing this successfully depends on the specifics of your implementation, and is beyond the scope of your original problem. 成功执行此操作的可行性和详细信息取决于您的实现细节,超出了原始问题的范围。

This depends on the layout manager you use. 这取决于您使用的布局管理器。 If you add a JPanel to a JFrame using the default layout manager, and the JFrame only contains the JPanel and nothing else, you'll achieve what you describe. 如果使用默认布局管理器将JPanel添加到JFrame,并且JFrame仅包含JPanel而不包含任何其他内容,那么您将实现所描述的内容。

Here's an example. 这是一个例子。 The JPanel is green; JPanel是绿色的; notice how it resizes as you resize the JFrame. 注意在调整JFrame大小时它如何调整大小。

import javax.swing.*;
import java.awt.*;

public class ScratchSpace {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Stretchy panel demo");
        final JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(Color.GREEN);
        panel.setPreferredSize(new Dimension(600, 400));
        final JComponent contentPane = (JComponent) frame.getContentPane();
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM