使 TextArea 的背景不透明

发布于 2024-10-15 06:31:34 字数 3631 浏览 2 评论 0原文

我想让我的 TextArea 的背景不透明。我在 netbeans 上使用 swing gui 表单。

我想重叠图像前面的说明(查看这张图片),但问题是如果文本位于图像前面,则看不到它们。我真的不知道不透明的背景是否是这里的问题(我使用分层窗格来放置图片所在的 JLabel 并与我放置说明的 TextArea 重叠)。

你能帮我找出问题所在吗? 在此处输入图像描述

这是代码:

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class About extends javax.swing.JDialog {

public About(JFrame owner) {
    super(owner, true);
    initComponents();
    setSize(500, 500);
    setLocation(300, 120);
    getContentPane().setBackground(Color.getHSBColor(204, 204, 255));
    jLabel1.setIcon(new ImageIcon("HangmanStartlight.jpg"));

 }

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jTabbedPane1 = new javax.swing.JTabbedPane();
    jLayeredPane1 = new javax.swing.JLayeredPane();
    jLabel1 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setText("jLabel1");
    jLabel1.setBounds(0, 0, 420, 290);
    jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTextArea1.setColumns(20);
    jTextArea1.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14)); // NOI18N
    jTextArea1.setForeground(new java.awt.Color(51, 0, 51));
    jTextArea1.setRows(5);
    jTextArea1.setText("Hangman\n\nGuess the \"HIDDEN PHRASE\" to proceed to the next\nlevel.  Hangman is filled with several words, \npresented in random order each time you play.\n\nhOw To pLay!!!\n\nYou can select letters by clicking on the letter buttons\nshown at the window.\n\nBe very careful! 'coz you are only allowed 10 wrong\nguesses per puzzle.\n\nYou will at the corner Top-Left of the window your \n\"Name\", your \"Score\", your \"Level\", your \"Lives Left\"\nand also the \"Life Line\" which you can use as a Hint.\n\nEnjoy Playing and Have Fun :)");
    jTextArea1.setWrapStyleWord(true);
    jScrollPane1.setViewportView(jTextArea1);

    jScrollPane1.setBounds(0, 0, 420, 280);
    jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTabbedPane1.addTab("tab1", jLayeredPane1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(26, 26, 26)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(39, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(128, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}

ps 我正在使用 swing builder/ swing gui 表单..

I want to make my TextArea's background opaque. I'm using swing gui forms at netbeans.

I want to overlap the instructions infront of the image (see this picture), but the problem is texts can't be seen if they are infront of the image. I really don't know if the not opaque background is the problem here (I've use the layered pane to have a JLabel where the picture was located and overlapping the TextArea where i placed my instructions).

Can you help me figure out what the problem is?
enter image description here

here's the code:

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class About extends javax.swing.JDialog {

public About(JFrame owner) {
    super(owner, true);
    initComponents();
    setSize(500, 500);
    setLocation(300, 120);
    getContentPane().setBackground(Color.getHSBColor(204, 204, 255));
    jLabel1.setIcon(new ImageIcon("HangmanStartlight.jpg"));

 }

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jTabbedPane1 = new javax.swing.JTabbedPane();
    jLayeredPane1 = new javax.swing.JLayeredPane();
    jLabel1 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setText("jLabel1");
    jLabel1.setBounds(0, 0, 420, 290);
    jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTextArea1.setColumns(20);
    jTextArea1.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 14)); // NOI18N
    jTextArea1.setForeground(new java.awt.Color(51, 0, 51));
    jTextArea1.setRows(5);
    jTextArea1.setText("Hangman\n\nGuess the \"HIDDEN PHRASE\" to proceed to the next\nlevel.  Hangman is filled with several words, \npresented in random order each time you play.\n\nhOw To pLay!!!\n\nYou can select letters by clicking on the letter buttons\nshown at the window.\n\nBe very careful! 'coz you are only allowed 10 wrong\nguesses per puzzle.\n\nYou will at the corner Top-Left of the window your \n\"Name\", your \"Score\", your \"Level\", your \"Lives Left\"\nand also the \"Life Line\" which you can use as a Hint.\n\nEnjoy Playing and Have Fun :)");
    jTextArea1.setWrapStyleWord(true);
    jScrollPane1.setViewportView(jTextArea1);

    jScrollPane1.setBounds(0, 0, 420, 280);
    jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTabbedPane1.addTab("tab1", jLayeredPane1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(26, 26, 26)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(39, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(128, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}

p.s. i am using swing builder/ swing gui forms..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强辩 2024-10-22 06:31:34

尝试使用后台面板中的提示之一。

Try using one of the tips from Background Panel.

紫南 2024-10-22 06:31:34

编写您自己的组件,在屏幕上手动绘制文本。这就是用 Java 制作游戏的方式,而不是用 Swing...

Write your own component which draws the text manually on the screen. That's how game's are made in Java, not with Swing...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文