我有四个内部框架和三个按钮。当我点击最大化按钮时,它会最大化,但会覆盖所有框架。但我的意思是应该显示最小化的框架。
请查看下面的代码。
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.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;
public class Test {
public Test() throws HeadlessException, PropertyVetoException {
createAndShowGUI();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new Test();
} catch (HeadlessException ex) {
//Logger.getLogger(MinPanel1.class.getName()).log(Level.SEVERE, null, ex);
} catch (PropertyVetoException ex) {
// Logger.getLogger(MinPanel1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
private void createAndShowGUI() throws HeadlessException, PropertyVetoException {
JFrame frame = new JFrame();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final JDesktopPane jdp = new JDesktopPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
frame.setContentPane(jdp);
frame.pack();
createAndAddInternalFrame(jdp, 0, 0);
createAndAddInternalFrame(jdp, 200, 0);
createAndAddInternalFrame(jdp, 400, 0);
createAndAddInternalFrame(jdp, 600, 0);
frame.setVisible(true);
}
private void createAndAddInternalFrame(final JDesktopPane jdp, int x, int y) throws PropertyVetoException {
final JInternalFrame jInternalFrame = new JInternalFrame("Test1", true, true, true, true);
jInternalFrame.setLocation(x, y);
final JInternalFrame jInternalFrame1 = new JInternalFrame("Test2", true, true, true, true);
JPanel jp= new JPanel();
JButton jb1 = new JButton("min");
JButton jb2 = new JButton("[]");
JButton jb3 = new JButton("X");
jInternalFrame.setLayout(new GridLayout(2, 2,2,2));
jInternalFrame1.add(jb1);
jInternalFrame.setSize(200, 200);//testing
jInternalFrame.setLayout(new GridLayout(2,2));
JButton jb= new JButton("min");
// jInternalFrame.add(jb);
// jInternalFrame.add(jb3);
//jInternalFrame.add(jb2);
jp.add(jb);
jp.add(jb2);
jp.add(jb3);
jInternalFrame.add(jp);
jb.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae) {
try {
jInternalFrame.setIcon(true);
} catch (PropertyVetoException ex) {
}
}
});
jb1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae) {
try {
jInternalFrame.setIcon(true);
} catch (PropertyVetoException ex) {
}
}
});
jb2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae) {
try {
jInternalFrame.setMaximum(true);
}
catch(Exception e)
{
}
}
});jb3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae) {
try {
jInternalFrame.dispose();
} catch (Exception ex) {
}
}
});
BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
jInternalFrame.remove(titlePane);
jInternalFrame.setVisible(true);
jInternalFrame1.setVisible(true);
jdp.add(jInternalFrame);
//jdp.add(jInternalFrame1);
}
}