Java 如何像JLabel一样转换JScrollPane

Java 如何像JLabel一样转换JScrollPane,java,swing,jscrollpane,Java,Swing,Jscrollpane,我必须将JScrollPane更改为类似JLabel的内容。我在这里使用了以下代码: myJScroll.setBorder(null); myJScroll.setEnabled(false); myJScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); myJScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEV

我必须将
JScrollPane
更改为类似
JLabel
的内容。我在这里使用了以下代码:

myJScroll.setBorder(null);
myJScroll.setEnabled(false);
myJScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
myJScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
但是使用
setEnable(false)
,它有另一种颜色覆盖此窗格


如何将此颜色更改为其他颜色?

默认情况下,
JScrollPane
包含不透明的
视口组件。您可以为
视口设置自定义背景色

myJScroll.getViewport().setBackground(Color.BLUE)

或使视口不不透明:

myJScroll.getViewport().setOpaque(false)

根据您的需要,您可能还希望使
JScrollPane
非不透明(以获得滚动窗格父组件的真实背景色:

myJScroll.setOpaque(false);

还要记住要添加到视口的组件的不透明度和默认背景色

例如,将此作为测试用例进行尝试:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollPaneTest extends JPanel
{
    private static final long serialVersionUID = 1L;

    public ScrollPaneTest()
    {
        super(new BorderLayout());

        JPanel myScrolledComponent = new JPanel(new BorderLayout());
        myScrolledComponent.setBackground(Color.YELLOW);
        myScrolledComponent.setOpaque(false);
        myScrolledComponent.add(new JLabel("Some text"), BorderLayout.CENTER);

        JScrollPane myJScroll = new JScrollPane(myScrolledComponent);
        myJScroll.setBackground(Color.CYAN);
        myJScroll.setOpaque(false);

        myJScroll.setBorder(null);
        myJScroll.setEnabled(false);
        myJScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        myJScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        myJScroll.getViewport().setBackground(Color.MAGENTA);
        myJScroll.getViewport().setOpaque(false);

        super.add(myJScroll, BorderLayout.CENTER);

        return;
    }

    @SuppressWarnings("deprecation")
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setBackground(Color.BLUE);
        ScrollPaneTest test = new ScrollPaneTest();
        test.setBackground(Color.RED);
        f.add(test);
        f.pack();
        f.show();
    }
}
上面的类将显示带有红色背景的
JLabel
文本(对
ScrollPaneTest
组件一直透明)

基于您的评论的第二个示例:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollPaneTest2 extends JScrollPane
{
    private static final long serialVersionUID = 1L;

    public ScrollPaneTest2()
    {
        super();
        super.setBackground(Color.CYAN);

        super.getViewport().setBackground(Color.MAGENTA);
        super.getViewport().setOpaque(false);

        return;
    }

    @SuppressWarnings("deprecation")
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setBackground(Color.BLUE);
        ScrollPaneTest2 scrollPane = new ScrollPaneTest2();
        scrollPane.setBackground(Color.RED);
        f.add(scrollPane);

        JPanel myScrolledComponent = new JPanel(new BorderLayout());
        myScrolledComponent.setBackground(Color.YELLOW);
        myScrolledComponent.setOpaque(false);
        myScrolledComponent.add(new JLabel("Some text"), BorderLayout.CENTER);

        scrollPane.getViewport().setView(myScrolledComponent);

        scrollPane.setBorder(null);
        scrollPane.setEnabled(false);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        f.pack();
        f.show();
    }
}

JScrollPane
包含默认不透明的
视口组件。您可以为
视口设置自定义背景色:

myJScroll.getViewport().setBackground(Color.BLUE);

或使视口不不透明:

myJScroll.getViewport().setOpaque(false);

