简体   繁体   English

Java Swing Applet-无法设置背景颜色

[英]Java Swing Applet- Cant set background color

Im new to Swing, first of all. 我是Swing的新手,首先。 Im trying to set the background color of an audio recording applet to make it blend with my webpage (white instead of the default grey), but the change never seems to take. 我试图设置录音小程序的背景颜色,使其与我的网页混合(白色而不是默认的灰色),但似乎永远不会改变。 Heres the applet initialization... 继承人applet初始化......

public void init()
{
    setLayout(null);
    setBackground(Color.white);
    JLabel recorder = new JLabel("Record");
    JLabel fileName = new JLabel("Please Enter File Name");
    JLabel status = new JLabel("Status...");
    fnametxt = new JTextField("FileNameHere");
    statustxt = new JTextField("");
    record = new JButton("Record");
    play = new JButton("Play");
    pause = new JButton("Pause");
    stop = new JButton("Stop");
    send = new JButton("Upload");
    listen = new JButton("Listen");
    save = new JButton("Save and Submit");
//A bunch of other stuff, event listeners and whatnot.

Im using no layout managers, Im setting all the positions manually. 我没有使用布局管理器,我手动设置所有位置。 Any ideas? 有任何想法吗?

You set (presumably) the background of the Applet, but that background will only show where it is not obstructed by another component. 您(可能)设置了Applet的背景,但该背景仅显示其未被其他组件阻挡的位置。

Depending on how you structured your GUI, there may be inner panels or other components covering the area. 根据您构建GUI的方式,可能会有内部面板或覆盖该区域的其他组件。 You need to change the color of those components, too (or alternately set them to be transparent using setOpaque(false)). 您也需要更改这些组件的颜色(或者使用setOpaque(false)将它们设置为透明)。

Edit: setOpaque() is only available for Swing components, not the Applet itself (since that is plain old AWT). 编辑:setOpaque()仅适用于Swing组件,而不适用于Applet本身(因为这是普通的旧AWT)。

您可能应该设置内容窗格的背景颜色。

If you're using a JPanel in your applet, you'll have to color the JPanel's content pane as well. 如果您在applet中使用JPanel,则还必须为JPanel的内容窗格着色。 The following code sets the background of the JPanel itself AND its content pane to white: 以下代码将JPanel本身及其内容窗格的背景设置为白色:

setBackground(Color.white);
getContentPane().setBackground(Color.white); //Color JPanel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM