标签顶部有标签的窗格
答
下面是一个简单的例子,使用JLayer(由mKorbel建议):
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
public class TopRightCornerLabelLayerUITest {
public static JComponent makeUI() {
JTabbedPane tab = new JTabbedPane();
tab.addTab("New tab1", new JLabel("1"));
tab.addTab("New Tab2", new JLabel("2"));
JPanel p = new JPanel(new BorderLayout());
p.add(new JLayer<JComponent>(tab, new TopRightCornerLabelLayerUI()));
return p;
}
private static void createAndShowUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowUI();
}
});
}
}
class TopRightCornerLabelLayerUI extends LayerUI<JComponent> {
private JLabel l = new JLabel("A Label at right corner");
private JPanel rubberStamp = new JPanel();
@Override public void paint(Graphics g, JComponent c) {
super.paint(g, c);
Dimension d = l.getPreferredSize();
int x = c.getWidth() - d.width - 5;
SwingUtilities.paintComponent(g, l, rubberStamp, x, 2, d.width, d.height);
}
}
答
我需要添加一个JLabel/JPanel中所示的图像中在调整 框架的大小时,标签窗格和标签/面板都应驻留。 标签和Tabbed窗格内的面板是独立的。如果有任何 有解决方案,请帮助我。
您可以使用:
我会使用JLayer如果是可能的(需要Java7)。
伟大的!工作......非常感谢 – Abin 2013-03-18 11:30:51
您可以告诉我如何在此标签上添加一个JPopupMenu(“右边的标签”),以在此标签上添加一些菜单项。 pleasee – Abin 2013-04-05 09:51:54
我想你可能想重写'LayerUI#processMouseEvent(MouseEvent e,JLayer l)'并且这样做:'if(labelRect.contains(pt)&& e.isPopupTrigger())popup.show(tabbedPane,pt .x,pt.y);' – aterai 2013-04-06 10:34:06