根据您的需要,您可能还希望使
JScrollPane
非不透明(以获得滚动窗格父组件的真实背景色:

myJScroll.setOpaque(false);

还要记住要添加到视口的组件的不透明度和默认背景色

例如,将此作为测试用例进行尝试:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollPaneTest extends JPanel
{
    private static final long serialVersionUID = 1L;

    public ScrollPaneTest()
    {
        super(new BorderLayout());

        JPanel myScrolledComponent = new JPanel(new BorderLayout());
        myScrolledComponent.setBackground(Color.YELLOW);
        myScrolledComponent.setOpaque(false);
        myScrolledComponent.add(new JLabel("Some text"), BorderLayout.CENTER);

        JScrollPane myJScroll = new JScrollPane(myScrolledComponent);
        myJScroll.setBackground(Color.CYAN);
        myJScroll.setOpaque(false);

        myJScroll.setBorder(null);
        myJScroll.setEnabled(false);
        myJScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        myJScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        myJScroll.getViewport().setBackground(Color.MAGENTA);
        myJScroll.getViewport().setOpaque(false);

        super.add(myJScroll, BorderLayout.CENTER);

        return;
    }

    @SuppressWarnings("deprecation")
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setBackground(Color.BLUE);
        ScrollPaneTest test = new ScrollPaneTest();
        test.setBackground(Color.RED);
        f.add(test);
        f.pack();
        f.show();
    }
}
上面的类将显示带有红色背景的
JLabel
文本(对
ScrollPaneTest
组件一直透明)

基于您的评论的第二个示例:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollPaneTest2 extends JScrollPane
{
    private static final long serialVersionUID = 1L;

    public ScrollPaneTest2()
    {
        super();
        super.setBackground(Color.CYAN);

        super.getViewport().setBackground(Color.MAGENTA);
        super.getViewport().setOpaque(false);

        return;
    }

    @SuppressWarnings("deprecation")
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        f.setBackground(Color.BLUE);
        ScrollPaneTest2 scrollPane = new ScrollPaneTest2();
        scrollPane.setBackground(Color.RED);
        f.add(scrollPane);

        JPanel myScrolledComponent = new JPanel(new BorderLayout());
        myScrolledComponent.setBackground(Color.YELLOW);
        myScrolledComponent.setOpaque(false);
        myScrolledComponent.add(new JLabel("Some text"), BorderLayout.CENTER);

        scrollPane.getViewport().setView(myScrolledComponent);

        scrollPane.setBorder(null);
        scrollPane.setEnabled(false);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        f.pack();
        f.show();
    }
}

启用的着色通常由UI委托提供,您可以在
UIManager
中更改颜色键,但是,从概念上讲,
JScrollPane
JLabel
是两个完全不同的东西,
JLabel
对于StartHanks是透明的,在特殊情况下,我需要使用JScrollPaneSo..y您正在尝试创建一个滚动窗格,其外观或行为与滚动窗格不同?这有多没用?@MadProgrammer:我使用myJScroll.setBackgroundColor(Color.white);要设置背景色,它可以正常工作,但jscrollpane上的颜色文本从黑色变为灰色。我使用setForeground(Color.Black)再次设置黑色,但不起作用。请给我建议。。。Thanks@PhạmQuốcBảo>代码> jScR卷卷子没有文本…也,为什么你需要禁用它?考虑提供一个演示你的问题的代码。这不是一个代码转储,而是你正在做的一个例子,它突出了你所面临的问题。这将导致更少的混乱和更好的响应。对于UI委托,您可能可以更改
UIManager
中的颜色键,但是,从概念上讲,
JScrollPane
JLabel
是两件完全不同的事情,
JLabel
对于StartHanks是透明的,在特殊情况下,我需要使用JScrollPaneSo..您正在尝试创建一个滚动窗格,..doesn看起来或行为不像滚动窗格?那有多没用?@MadProgrammer:我使用myJScroll.setBackgroundColor(Color.white);设置背景色时,它工作正常,但jscrollpane上的彩色文本从黑色变为灰色。我使用setForeground(Color.Black)再次设置黑色,但不起作用。请给我建议。。。Thanks@PhạmQuốcBảo>代码> jScLabPANE<代码>没有文本…也,为什么你需要禁用它?考虑提供一个演示你的问题的代码。这不是一个代码转储,而是你正在做的一个例子,它突出了你所面临的问题。这将导致更少的混乱和更好。responses@PhạmQuốcBảo我发现确定显示哪个组件的背景的最佳方法是将所有涉及的组件设置为不透明,并为每个组件设置完全不同的背景颜色。JScrollPaneTest从JScrollPane扩展而来,而不是Jpane。@PhạmQuốcBảo关于扩展JScrollPane的第二个示例,请参见我的修订答案。@PhạmQuốcBảo我发现确定显示哪个组件的背景的最佳方法是将所有涉及的组件设置为不透明,并为每个组件设置完全不同的背景颜色。JScrollPaneTest从JScrollPane扩展而来,而不是Jpane。@PhạmQuốcBảo关于扩展JScrollPane的第二个示例,请参见我的修订答案。