有什么方法可以在文本场中设置所有结果值?它仅设置为预期的最后一个值
我正在创建一个程序,该程序将字符串输入转换为二进制/八倍字符串,并在文本字段中显示结果,但只有最后一个值(1个单独字符的二进制/八分位字符串)显示为预期。但是,还有其他方法可以显示所有值吗?
resultTextField = new JTextField();
resultTextField.setBounds(10,170,200,100);
resultTextField.setForeground(Color.green);
resultTextField.setBackground(Color.black);
resultTextField.setOpaque(true);
resultTextField.setVisible(visibilty);
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==binaryButton) {
convertToBinary();//to convert the string to binary string
}
if(e.getSource()== octaButton) {
convertToOcta();//to convert to octal string
}
}
public void convertToBinary() {
visibilty = true;
将输入文本(Maintextfield(string))转换为char数组
char[] textArray = mainTextField.getText().toCharArray();
for(int ascii : textArray)//this will convert individual characters to their ascii value
{
String binaryString = Integer.toBinaryString(ascii);//converts ascii value to binary string
resultTextField.setText(binaryString); //to get result
resultTextField.setVisible(visibilty);
//System.out.println(binaryString);
}
}
相同的东西,但如Convertobinary,但输入转换为八十位字符串
public void convertToOcta() {
visibilty = true;
String text = mainTextField.getText();
char[] textArray = text.toCharArray();
for(int ascii : textArray)
{
String octalString = Integer.toOctalString(ascii);
resultTextField.setText(octalString);
//System.out.println(octalString);
}
resultTextField.setVisible(visibilty);
}
}
I am creating a program which converts a string input to binary/octal string and shows the result in a textfield, but only last value (binary/octal string of 1 individual character ) gets shown as expected. But is there any other way to show all values?
resultTextField = new JTextField();
resultTextField.setBounds(10,170,200,100);
resultTextField.setForeground(Color.green);
resultTextField.setBackground(Color.black);
resultTextField.setOpaque(true);
resultTextField.setVisible(visibilty);
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==binaryButton) {
convertToBinary();//to convert the string to binary string
}
if(e.getSource()== octaButton) {
convertToOcta();//to convert to octal string
}
}
public void convertToBinary() {
visibilty = true;
converting input text (maintextField (String)) to char array
char[] textArray = mainTextField.getText().toCharArray();
for(int ascii : textArray)//this will convert individual characters to their ascii value
{
String binaryString = Integer.toBinaryString(ascii);//converts ascii value to binary string
resultTextField.setText(binaryString); //to get result
resultTextField.setVisible(visibilty);
//System.out.println(binaryString);
}
}
same thing but as in converToBinary but input gets converted octal string
public void convertToOcta() {
visibilty = true;
String text = mainTextField.getText();
char[] textArray = text.toCharArray();
for(int ascii : textArray)
{
String octalString = Integer.toOctalString(ascii);
resultTextField.setText(octalString);
//System.out.println(octalString);
}
resultTextField.setVisible(visibilty);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要包括已经存在的内容
You just need to include the contents that were already there
每当您从文本组件中添加/删除文本时,就会生成
documentevent
。当然,对于这项简单的任务,您可能不在乎DocumentEvents,但是在使用文本组件时,这是对未来的考虑。您可以使用简单的代码,例如:
此方法将导致多个
removeupdate()
和insertupdate()
事件,而实际上您可能只想生成一个单个insertupdate()
事件。同样,这相对效率低下,因为每次您设置文本时,都需要清除文档中的所有文本,然后用新文本重建文档。另一个选项是更新文本字段的文档:
这将导致多个
insertupdate()
事件。另外,请注意,您需要使用try/catch逻辑来处理insertstring(...)
方法上的异常。一种更简单的方法可能是使用一行
jtextarea
,然后您只需使用append(...)
方法即可附加新文本。它更新了文档,如上所示,但是它已经处理了异常逻辑,因此您无需担心它。这将导致生成多个
insertupdate()
事件。或者,如果您确实想使用
jtextfield
,则可以首先使用stringBuffer
并在将每个字符串转换时附加。然后在循环外,您可以将文本设置一次。类似:将仅生成单个
insertupdate()
事件。Whenever you add/remove text from a text component a
DocumentEvent
is generated. Of course, for this simple task, you probably don't care about the DocumentEvents, but it is something to consider for the future when working with text components.You could use simple code like:
This approach will result in multiple
removeUpdate()
andinsertUpdate()
events, when in reality you probably only want to generate a singleinsertUpdate()
event. Also this is relatively inefficient since every time you set the text you need to clear all the text in the Document and then rebuild the Document with the new text.Another option is to update the Document of the text field:
This will result in multiple
insertUpdate()
events. Also, note that you will need to use try/catch logic to handle an Exception on theinsertString(...)
method.A simpler approach might be to use a single line
JTextArea
, then you can just use theappend(...)
method to append new text. It updates the Document as shown above, but it already handles the Exception logic so you don't need to worry about it.This will result in multiple
insertUpdate()
events being generated.Or if you really want to use a
JTextField
then you can first use aStringBuffer
and append each String as it is converted. Then outside your loop you can set the text once. Something like:Only a single
insertUpdate()
event will be generated.