当前位置: 首页 > 知识库问答 >
问题:

如何移动放置在容器底部的最小化组件?

须原
2023-03-14

我在一个容器中有三个组件和按钮。当我点击最小化按钮时,组件会最小化到容器的底部,当我点击最小化组件时,它会最大化。

假设三个组件位于底部,如果我最大化第二个组件,那么它就会最大化,而第三个最小化组件不会占据第二个组件的位置,这仍然是空间。

package Project;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class Test2 {

    public Test2() throws HeadlessException, PropertyVetoException {
        createAndShowGUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new Test2();
                } catch (HeadlessException ex) {
                    ex.printStackTrace();
                } catch (PropertyVetoException ex) {
                    ex.printStackTrace();
                }

            }
        });
    }

    private void createAndShowGUI() throws HeadlessException, PropertyVetoException {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JDesktopPane jdp = new JDesktopPane() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(600, 400);
            }
        };

        frame.setContentPane(jdp);
        frame.pack();

        createAndAddInternalFrame(jdp, 0, 0);
        createAndAddInternalFrame(jdp, 300, 0);
        createAndAddInternalFrame(jdp, 1, 200);

        frame.setVisible(true);
    }

    private void createAndAddInternalFrame(final JDesktopPane jdp, int x, int y) throws PropertyVetoException {
        final JInternalFrame jInternalFrame = new JInternalFrame("", true, true, true, true);
        jInternalFrame.setLocation(x, y);

        JPanel jp = new JPanel();
        JLabel jl=new JLabel("panel"+x);

        JButton jb = new JButton("_");
        JButton jb2 = new JButton("[]");
        JButton jb3 = new JButton("X");

        jInternalFrame.setLayout(new GridLayout(2, 2));
jp.add(jl);
        jp.add(jb);
        jp.add(jb2);
        jp.add(jb3);

        jInternalFrame.add(jp);

        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (jInternalFrame.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
                        jdp.remove(jInternalFrame);
                        jdp.add(jInternalFrame, JDesktopPane.DEFAULT_LAYER);
                        jdp.revalidate();
                        jdp.repaint();
                    }
                    jInternalFrame.pack();
                    jInternalFrame.setIcon(true);
                } catch (PropertyVetoException ex) {
                    ex.printStackTrace();
                }

            }
        });
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (jInternalFrame.isMaximum()) {//restore
                        jInternalFrame.pack();
                    } else {//maximize
                        jInternalFrame.setMaximum(true);
                    }
                    jdp.remove(jInternalFrame);
                    jdp.add(jInternalFrame, JDesktopPane.FRAME_CONTENT_LAYER);
                    jdp.revalidate();
                    jdp.repaint();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    jInternalFrame.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        });

        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
        jInternalFrame.remove(titlePane);

        jInternalFrame.pack();
        jInternalFrame.setVisible(true);
        jdp.repaint();

        jdp.add(jInternalFrame);
    }
}

共有2个答案

伯庆
2023-03-14

类似这样的:

jdp.setDesktopManager( new DefaultDesktopManager(){
    @Override
    public void deiconifyFrame(JInternalFrame f) {
        super.deiconifyFrame(f);
        JDesktopPane d = f.getDesktopPane();
         JInternalFrame[] frames = d.getAllFrames();
         for(JInternalFrame frame : frames ) {
             Rectangle bounds = getBoundsForIconOf(frame);
              // relayout all frames
         }
    }
});
萧业
2023-03-14

我已经用金属和Windows L测试过了

基本上,当组件失效并调用 doLayout 方法时,我们会检查是否存在任何 J 内部框架.J桌面图标组件。然后,我们将这些并按照自己的喜好布置它们...

public class TestInternalFrame {

    public static void main(String[] args) {
        new TestInternalFrame();
    }

    private int xpos = 0;
    private int ypos = 0;

