【问题标题】:(JTabbedPane) Transparence does not work on L&F different from Nimbus(JTabbedPane) 透明度不适用于与 Nimbus 不同的 L&F
【发布时间】:2015-12-21 22:53:59
【问题描述】:

我制作了这个 java JFrame:

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

}

我的问题是,如果我想使用 Nimbus Look And Feels,我的 TabbedPane 会变得透明,这没关系...但是如果我更改为 Metal 或 System Look And Feels,透明度就会消失..

这是显示此行为的图像。

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

我错过了什么?

编辑:

LuxxMiner 解决方案带回透明度:

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

frame= new ServerFrame()之前

和:

scrollPane.setOpaque(false);

现在的问题是:

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

此外,我讨厌 tabbedPane 的边框,我会留下滚动窗格的 LineBorder:

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

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

【问题讨论】:

  • 嗨弗朗西斯科。我们的工作方式与其他论坛不同。请不要编辑问题的答案。如果您对自己的问题有答案,请将其写为答案(使用底部的“发布您的答案”或“回答您自己的问题”按钮。)另外,请不要在标题中写下 SOLVED。系统本身将指示是否有接受的答案。您可以在一段时间后接受自己的答案。
  • 好的,我马上就做,对不起。
  • 没问题。只是需要一点时间来适应我们的不同。

标签: java swing opacity jtabbedpane nimbus


【解决方案1】:

主要答案:

这似乎是在不同的 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. EventQueue.invokeLater() 中调用ServerFrame 以防止进一步的问题:EventQueue.invokeLater(new Runnable() { public void run() { ServerFrame f = new ServerFrame(); } });
  2. 不要使用空布局,而是查看layout managers

【讨论】:

  • 您的解决方案恢复了透明度,但请看我刚刚制作的这个视频(WMV 2MB,您必须下载才能看到它,嵌入式播放器不起作用):www76.zippyshare.com/v/fOfL6OLP/file.html 如果我尝试写入我的文本区域,一切都变得很奇怪......此时还有 NIMBUS L&F。此外,tabTitle 仍然不透明.. 有没有办法设置它?
  • @FrancescoRizzi 我找到了第一个问题的解决方案(见编辑),如果我也找到第二个问题,我会通知你。
  • 它可以工作,但现在我的标签窗格有边框,我怎样才能将它们设置为不可见?我只想看到我的滚动窗格边框:scrollPane.setBorder(new LineBorder(new Color(192,192,192)));编辑:tabbedpane 边框仅在与 Nimbus 不同的其他 L&F 中出现
  • @FrancescoRizzi 这就是为什么我说“如果我也找到第二个,我会通知你”...... - 我正在努力:P
  • @FrancescoRizzi 我还没有找到一个不是次优的解决方案,抱歉。我唯一的建议是复制我的新代码(请参阅第一个代码块的编辑)并将滚动窗格的边框设置为 Color.WHITE - 它不会再丑陋了,但它会还在那里。
【解决方案2】:

解决方案

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

【讨论】:

    猜你喜欢
    • 2012-05-27
    • 2011-12-31
    • 1970-01-01
    • 2014-07-27
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    相关资源
    最近更新 更多