gpt4 book ai didi

java - 顶部带有标签的选项卡式 Pane

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:47 25 4
gpt4 key购买 nike

我需要添加一个 JLabel/JPanel,如图所示。调整框架大小时,应同时保留选项卡式 Pane 和标签/面板。 Tabbed Pane 内的标签和面板是独立的。我该怎么做?

enter image description here

最佳答案

这是一个使用 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);
}
}

关于java - 顶部带有标签的选项卡式 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15473511/

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