我有它在容器中三个组成部分和按钮。 当我击最小化按钮部件被最小化到容器的底部,并且当我打最小化的部件然后它获取最大化。
假设三个组件都趴在底部,如果我最大限度的第二个组件然后它得到最大化和最小化的第三组件不采取第二的位置,这仍然是空间。
截图
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);
}
}