简体   繁体   English

如何在Java Swing中制作JTabbedPane的圆角

[英]How to make rounded corners of JTabbedPane in Java Swing

I need make a JTabbedPane like this (I made the image in Photoshop): 我需要制作一个像这样的JTabbedPane(我在Photoshop中制作了图像):

敬畏

But in my look and feel (based on TabbedPaneUI: javax.swing.plaf.basic.BasicTabbedPaneUI) looks like this: 但是在我的外观(基于TabbedPaneUI:javax.swing.plaf.basic.BasicTabbedPaneUI)上看起来像这样:

敬畏

How can I do it? 我该怎么做?

I've tried change LAF properties, but I didn't find a solution. 我曾尝试更改LAF属性,但没有找到解决方案。 If I use setBorder method the swing make this: 如果我使用setBorder方法,则进行以下操作:

jtabbedpane1.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1, true));

敬畏

Java changed only the upper left corner as outer border as image above shows. Java仅更改了左上角的外边界,如上图所示。

I need a solution that might use the Paint method on an extended JTabbedPane class, but I really don't know if this is correct or how do this. 我需要一个可能在扩展的JTabbedPane类上使用Paint方法的解决方案,但我真的不知道这是否正确或如何执行。

Rounded corners are actually a boolean argument when instantiating a border, as can be seen here with BorderFactory . 实例化边框时,圆角实际上是布尔参数,如BorderFactory所示

So what we can do is something like this: 所以我们可以做的是这样的:

    pane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2, true));

Where "true" refers to rounded corners. 其中“ true”是指圆角。

If you are interested in customizing the border further, you will most likely have to paint it yourself, in which case I would look here for a further read. 如果您有兴趣进一步自定义边框,则很可能需要自己绘制边框,在这种情况下,我将在此处进行进一步的阅读。

Edit regarding your code: 编辑有关您的代码:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.YELLOW);
    g.drawPolyLine(new int[]{getX(), getX() getX() + 12}, new int[]{getY() + 12, getY(), getY()});
    g.drawPolyLine(.....); // next corner
    g.drawPolyLine(.....); // next corner
}

etc. where you repeat for each corner that you want your L shape at. 等等,在该处重复您想要L形的每个角。

I read the tutorial above and tried override paintComponent method in my extended JTabbedPane class, see: 我阅读了上面的教程,并尝试在扩展的JTabbedPane类中重写paintComponent方法,请参见:

public class MyTabbedPane extends JTabbedPane {    

[...]
@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.YELLOW);
        g.drawRoundRect(getX()-12, getY()-11, getWidth()-4, getHeight()-22, 6, 6);
    }
}

The result: 结果:

https://i.imgur.com/YLXkVRS.jpg https://i.imgur.com/YLXkVRS.jpg

Here is the start of an answer. 这是答案的开始。

import javax.swing.*;
import java.awt.Dimension;
import javax.swing.plaf.TabbedPaneUI;
import javax.swing.plaf.metal.MetalTabbedPaneUI;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Insets;
public class Bordered{

    public static void main(String[] args){
        JFrame frame = new JFrame("border check");
        JPanel content = new JPanel();
        JTabbedPane tabs = new JTabbedPane();

        JPanel one = new JPanel();

        one.add(new JLabel("first tab"));
        one.setOpaque(true);
        one.setBackground(Color.WHITE);
        JPanel two = new JPanel();
        two.add(new JLabel("second tab"));

        tabs.add("one", one);
        tabs.add("two", two);

        tabs.setUI( new MetalTabbedPaneUI(){

            @Override
            protected void paintContentBorder(Graphics g, int placement, int selectedIndex){
                int width = tabPane.getWidth();
                int height = tabPane.getHeight();
                Insets insets = tabPane.getInsets();
                Insets tabAreaInsets = getTabAreaInsets(placement);
                int x = insets.left;
                int y = insets.top;
                int w = width - insets.right - insets.left;
                int h = height - insets.top - insets.bottom;
                y += calculateTabAreaHeight(placement, runCount, maxTabHeight);

                h -= (y - insets.top);
                //g.fillRoundRect(x, y, w, h, 5, 5);
            }
        });

        tabs.setPreferredSize(new Dimension(400, 200));
        content.add(tabs);
        frame.setContentPane(content);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

Somethings to note, The inner panel the ones holding the jlabel have square corners. 需要注意的是,内部面板上带有jlabel的面板有角。 I've shown this by making one white. 我已经通过制作白色来说明这one I've taken some of the boundary code from BasicTabbedPaneUI source code. 我从BasicTabbedPaneUI源代码中提取了一些边界代码。

They really did not make this easy to manage, but looking at the source for the MetalTabbedPaneUI you can see they draw each border as a line, and it would need to be modified to draw a curve at the ends. 他们的确并没有使它易于管理,但是查看MetalTabbedPaneUI的源代码,您会看到他们将每个边框绘制为一条线,并且需要对其进行修改以在末端绘制一条曲线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM