Java Swing GUI,自定义JtabbedPane样式 [英] Java Swing GUI, customising JtabbedPane style

查看:862
本文介绍了Java Swing GUI,自定义JtabbedPane样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是秋千的初学者。我试图创建一个标签窗口作为我的项目的GUI的一部分。但是如下所示,用于导航选项卡的按钮显示某种边框。有没有办法,我可以删除这些边界?请参阅附图查看问题。

I am a beginner in swing. I tried to create a tabbed window as part of the GUI for my project. But as shown below, the buttons for navigating tabs shows some sort of a border. Is there a way I can remove these borders? See the attached image to see the problem.

>

GUI的代码如下

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class tst {    
    /**
     * @param args the command line arguments
     */
    public static String  generateHtml(String tabButtonLabel,String style) {
        /*@@Generates HTML for the tab button when the button label is given*/
        String ret = "<html><body style = '"+style+"'>"+tabButtonLabel+"</body></html>";
        return ret;
    }

    public static void main(String[] args) {
        // TODO code application logic here
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JFrame frame = new JFrame("tst");
        frame.setVisible(true);
        frame.setSize(screenSize);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        /*Groups tab*/            
        JPanel groups = new JPanel();
        groups.setBackground(Color.gray);            
        /*Settings Tab*/            
        JPanel settings = new JPanel();            
        /*About Tab*/            
        JPanel about = new JPanel();            
        /*Tabbed pane to hold all panels*/
        JTabbedPane tabbedPane = new JTabbedPane();             
        /*Tabbed Pane CSS*/
        String tabButtonCss = "margin:0;width:110px;height:10px;border-radius:3px;padding:10px;background:#fff;text-align:center;border:none;";

        tabbedPane.setVisible(true);
        tabbedPane.add(generateHtml("Groups",tabButtonCss),groups);
        tabbedPane.add("Settings",settings);
        tabbedPane.add("About",about);            
        /*Tab styles*/
        tabbedPane.setBackground(Color.gray);
        tabbedPane.setForeground(Color.white);
        tabbedPane.setBounds(0, 0, 0,0);
        //tabbedPane.setBorder(new EmptyBorder(-10,-20,-10,0));            
        frame.add(tabbedPane);
    }
}


推荐答案

注意@Smit,它都是由CSS。还要注意,Java HTML / CSS引擎不能识别颜色的<3>数字'样式快捷方式 #fff 。要得到白色,我们必须使用 #ffffff

As noted by @Smit, it is all down to the CSS. Note also that the Java HTML/CSS engine does not recognize the '3 digit' style shortcut of #fff for colors. To get white we must use #ffffff.



import java.awt.*;
import javax.swing.*;

class ExampleCSS {

    public static void showStyle(String style) {
        JPanel gui = new JPanel(new BorderLayout());

        String html1 = "<html><body style = '"+style+"'>";
        String html2 =  "</body></html>";
        JTabbedPane tp = new JTabbedPane();
        tp.addTab(html1 + "Groups" + html2, new JLabel(style));
        tp.addTab(html1 + "Settings" + html2, new JLabel(style));
        tp.addTab(html1 + "About" + html2, new JLabel(style));

        gui.add(tp);

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setContentPane(gui);
        f.setMinimumSize(new Dimension(400,100));
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                String[] css = {
                    "margin:0;background:#ffffff;",
                    "padding:10px;",
                    "width:110px;height:10px;border-radius:3px;"
                        + "text-align:center;border:none;"
                };
                showStyle(css[0]);
                showStyle(css[0]+css[1]);
                showStyle(css[0]+css[1]+css[2]);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

另请参见@whiskeyspider建议re @trashgod解决方案 BG颜色

See also @whiskeyspider suggestion re @trashgod solution for the BG color. Much (much) nicer than trying to force it in the HTML.

) .com / sNVWR.png>

这篇关于Java Swing GUI,自定义JtabbedPane样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