gpt4 book ai didi

java - 从 JButton ActionListener 填充 JList

转载 作者:行者123 更新时间:2023-12-01 12:43:21 25 4
gpt4 key购买 nike

按下按钮后尝试填充 JList 时遇到很多问题。下面的代码使用了我之前成功使用过的技术,但我一直无法使其工作。目标是在按下按钮后运行测试,并在单独的 JList 中显示通过的 url 和失败的 url。

Action 监听器:

//Start button--starts tests when pressed.
JButton start = new JButton("Start");
start.setPreferredSize(new Dimension(400, 40));
start.setAlignmentX(Component.CENTER_ALIGNMENT);
start.addActionListener(new Web(urlA, codeA, cb, passJ, failJ));
panel2.add(start);

Action 监听器方法:

    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;


public class Web implements ActionListener {
private ArrayList<String> urls;
private ArrayList<Integer> statusCodes;
private JComboBox cb;

private JList passJ = new JList();
private JList failJ = new JList();

//constructor--allows other values to be used
public Web(ArrayList<String> urls, ArrayList<Integer> statusCodes, JComboBox cb, JList passList, JList failList ){
this.urls = urls;
this.statusCodes = statusCodes;
this.cb = cb;
this.passJ = passJ;
this.failJ = failJ;

}


@Override
public void actionPerformed(ActionEvent event){

ArrayList<String> resultsP = new ArrayList<String>();
ArrayList<String> resultsF = new ArrayList<String>();

//get source
JButton start = (JButton) event.getSource();


//get value from combobox
String selected = cb.getSelectedItem().toString();

if(selected.equals("ALL")){

}

if(selected.equals("STATUS CODE")){

for(int i = 0; i < urls.size(); i++){


try {
URL u = new URL(urls.get(i));
HttpURLConnection connection = (HttpURLConnection)u.openConnection(); //open connection and cast to HttpURLConnection
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();

if (code == statusCodes.get(i)){
System.out.println(i + "."+ urls.get(i)+" \t\t\t PASS");
resultsP.add(urls.get(i));

}
else{
System.out.println(i + "." +urls.get(i)+ "\t\t\t FAIL");
resultsF.add(urls.get(i));


}

} catch (MalformedURLException | ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

for (String str: resultsP){
System.out.println(str);
}
System.out.println("/////////////////////////////////////////////////////////////////////////////////");
for (String str: resultsF){
System.out.println(str);
}

passJ.removeAll();
failJ.removeAll();

passJ.setListData(resultsP.toArray());
failJ.setListData(resultsF.toArray()) ;

passJ.repaint();
failJ.repaint();


}//StatusCodeTest


}
}

如何将列表添加到 GUI:

        JList passJ = new JList(urlA.toArray()); 

JScrollPane scroll1 = new JScrollPane(passJ);
scroll1.setPreferredSize(new Dimension (700, 150));
scroll1.setMaximumSize( scroll1.getPreferredSize() );
scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

panel2.add(scroll1);
panel2.add(Box.createRigidArea(new Dimension(0,50)));

JList failJ = new JList(urlA.toArray());
JScrollPane scroll2 = new JScrollPane(failJ);
scroll2.setPreferredSize(new Dimension(700, 150));
scroll2.setMaximumSize(scroll1.getPreferredSize());
scroll2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

panel2.add(scroll2);
//spacer
panel2.add(Box.createRigidArea(new Dimension(0,25)));

任何GUI舞蹈将不胜感激。

最佳答案

  1. 您似乎有 passJ 的不同实例/failJ在你的Web类和GUI类。
  2. passJ.removeAll(); failJ.removeAll();不清除 JList 的项目,来自 Container 的方法.

这是向 JList 添加/清除项目的简单示例:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestFrame extends JFrame {

private JList<Integer> normal;
private JList<Integer> fail;
private Integer[] vals;

public TestFrame() {
init();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}

private void init() {
normal = new JList<Integer>(new DefaultListModel<Integer>());
fail = new JList<Integer>(new DefaultListModel<Integer>());

vals = new Integer[]{1,2,3,4,5,6,7,8,9,33};
JButton add = new JButton("collect data");
add.addActionListener(getCollectListener());
JButton clear = new JButton("clear data");
clear.addActionListener(getClearListener());

JPanel p = new JPanel();
p.add(new JScrollPane(normal));
p.add(new JScrollPane(fail));

JPanel btnPanel = new JPanel();
btnPanel.add(add);
btnPanel.add(clear);
add(p);
add(btnPanel,BorderLayout.SOUTH);
}

private ActionListener getClearListener() {
return new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
((DefaultListModel<Integer>)normal.getModel()).removeAllElements();
((DefaultListModel<Integer>)fail.getModel()).removeAllElements();
}
};
}

private ActionListener getCollectListener() {
return new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
for(Integer i : vals){
if(i%3==0){
((DefaultListModel<Integer>)normal.getModel()).addElement(i);
} else {
((DefaultListModel<Integer>)fail.getModel()).addElement(i);
}
}
}
};
}

public static void main(String args[]) {
new TestFrame();
}

}

关于java - 从 JButton ActionListener 填充 JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24888853/

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