我制作了这个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外观,我的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不再是透明的,任何与Nimbus不同的L&F。

此外,我讨厌tabbedPane的bordeline,我只是离开滚动面板的LineBorder:

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


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

最佳答案

主要回答:

在不同的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);




其他建议:


调用ServerFrame内部的EventQueue.invokeLater()以防止出现进一步的问题:EventQueue.invokeLater(new Runnable() { public void run() { ServerFrame f = new ServerFrame(); }});
不要使用null-layout,而是查看layout managers

10-05 17:55