gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:37 28 4
gpt4 key购买 nike

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

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

截图
enter image description here

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);
}
}

最佳答案

我已经用 Metal 和 Windows L&F 测试了这个,你可能需要用其他一些测试它。

基本上,当组件无效并调用 doLayout 方法时,我们检查是否存在任何 JInternalFrame.JDesktopIcon 组件。然后我们把它们拿出来,按照我们喜欢的方式布置它们……

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();

}
}
}
}

别搞错了,这是一个粗略的 hack

已更新

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);

}
}

你真的需要花时间学习How to use Internal FramesHow to use Layered Panes因为(至少最后一部分)包含在这些...

关于java - 如何移动放置在容器底部的最小化组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14889369/

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