(JTabbedPane)透明度不适用于与Nimbus不同的L& F

时间:2015-09-24 10:57:33

标签: java swing opacity jtabbedpane nimbus

我制作了这个java JFrame:

sink

}

我的问题是,如果我想使用Nimbus外观和感觉,我的TabbedPane变得透明,它没问题......但如果我改为金属或系统外观,透明度就会消失..

这是一张显示此行为的图片。

[![Nimbus and Syste L& F] [1]] [1]

我缺少什么?

编辑:

LuxxMiner解决方案带来了透明度:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.Component;
import javax.swing.JTabbedPane;
import java.awt.Rectangle;

public class ServerFrame{

private JFrame frame;

private JTabbedPane tabbedPane;

public static void main(String [] args){

    ServerFrame f=new ServerFrame();

}
protected ServerFrame(){

    frame=new JFrame();
    frame.setLocation(new Point(500, 100));
    frame.setResizable(false);
    frame.setTitle("JSock Network System - v1.0");
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    try {

        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {

            if ("Nimbus".equals(info.getName())) {

                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    }
    catch (Exception e) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {

            e1.printStackTrace();
        }
    }

    frame.getContentPane().setBackground(Color.DARK_GRAY);

    frame.setSize(new Dimension(805, 462));
    frame.setLocationRelativeTo(null);
    frame.getContentPane().setMinimumSize(new Dimension(800, 600));
    frame.getContentPane().setMaximumSize(new Dimension(800, 600));
    frame.getContentPane().setPreferredSize(new Dimension(800, 600));
    frame.getContentPane().setLayout(null);

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setForeground(Color.DARK_GRAY);
    tabbedPane.setBounds(new Rectangle(0, 0, 0, 333));
    tabbedPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    tabbedPane.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
    tabbedPane.setBackground(new Color(0,0,0,0));
    tabbedPane.setOpaque(false);
    tabbedPane.setBounds(5, 95, 789, 333);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    scrollPane.setViewportBorder(new EmptyBorder(0, 0, 0, 0));
    scrollPane.setBackground(new Color(0,0,0,0));
    scrollPane.setBorder(new LineBorder(new Color(192, 192, 192)));
    scrollPane.getViewport().setOpaque(false);
    scrollPane.setOpaque(false);

    JTextArea textArea= new JTextArea();
    textArea.setOpaque(false);
    textArea.setForeground(Color.DARK_GRAY);
    textArea.setFont(new Font("Consolas", Font.PLAIN, 10));
    textArea.setEditable(false);
    textArea.setBorder(new EmptyBorder(0, 0, 0, 0));
    textArea.setBackground(new Color(0,0,0,0));

    scrollPane.setViewportView(textArea);
    tabbedPane.addTab("SERVER", null,scrollPane,null);
    frame.getContentPane().add(tabbedPane);

    JLabel lblNewLabel = new JLabel("QTminer");
    lblNewLabel.setForeground(Color.WHITE);
    lblNewLabel.setFont(new Font("Segoe UI", Font.BOLD, 71));
    lblNewLabel.setBounds(317, 6, 305, 77);
    frame.getContentPane().add(lblNewLabel);

    JLabel lblNetworkSystem = new JLabel("JSockNS v1.0 Inside");
    lblNetworkSystem.setAlignmentX(Component.CENTER_ALIGNMENT);
    lblNetworkSystem.setForeground(Color.WHITE);
    lblNetworkSystem.setFont(new Font("Segoe UI", Font.BOLD, 22));
    lblNetworkSystem.setBounds(455, 68, 238, 39);
    frame.getContentPane().add(lblNetworkSystem);

    JLabel lblJsocknslog = new JLabel("JSockNS Log");
    lblJsocknslog.setBounds(720, 400, 85, 26);
    frame.getContentPane().add(lblJsocknslog);
    lblJsocknslog.setForeground(Color.WHITE);
    lblJsocknslog.setFont(new Font("Segoe UI Semilight", Font.PLAIN, 13));

    JLabel background = new JLabel("");
    background.setIcon(new ImageIcon("C:\\Users\\Francesco\\Desktop\\841887-light-blue-wallpaper.jpg"));
    background.setBounds(-1121, -400, 2019, 912);
    frame.getContentPane().add(background);
    frame.setVisible(true);
}

之前UIManager.put("TabbedPane.contentOpaque", false);

frame= new ServerFrame()

现在的问题是:

TabTitle不再透明,任何L& F与Nimbus不同。

此外,我讨厌tabbedPane的bordeline,我会离开我的滚动窗格的LineBorder:

scrollPane.setOpaque(false);

如何让我的tabbedPane边框不可见?

2 个答案:

答案 0 :(得分:2)

主要答案:

这似乎是在不同的LaF中使用TabbedPane的一些问题。在执行ServerFrame f = new ServerFrame();

之前先这样做
Color transparent = new Color(0, 0, 0, 0);
UIManager.put("TabbedPane.contentAreaColor", transparent);
UIManager.put("TabbedPane.selected", transparent);
UIManager.put("TabbedPane.unselectedTabBackground", transparent);
UIManager.put("TabbedPane.background", transparent);
UIManager.put("TabbedPane.borderHightlightColor", transparent);
UIManager.put("TabbedPane.darkShadow", transparent);
UIManager.put("TabbedPane.shadow", transparent);
UIManager.put("TabbedPane.focus", transparent);
UIManager.put("TabbedPane.selectHighlight", transparent);
UIManager.put("TabbedPane.lightHighlight", transparent);
UIManager.put("TabbedPane.light", transparent);
UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));

