用户在小程序中输入一个字符串 然后将该字符串切割成较小的字符串并加载到数组A1中。 列表框中的JList值显示在applet查看器中。但不在applet中。 JList在Panel2中。是什么阻止了applet中的值?
import java.awt.event.*;
import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashSet;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class QueryWindow extends JApplet{
JTextField textField;
JButton button, button2;
JLabel label, label2 ;
JList<String> listbox ;
JPanel thePanel;
JPanel Panel1, Panel2;
DefaultListModel<String> listModel;
public void init(){
GridBagLayout layout =new GridBagLayout();
thePanel = new JPanel();
thePanel.setLayout(layout);
GridBagConstraints gridConstraints = new GridBagConstraints();
定义组件的x,y位置 使用了两个面板。 Panel2保存JList组件。
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.gridwidth = 2;
gridConstraints.gridheight = 1;
gridConstraints.weightx = 10;
gridConstraints.weighty = 10;
gridConstraints.insets = new Insets(5,5,5,5);
gridConstraints.anchor = GridBagConstraints.CENTER;
gridConstraints.fill = GridBagConstraints.HORIZONTAL;
textField = new JTextField(" ", 60);
Font font =new Font("Helvetica", Font.PLAIN, 18);
textField.setFont(font);
button = new JButton("Enter Your Query");
label = new JLabel(" ");
thePanel.add(textField, gridConstraints);
gridConstraints.gridwidth = 1;
gridConstraints.gridx = 62;
thePanel.add(button, gridConstraints);
gridConstraints.gridwidth = 1;
gridConstraints.gridx = 1;
gridConstraints.gridy = 2;
thePanel.add(label, gridConstraints);
gridConstraints.gridwidth = 3;
gridConstraints.gridheight = 20;
gridConstraints.gridx = 1;
gridConstraints.gridy = 3;
gridConstraints.fill = GridBagConstraints.BOTH; //BOTH
Panel2 = new JPanel((LayoutManager) new FlowLayout(FlowLayout.LEFT));
label2 = new JLabel("Annotations ");
Panel2.add(label2);
listModel = new DefaultListModel<String>();
listbox = new JList<String>(listModel);
listbox.setVisibleRowCount(10);
listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Panel2.add(new JScrollPane(listbox));
thePanel.add(Panel2, gridConstraints);
创建一个将监视按钮单击的对象
GetResultsListener butListener = new GetResultsListener();
button.addActionListener(butListener);
this.add(thePanel);
this.setVisible(true);
}
按Query按钮调用基于REST的方法,切断长字符串。
class GetResultsListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String textFieldText = textField.getText();
ArrayList<String> A1 = new ArrayList<String>();
listModel.clear();
textField.setText(textFieldText);
调用基于REST的代码来注释更长的字符串
queryToAnnotate方法返回数组A1中的字符串值。我强制打印A1的内容 屏幕上。我将A1中的值分配给listModel。小程序查看器正确显示&gt; JList中的值。 但是小程序中的JList框是空的。
AnnotateQuery annotateQuery =new AnnotateQuery();
try {
annotateQuery.queryToAnnotate(textFieldText, A1);
for (int h=0; h<A1.size(); h++)
{
listModel.addElement(A1.get(h));
}
}
catch (UnsupportedEncodingException e1) {
// // TODO Auto-generated catch block
e1.printStackTrace();
}
Panel2.repaint();
A1.clear();
}
}
}
}