[英]--Invisible, Not visible, but editable JTextField
How do I make a textfield, that is editable, but invisible? 如何创建一个可编辑但不可见的文本字段? I need to give parameters to my program, but I don't want the users to see what they are actually putting into the input.
我需要给程序提供参数,但是我不希望用户看到他们实际输入的内容。 If I set it to invisible, I can't edit it's contents either.
如果将其设置为不可见,我也将无法编辑其内容。 I tried making it transparent like this: Making a JButton invisible, but clickable?
我尝试将其透明化,如下所示: 使JButton不可见,但可单击? but for some reason, the textfield still shows.
但由于某些原因,文本字段仍然显示。 I also tried using layeredpanes, but I can only put the textfield on top of them, not the other way, NetBeans just moves them around, so everything fit.
我也尝试过使用分层窗格,但是我只能将文本字段放在它们之上,而不能用其他方法,NetBeans只是将它们移动,因此一切都适合。
I'm open to other ideas, the input is a string followed by an "enter". 我对其他想法持开放态度,输入内容是一个字符串,后跟一个“输入”。
I think I asked the question wrong. 我想我问错了这个问题。 The problem isn't that the text is visible, but the whole text field is visible.
问题不在于文本可见,而是整个文本字段可见。 The input is a string from the barcode reader, so nothing needed to be shown at all.
输入是来自条形码读取器的字符串,因此完全不需要显示任何内容。
EDIT: We solved it in a different way. 编辑:我们以不同的方式解决了它。 We added a white line to the top of the background image, put the textfield there, and changed every of it's colours to white.
我们在背景图片的顶部添加了一条白线,将文本字段放在此处,并将每种颜色更改为白色。
I think you can use a JPasswordField for your purpose the user can see the number of characters that he write but no the content 我认为您可以将JPasswordField用于您的目的,用户可以看到他编写的字符数,但看不到内容
https://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html https://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html
New idea: 新主意:
you can add a changelistener to your jtextfield and when the user write any character it save to a StringBuilder whit the append method, then set the Jtextfield to null automatically to clear jtextfield 您可以在jtextfield中添加一个changelistener,当用户编写任何字符时,将其保存到带有append方法的StringBuilder中,然后将Jtextfield自动设置为null以清除jtextfield
whit this way all the characters that user write will store in a StringBuilder and when he clicks the enter button you will have the String that user write character to character 这样,用户写入的所有字符都将存储在StringBuilder中,当他单击Enter按钮时,您将获得用户将字符写入字符的String
then you only need to call toString() method of StingBuilder to get the complete String 那么您只需要调用StingBuilder的toString()方法即可获取完整的String
I think that adding keylistener to jpanel would solve your issues, try something like this: 我认为向jpanel添加keylistener可以解决您的问题,请尝试如下操作:
String str = ""; // global
public void yourMethod() {
JFrame yourFrame = new JFrame();
JPanel yourPanel = new JPanel();
yourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
yourFrame.add(yourPanel);
yourPanel.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println(str);// here you can use switch for cases that you want or whatever you want to do with string, I simply print it out
str = "";
} else {
str += e.getKeyChar();
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
});
// jpanel must be focused if you want key listener to work
yourPanel.setFocusable(true);
yourPanel.requestFocusInWindow();
yourFrame.setSize(300, 250);
yourFrame.setVisible(true);
}
First of all I know I'm late and it's no more help for you but maybe it is for others. 首先,我知道我来晚了,这不再对您有帮助,但也许对其他人有帮助。
What i did was to just set the textfield in the corner and set both width and height to 1 (like this: TextField.setBounds(0, 0, 1, 1);
) which makes it pretty much impossible to be seen. 我所做的只是将文本字段设置在角落,并将宽度和高度都设置为1(例如:
TextField.setBounds(0, 0, 1, 1);
),这使得几乎看不到它。 This enables you to still use it. 这使您仍然可以使用它。 I know this technically doesn't make it invisible but it is very close.
我知道这在技术上不会使其不可见,但它非常接近。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.