English 中文(简体)
如何抹黑一个摇摆部件?
原标题:How to black out a Swing component?
  • 时间:2012-05-22 13:40:35
  •  标签:
  • java
  • swing

Hi 有要求, 在某些条件下, 我要将保存字符串值的 秋千 组件( Text Field) 黑掉 。 也就是说, 该部件不应显示它持有的字符串值 。

问题回答

JPassword Field , 这是一个文字字段, 可以拼接显示

如果这不够好, 您可以将内容拖动并存储到外部、 空白或毛线或任何字段中, 并且 < code> setEnabled( false) 在上面。 如果您再次启用此字段, 请将内容放回去 。 如果有必要同时使用 < code> getText () , 您将不得不覆盖此内容, 以便您可以酌情替换所存储的内容 。

http://docs.oracle.com/javase/tuative/uiswind/misc/jlayer.html" rel="不跟随无悔者">JLayer组件 :

"https://i.sstatic.net/inljY.png" alt="JPanel前面梯度的例子"/ >

要让整个文本变成黑色调用

textfield.setForeground(new Color(0,0,0,0))
textfield.setBackground(Color.BLACK)
textfield.setOpaque(true)

以阻止它们选择文本呼叫

textfield.setFocusable(false)

您应该使用 < code>. set Veabilable( false) 隐藏文本字段, 并用黑色( 例如, < code> JLabel ) 以黑色背景替换它。 可以通过将两个组件( 标签和文本字段) 放在专用的 < code> JPannel 中, 并把它们叠叠在一起来做到这一点。 您可以选择只隐藏文本字段, 如果您不需要黑区域, 也可以这样做 。

这样做的其他方法仅涉及文本字段,但没有一种方法是防傻的,因为不同的操作系统会使滚动组件不同(esp.Mac OS X),从而压倒文本字段s paint () 方法,或改变文本颜色(如@GarrettHall所描述的)并不总是有效,例如。

Probably the easiest way to do this would be to remove the component from its container and replace it with a dummy component of the same type. This could be nicely encapsulated by creating your own component that encapsulates swapping the components (skeleton):

public class BlackOutTextField extends JPanel {

    private final JTextField realField = new JTextField();
    private final JTextField dummyField = new JTextField();
    private boolean isBlackedOut;

    {
        dummyField.setEditable(false);
        setLayout(new BorderLayout());
        add(realField, BorderLayout.CENTER);
    }

    public String getText() {
        return isBlackedOut ? "" : realField.getText();
    }

    public void setText(final String text) {
        if (!isBlackedOut)
            realField.setText(text);
    }

    public void setBlackedOut(final boolean blackedOut) {
         if (this.isBlackedOut != blackedOut) {
             this.isBlackedOut = blackedOut;
             removeAll();
             add(this.isBlackedOut ? dummyField : realField, BorderLayout.CENTER);
             revalidate();
         }
    }
}

你明白这个想法。

我举了一个小例子,使字符黑,文本框黑,也黑,正如你所要求的,使用“只加”JText Field。

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RunnableTest{    
    public static void main(String args[]){

        JFrame frame = new JFrame("frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        JTextField textfield = new JTextField();
        textfield.setPreferredSize(new Dimension(400,30));
        textfield.setForeground(Color.black);
        textfield.setBackground(Color.black);
        textfield.setSelectedTextColor(Color.black);
        textfield.setSelectionColor(Color.black);

        frame.getContentPane().add(panel);
        panel.add(textfield);
        frame.pack();

         frame.setVisible(true);    
    } 
}

这个方法 textfield. set Forework( coor. black); 将字体设置为黑色, 并且 textfield. setBackground( color. black); 将背景设置为黑色 。

textfield.setSelectedTextColor(Color.black);
textfield.setSelectionColor(Color.black);

设置选择的黑色,以便您无法看到所选择的内容。

当然,如果您不想使用 JText Field 并阻止用户复制文本,您可以使用密码字段作为替代办法。

EDIT: 如果您不想让用户使用 CTRL+C 复制文本, 在文本字段中添加一个按键适应器, 这样您就可以知道当用户同时按下两个按键时。 当然, 其他系统中还有很多其他组合, 但这不是问题所在 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...