我的 Java 有点生锈,所以请多多包涵。我的 GUI 类中有一个方法,它调用另一个返回 JList 的类文件。我遇到的问题是从 JList 中获取文本,您可以在下面看到输出示例
package com.example.tests;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.example.tests.IJ_runTestAFJ;
public class GUI_v2 extends JFrame
{
private static final long serialVersionUID = 1L;
IJ_CommonSetup setup = new IJ_CommonSetup();
Container c;
JPanel panel;
JScrollPane userScrollPane, errorScrollPane, sysScrollPane;
JTextArea tfUserError, tfSysError;
private JButton resetButton;
public JList<String> errorList;
GUI_v2()
{
resetButton = new JButton();
resetButton.setText("Click to populate TextArea");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
//test.runTest_Login(stUserName,stPwd);
updatePanel();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
panel = new JPanel();
tfSysError = new JTextArea(10,33);
tfSysError.setLineWrap(true);
tfSysError.setEditable(false);
tfSysError.setWrapStyleWord(false);
sysScrollPane = new JScrollPane(tfSysError);
sysScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(sysScrollPane);
panel.add(resetButton);
c = getContentPane();
c.add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(400,250); //width, height
setLocation(600,0);
setResizable(false);
validate();
}//close GUI
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI_v2().setVisible(true);
}
});
}
public void updatePanel()
{
errorList = new JList<String>();
errorList = setup.getErrorJList();
tfSysError.append(errorList.getComponent(1).toString());
validate();
}
}// end on class
IJ_CommonSetup.java
package com.example.tests;
import javax.swing.JLabel;
import javax.swing.JList;
public class IJ_CommonSetup{
/**
*
*/
public static String stError = new String();
public static JList<String> stJListError = new JList<String>();
public JList<String> getErrorJList(){
String error1 = new String("TestTestTestTestTestTestTestTestTestTestTestTestTestTest ");
String error2 = new String("ApplesApplesApplesApplesApplesApplesApplesApplesApplesApples ");
JLabel newError1 = new JLabel();
newError1.setText(error1);
JLabel newError2 = new JLabel(error2);
stJListError.add(newError1);
stJListError.add(newError2);
return stJListError;
}
}