Tôi muốn tùy chỉnh giao diện của các tab trong JTabbedPane.
Tôi muốn bắt đầu từ hành vi đơn giản và đơn giản nhất: không có viền, màu đồng nhất.
Vấn đề là một sự không đồng đều vẫn còn: các tab lề nhỏ chồng lên nhau.
JTabbedPane tùy chỉnh giao diện tab
Bạn thấy rằng kể từ tab thứ hai được chọn, nó được "đưa đến fore". Điều này đạt được bằng một lề nhỏ chồng lên nhau. Có cách nào (không phức tạp) để vô hiệu hóa hành vi này không?
đơn giản, kiểm chứng (chỉ sửa chữa hàng nhập khẩu) mã:
public class TabbedPane_LookStudy extends JFrame{
public static void main(String [] args) throws UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
new TabbedPane_LookStudy().setVisible(true);
}
public TabbedPane_LookStudy() {
JTabbedPane tp = new JTabbedPane();
tp.setUI(new MyTabbedPaneUI());
add(tp);
tp.addTab("first",new JPanel());
tp.addTab("second", new JPanel());
tp.addTab("third", new JPanel());
setPreferredSize(new Dimension(180,100));
pack();
}
public static class MyTabbedPaneUI extends javax.swing.plaf.basic.BasicTabbedPaneUI {
@Override
protected void paintTab(Graphics g, int tabPlacement, Rectangle[] rects,
int tabIndex, Rectangle iconRect, Rectangle textRect) {
Color savedColor = g.getColor();
g.setColor(Color.PINK);
g.fillRect(rects[tabIndex].x, rects[tabIndex].y,
rects[tabIndex].width, rects[tabIndex].height);
g.setColor(Color.BLUE);
g.drawRect(rects[tabIndex].x, rects[tabIndex].y,
rects[tabIndex].width, rects[tabIndex].height);
g.setColor(savedColor);
}
}
}
+1 hình ảnh, sscce, câu hỏi – mKorbel