guy*_*sei 2 java eclipse swing
因此,当单击一个按钮时,我的方法返回一个字符串的arraylist,我试图在JtextArea中逐行显示字符串.这是我第一次在eclipse中玩GUI,但到目前为止我还在
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);
Run Code Online (Sandbox Code Playgroud)
我以为我可能会做
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);
}
});
Run Code Online (Sandbox Code Playgroud)
这绝对不起作用,如果它确实会在ArrayList格式中显示字符串,那么我应该有一个循环,但我有点迷失.
任何帮助都是极好的.
试试这个:
for(String a : anomalies){
textArea.append(a + "\n");
}
Run Code Online (Sandbox Code Playgroud)
代替:
textArea.setText(anomalies);
Run Code Online (Sandbox Code Playgroud)