    public TestInternalFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
                DesktopPane pane = new DesktopPane();
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());

                JFrame frame = new JFrame();
                frame.add(pane);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public JInternalFrame newInternalFrame() {
        JInternalFrame inf = new JInternalFrame("Blah", true, true, true, true);
        inf.setLocation(xpos, ypos);
        inf.setSize(100, 100);
        inf.setVisible(true);

        xpos += 50;
        ypos += 50;

        return inf;
    }

    public class DesktopPane extends JDesktopPane {

        @Override
        public void doLayout() {
            super.doLayout();
            List<Component> icons = new ArrayList<Component>(25);
            for (Component comp : getComponents()) {
                if (comp instanceof JInternalFrame.JDesktopIcon) {
                    icons.add(comp);
                }
            }

            int x = 0;
            for (Component icon : icons) {

                int y = getHeight() - icon.getHeight();
                icon.setLocation(x, y);
                x += icon.getWidth();

            }
        }
    }
}

别搞错了,这是一次粗暴的攻击

已更新

int x = 0;
for (Component icon : icons) {
    int y = getHeight() - icon.getHeight();
    icon.setLocation(x, y);
    x += icon.getWidth();
    setLayer(icon, 10); // <--- Add me
}

对于您的另一个问题,您只需要将图标移动到更高的层。这样做的问题是,你实际上需要找到一个足够高的层。你可以使用 Integer.MAX_VALUE,但这有点苛刻(你可能想要一些东西在上面),相反,你可以计算最大层并坐在1之上......

public void doLayout() {
    super.doLayout();
    List<Component> icons = new ArrayList<Component>(25);
    int maxLayer = 0;
    for (Component comp : getComponents()) {
        if (comp instanceof JInternalFrame.JDesktopIcon) {
            icons.add(comp);
            maxLayer = Math.max(getLayer(comp), maxLayer);
        }
    }

    maxLayer++;
    int x = 0;
    for (Component icon : icons) {

        int y = getHeight() - icon.getHeight();
        icon.setLocation(x, y);
        x += icon.getWidth();
        setLayer(icon, maxLayer);

    }
}

你真的需要花时间研究如何使用内部框架和如何使用分层窗格,因为(至少最后一部分)在这些内容中有所介绍。

 类似资料:
  • 问题内容: 我在容器中有三个组件,在其中有按钮。当我按下 最小化按钮时,组件将最小化到容器的底部, 当我按下最小化组件时,它将最大化。 假设三个组件位于底部,并且如果我最大化第二个 组件,则它会最大化,而第三个最小化组件不会占据 第二个组件的位置,并且仍保留为空白。 问题答案: 我已经在和上进行了测试,您可能需要与 其他一些测试。 基本上,当组件失效并 调用该doLayout方法时 ,我们将检查是

  • 问题内容: 鉴于以下HTML: 我想坚持到底。 不使用绝对定位就可以实现吗?如果float属性支持’bottom’的值,那似乎可以解决问题,但是不幸的是,事实并非如此。 问题答案: 可能不会。 分配给,然后分配给。

  • 问题内容: 我需要使用才能使用keyboard_actions,以便可以在iOS的键盘上方放置一个“完成”按钮(此刻使用数字键盘) 在将有一列作为一个孩子,然后一个按钮被放置在底部。我尝试过使用来将高度提高到。 我尝试将with与该属性一起使用,但是最终当键盘出现时,该小部件不会消失。 旁注:脚手架同时具有和设置为(默认值) 问题答案: 问题在于它是孩子。因此,要在两者之间使用自动尺寸小部件- 我

  • 我正在设计的网页布局像这样:http://jsfiddle.net/5sTub/ 编辑:注意:我并不是想要一个黏糊糊的页脚。我只想把它放在页面底部。回答之前请先看小提琴 我试图定位在页面底部的页脚,但无法像你在上面的链接中看到的那样。我尝试了在互联网上找到的一切,包括设置容器元素的位置:相对;页脚的

  • 在 一到三个 。有没有办法在后始终将单元格放置在屏幕底部?

  • 我有一个JPanel,在我点击一个按钮后,我希望图标垂直显示,一个在顶部,另一个在底部点击第二个图标,依此类推。 因此,每次点击时,图标位置会在顶部和底部之间交替变化。 我尝试了许多布局经理,但似乎无法让它像我希望的那样工作。 编辑:举例, 第一次点击后; 二次点击, 第三次点击,顶部的另一个图标,依此类推。这样做的目的是添加到我正在创建的国际象棋游戏中。所以我希望死棋子出现在适当的玩家端(黑色或