Java 显示ArrayList<;字符串>;在JTextArea
因此,当单击按钮时,我的方法返回字符串的arraylist,我试图在JtextArea中逐行显示字符串。这是我第一次在eclipse中使用GUI,但到目前为止,我还处于领先地位Java 显示ArrayList<;字符串>;在JTextArea,java,eclipse,swing,Java,Eclipse,Swing,因此,当单击按钮时,我的方法返回字符串的arraylist,我试图在JtextArea中逐行显示字符串。这是我第一次在eclipse中使用GUI,但到目前为止,我还处于领先地位 JButton btnNewButton_1 = new JButton("Coordinate Anomalies"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent
JButton btnNewButton_1 = new JButton("Coordinate Anomalies");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ArrayList<String> anomalies = vessels.coordinateAnomaly();
}
});
btnNewButton_1.setBounds(10, 45, 172, 23);
frame.getContentPane().add(btnNewButton_1);
JTextArea textArea = new JTextArea();
textArea.setBounds(10, 79, 172, 339);
frame.getContentPane().add(textArea);
JButton btnNewButton_1=新JButton(“坐标异常”);
btnNewButton_1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
ArrayList异常=血管。协调异常();
}
});
btnewbutton_1.后退(10,45,172,23);
frame.getContentPane().add(btnNewButton_1);
JTextArea textArea=新的JTextArea();
textArea.setBounds(10,79,172,339);
frame.getContentPane().add(textArea);
我在想我有可能做到
JButton btnNewButton_1 = new JButton("Coordinate Anomalies");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ArrayList<String> anomalies = vessels.coordinateAnomaly();
JTextArea textArea = new JTextArea();
textArea.setText(anomalies);
textArea.setBounds(10, 79, 172, 339);
frame.getContentPane().add(textArea);
}
});
JButton btnNewButton_1=新JButton(“坐标异常”);
btnNewButton_1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
ArrayList异常=血管。协调异常();
JTextArea textArea=新的JTextArea();
textArea.setText(异常);
textArea.setBounds(10,79,172,339);
frame.getContentPane().add(textArea);
}
});
这肯定不起作用,如果它真的起作用,将以ArrayList格式显示字符串,所以我应该在某些地方有一个循环,但我有点迷路了。
任何帮助都会很棒 试试这个:
for(String a : anomalies){
textArea.append(a + "\n");
}
而不是:
textArea.setText(anomalies);
当向JFrame添加JTextArea时,只需像这样在其中添加滚动窗格
添加(新的JScrollPane(textarea)) 最终听从了切基拉先生的建议
for(String a : anomalies){
if(a.equals(anomalies.get(anomalies.size()-1))){
textArea_1.append(a);
}else{
textArea_1.append(a + "\n");
}
}
根据格式,您可能希望使用append而不是settext。。。如果您只想打印数组列表,则需要(使用格式)执行Arrays.toString(anomallies),否则需要执行for循环。对于(字符串s:异常)textArea.append(s)这完全有效,但在我高亮显示列表并且列表大于框架之前,显示的列表是不可见的,是否有可滚动选项?@guy_sensei您可以在@guy_sensei frame.getContentPane()内创建JTextArea。添加(jp);-您应该添加滚动窗格,而不是textArea@guy_sensei你说的隐形到底是什么意思?让我们来看看。