自己实在是个懒人,blog难得更新一次,更新也是一些鸡毛蒜皮的小东西。不过还是希望能对其他朋友或自己将来遇到类似问题能有个解答。最新在做一个swing项目,客户要求能把JInternalFrame的Title bar去掉,同时还能加回来。由于网上搜一下没有找到解决办法,只能自己研究一下并改了下JInternalFrame,先记录如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.peer.ComponentPeer;
import java.beans.PropertyVetoException;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.UIManager;
import javax.swing.plaf.InternalFrameUI;
import javax.swing.plaf.basic.BasicInternalFrameUI;
public class MCOCInternalFrame extends JInternalFrame {
//private String lookAndFeel = null;
BasicInternalFrameUI orgUi = null;
BasicInternalFrameUI newUi = null;
JComponent northPanel = null;
private boolean isHidden = false;
public MCOCInternalFrame() {
super();
northPanel = ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).getNorthPane();
orgUi = ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI());
newUi = new BasicInternalFrameUI(this);
}
public void showNorthPanel() {
this.setUI(orgUi);
this.putClientProperty("JInternalFrame.isPalette", Boolean.FALSE);
isHidden = false;
}
public void hideNorthPanel() {
this.setUI(newUi);
((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
isHidden = true;
}
public void updateUI() {
super.updateUI();
if (isHidden) {
hideNorthPanel();
}
}
}
创建该InternalFrame对象后,通过showNorthPanel(), hideNorthPanel()来显示或隐藏title bar,另外updateUI()重写是因为界面被动态改变lookandfeel时,保证title bar上多的一小个bar出现。