有什么方法可以在文本场中设置所有结果值?它仅设置为预期的最后一个值

发布于 2025-02-01 04:53:40 字数 1597 浏览 2 评论 0原文

我正在创建一个程序,该程序将字符串输入转换为二进制/八倍字符串,并在文本字段中显示结果,但只有最后一个值(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 技术交流群。

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

发布评论

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

评论(2

◇流星雨 2025-02-08 04:53:40

您只需要包括已经存在的内容

String binaryString = Integer.toBinaryString(ascii);//converts ascii value to binary string
resultTextField.setText(resultTextField.getText() + binaryString); //to get result

You just need to include the contents that were already there

String binaryString = Integer.toBinaryString(ascii);//converts ascii value to binary string
resultTextField.setText(resultTextField.getText() + binaryString); //to get result
左岸枫 2025-02-08 04:53:40

每当您从文本组件中添加/删除文本时,就会生成documentevent。当然,对于这项简单的任务,您可能不在乎DocumentEvents,但是在使用文本组件时,这是对未来的考虑。

您可以使用简单的代码,例如:

resultTextField.setText( resultTextField.getText() + octalString );

此方法将导致多个removeupdate()insertupdate()事件,而实际上您可能只想生成一个单个insertupdate()事件。同样,这相对效率低下,因为每次您设置文本时,都需要清除文档中的所有文本,然后用新文本重建文档。

另一个选项是更新文本字段的文档:

Document doc = resultTextField().getDocument().
doc.insertString(...).

这将导致多个insertupdate()事件。另外,请注意,您需要使用try/catch逻辑来处理insertstring(...)方法上的异常。

一种更简单的方法可能是使用一行jtextarea,然后您只需使用append(...)方法即可附加新文本。它更新了文档,如上所示,但是它已经处理了异常逻辑,因此您无需担心它。

//resultTextField.setText(octalString);
resultTextArea.append(octalString);

这将导致生成多个insertupdate()事件。

或者,如果您确实想使用jtextfield,则可以首先使用stringBuffer并在将每个字符串转换时附加。然后在循环外,您可以将文本设置一次。类似:

StringBuffer buffer = new StringBuffer();

for(int ascii : textArray)
{
    String octalString = Integer.toOctalString(ascii);
    //resultTextField.setText(octalString);
    buffer.append(octalString);
}

resultTextField.setText(buffer.toString());

将仅生成单个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:

resultTextField.setText( resultTextField.getText() + octalString );

This approach will result in multiple removeUpdate() and insertUpdate() events, when in reality you probably only want to generate a single insertUpdate() 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:

Document doc = resultTextField().getDocument().
doc.insertString(...).

This will result in multiple insertUpdate() events. Also, note that you will need to use try/catch logic to handle an Exception on the insertString(...) method.

A simpler approach might be to use a single line JTextArea, then you can just use the append(...) 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.

//resultTextField.setText(octalString);
resultTextArea.append(octalString);

This will result in multiple insertUpdate() events being generated.

Or if you really want to use a JTextField then you can first use a StringBuffer and append each String as it is converted. Then outside your loop you can set the text once. Something like:

StringBuffer buffer = new StringBuffer();

for(int ascii : textArray)
{
    String octalString = Integer.toOctalString(ascii);
    //resultTextField.setText(octalString);
    buffer.append(octalString);
}

resultTextField.setText(buffer.toString());

Only a single insertUpdate() event will be generated.

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