(JTabbedPane) 透明度不适用于不同于 Nimbus 的 L&F
(JTabbedPane) Transparence does not work on L&F different from Nimbus
我做了这个 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 和系统 L&F][1]][1]
我错过了什么?
编辑:
LuxxMiner 解决方案恢复透明度:
UIManager.put("TabbedPane.contentOpaque", false);
在frame= new ServerFrame()
之前
和:
scrollPane.setOpaque(false);
现在的问题是:
TabTitle 不再透明,L&F 与 Nimbus 不同。
此外,我讨厌 tabbedPane 的 bordeline,我只想保留我的 scrollpane 的 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);
补充建议:
- 调用
EventQueue.invokeLater()
中的 ServerFrame
以防止进一步的问题:EventQueue.invokeLater(new Runnable() {
public void run() {
ServerFrame f = new ServerFrame();
}
});
- 不要使用空布局,请查看 layout managers。
解决方案
从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);
这是选项卡面板的代码。
结果:
此图显示了这些修复后的结果。此处显示的边框是 scrollPane 的边框,将其设置为 EmptyBorder(0,0,0,0) 以移除。
scrollPane.setBorder(new LineBorder(new Color(192,192,192)));
我做了这个 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 和系统 L&F][1]][1]
我错过了什么?
编辑:
LuxxMiner 解决方案恢复透明度:
UIManager.put("TabbedPane.contentOpaque", false);
在frame= new ServerFrame()
和:
scrollPane.setOpaque(false);
现在的问题是:
TabTitle 不再透明,L&F 与 Nimbus 不同。
此外,我讨厌 tabbedPane 的 bordeline,我只想保留我的 scrollpane 的 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);
补充建议:
- 调用
EventQueue.invokeLater()
中的ServerFrame
以防止进一步的问题:EventQueue.invokeLater(new Runnable() { public void run() { ServerFrame f = new ServerFrame(); } });
- 不要使用空布局,请查看 layout managers。
解决方案
从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);
这是选项卡面板的代码。
结果:
此图显示了这些修复后的结果。此处显示的边框是 scrollPane 的边框,将其设置为 EmptyBorder(0,0,0,0) 以移除。
scrollPane.setBorder(new LineBorder(new Color(192,192,192)));