您也可以将UIManager.put("TabbedPane.contentAreaColor", transparent);替换为UIManager.put("TabbedPane.contentOpaque", false);

编辑:

另外添加:

scrollPane.setOpaque(false);

和此:

textArea.setOpaque(false);

其他建议:

  1. 调用ServerFrame内的EventQueue.invokeLater()以防止出现更多问题:EventQueue.invokeLater(new Runnable() { public void run() { ServerFrame f = new ServerFrame(); } });
  2. 请勿使用null-layout,请查看layout managers

答案 1 :(得分:0)

从LuxxMiner提出的解决方案开始:

这样做:

1)

        //this sets the tabTitle:
    //PLEASE NOTE, use this exact syntax, new Color(0,0,0,0) each time)
    UIManager.put("TabbedPane.focus", new Color(0,0,0,0));  //removes focus rectangle.
    UIManager.put("TabbedPane.selected", new Color(0,0,0,0));   //removes background.
    UIManager.put("TabbedPane.contentOpaque",  false);  //removes background of tab content (not tab title)

这些必须是创建一切之前的第一行。

2)

tabbedPane = new JTabbedPane(JTabbedPane.TOP);

    tabbedPane.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI(){


        protected void paintContentBorder(Graphics g,int tabPlacement,int selectedIndex){} //removes tab content border.
        protected void paintTabBorder(Graphics g, int tabPlacement,int tabIndex,int x, int y, int w, int h,boolean isSelected){} //removes tab (title) border.
    });
    tabbedPane.setForeground(Color.DARK_GRAY);
    tabbedPane.setBounds(new Rectangle(0, 0, 0, 333));
    tabbedPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    tabbedPane.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
    tabbedPane.setBackground(new Color(0,0,0,0));
    //tabbedPane.setOpaque(false); not needed
    tabbedPane.setBounds(5, 95, 789, 333);

这是tabbedPane的代码。

结果:

此图显示了这些修复后的结果。此处显示的边框是scrollPane的一个,将其设置为EmptyBorder(0,0,0,0)以进行删除。

scrollPane.setBorder(new LineBorder(new Color(192,192,192)));

enter image description here