2013-03-25 206 views
1

我是一名初学者。我试图创建一个选项卡式窗口作为我的项目的GUI的一部分。但是,如下所示,用于导航选项卡的按钮显示某种类型的边框。有没有办法删除这些边界?看到附加的图像看问题。Java Swing GUI,自定义JtabbedPane风格

enter image description here

用于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); 
    } 
} 
+1

大多数可能是效果过得去'tabButtonCss'引起的。 – Smit 2013-03-25 18:59:23

+1

在这里看到也许是一个更清洁的解决方案:http://stackoverflow.com/questions/8752037/how-to-change-background-color-of-jtabbedpane – whiskeyspider 2013-03-25 19:01:36

+1

为了更好的帮助更快,发布[SSCCE](http:// sscce.org/)。 – 2013-03-26 00:00:12

回答

6

正如@Smit所指出的,它是所有下到CSS。另请注意,Java HTML/CSS引擎无法识别颜色为#fff的'3位'样式快捷方式。要获得白色,我们必须使用#ffffff

Css1 Css2 Css3

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); 
    } 
} 

见再为BG color @trashgod解决方案也@whiskeyspider建议。很多(太多)比试图强制它在HTML中更好。

+0

这真的很有用......但仍然有关于保证金的问题仍然存在:(... – 2013-03-26 04:23:33

+1

我在垃圾回收示例中看不到'问题边框',将其用于颜色。 – 2013-03-26 06:01:53