gpt4 book ai didi

java - 当我按下按钮时,如何将 stringbuilder 的结果打印到 jtext 区域

转载 作者:太空宇宙 更新时间:2023-11-04 14:26:11 26 4
gpt4 key购买 nike

我有这段代码,我想知道如何打印//System.out.println(result);当我按下按钮时进入 jtext 区域。

我尝试使用text.settext(result);尝试使用 text.settext(result.toString());

我没主意了

package nioCount;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
public class Settings
{






public static void main(String...args) throws IOException {

String line = null;
Pattern category = Pattern.compile("^\\[(.*?)\\]$"); // matches [Cars]
Pattern itemAndQuantity = Pattern.compile("^(\\w+)=(\\d+)$"); // matches Lamborghini=6
StringBuilder result = new StringBuilder();
{


try (BufferedReader br = new BufferedReader(new FileReader("D:/test.txt"))) {
while ((line = br.readLine()) != null) {
Matcher categoryMatcher = category.matcher(line);
Matcher itemMatcher = itemAndQuantity.matcher(line);
if (categoryMatcher.matches()) {
if (result.length() > 0) { // found new category, put on new line
result.append(System.getProperty("line.separator"));
}
String categoryName = categoryMatcher.group(1); // Cars
result.append(categoryName).append(": "); // Cars:
} else if (itemMatcher.matches()) {
String item = itemMatcher.group(1); // Lamborghini
String quantity = itemMatcher.group(2); // 6
result.append(item).append(" ") // Lamborghini
.append(quantity) // Lamborghini 6
.append(", "); // Lamborghini 6,
}
}


// we are done processing the file, output the result
//System.out.println(result);


}
JTextArea text;
text = new JTextArea(result.toString());

JFrame frame = new JFrame();
frame.setForeground(Color.YELLOW);
frame.setAutoRequestFocus(false);


//text.setAlignmentX(Component.LEFT_ALIGNMENT);
// text.setHorizontalAlignment(SwingConstants.CENTER);
text.setEditable(false);
text.setBounds(100, 92, 436, 195);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(text);
frame.getContentPane().setPreferredSize(new Dimension(700,500));


JButton btnNewButton = new JButton("Count");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Reader in = null;

}
});
btnNewButton.setBounds(100, 385, 134, 50);
frame.getContentPane().add(btnNewButton);

JButton btnResetCount = new JButton("Reset Count");
btnResetCount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText(null);

}
});
btnResetCount.setBounds(402, 385, 134, 50);
frame.getContentPane().add(btnResetCount);
frame.setResizable(true);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}




}
}

最佳答案

做你想做的事情的最快方法是:

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Settings
{

public static void main(String... args) throws IOException
{
String result = aMethodThatReadsTheFileAndReturnsAString();

JTextArea text = new JTextArea(result);

JFrame frame = new JFrame();
frame.setForeground(Color.YELLOW);
frame.setAutoRequestFocus(false);
text.setEditable(false);
text.setBounds(100, 92, 436, 195);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(text);
frame.getContentPane().setPreferredSize(new Dimension(700, 500));

JButton btnNewButton = new JButton("Count");
btnNewButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
text.setText(aMethodThatReadsTheFileAndReturnsAString());
}
});
btnNewButton.setBounds(100, 385, 134, 50);
frame.getContentPane().add(btnNewButton);

JButton btnResetCount = new JButton("Reset Count");
btnResetCount.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.setText("");

}
});
btnResetCount.setBounds(402, 385, 134, 50);
frame.getContentPane().add(btnResetCount);
frame.setResizable(true);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);

}

public static String aMethodThatReadsTheFileAndReturnsAString()
{
String line = null;
Pattern category = Pattern.compile("^\\[(.*?)\\]$"); // matches [Cars]
Pattern itemAndQuantity = Pattern.compile("^(\\w+)=(\\d+)$"); // matches
// Lamborghini=6
StringBuilder result = new StringBuilder("result @"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())+" is: \n");
{

try (BufferedReader br = new BufferedReader(new FileReader(
"D:/test.txt")))
{
while ((line = br.readLine()) != null)
{
Matcher categoryMatcher = category.matcher(line);
Matcher itemMatcher = itemAndQuantity.matcher(line);
if (categoryMatcher.matches())
{
if (result.length() > 0)
{ // found new category, put on new line
result.append(System.getProperty("line.separator"));
}
String categoryName = categoryMatcher.group(1); // Cars
result.append(categoryName).append(": "); // Cars:
}
else if (itemMatcher.matches())
{
String item = itemMatcher.group(1); // Lamborghini
String quantity = itemMatcher.group(2); // 6
result.append(item).append(" ") // Lamborghini
.append(quantity) // Lamborghini 6
.append(", "); // Lamborghini 6,
}
}
}
catch(Exception x)
{
result.append(x.toString());
}

return result.toString();
}
}
}

text.appendText(aMethodThatReadsT​​heFileAndReturnsAString()) 如果您希望将输出添加到 TextArea 中已显示内容的末尾。

正确的方法是按下按钮时开始读取,并在操作后显示结果。读取应该由另一个为此目的启动的线程来完成。您可能想阅读 Concurrency in Swing

关于java - 当我按下按钮时,如何将 stringbuilder 的结果打印到 jtext 区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26638656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com