问题遇到的现象和发生背景
这是题目要求:
UI control- JList
问题相关代码,请勿粘贴截图
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
public class lab03 {
public static void main(String[] args) {
Window window = new Window();
window.run();
}
}
class Window {
private JFrame jFrame = new JFrame("Gu siyi_21321302");
private Button button1 = new Button("Add new purchases info");
private Button button2 = new Button("Remove the purchases by its number");
private Button button3 = new Button("Print");
private Button button4 = new Button("Print");
private TextField textField1 = new TextField();
private TextField textField2 = new TextField();
private TextField textField3 = new TextField();
private TextField textField4 = new TextField();
private Box box1 = Box.createVerticalBox();
private Box box = Box.createHorizontalBox();
private Box box2 = Box.createHorizontalBox();
private Box box3 = Box.createHorizontalBox();
private Box box4 = Box.createHorizontalBox();
private Box box5 = Box.createHorizontalBox();
private Box box6 = Box.createVerticalBox();
private List listTag = new List();
private Label labelTag = new Label("name");
private Label labelTitle = new Label("comment");
private Label labelDes = new Label("amount of credits spent");
private Label labelDate = new Label("data of purchase");
private HashMap<String,Data> hashSetHashMap = new HashMap<>();
public void run(){
box2.add(labelTag);
box2.add(textField1);
box3.add(labelTitle);
box3.add(textField2);
box4.add(labelDes);
box4.add(textField3);
box5.add(labelDate);
box5.add(textField4);
box6.add(box2);
box6.add(box3);
box6.add(box4);
box6.add(box5);
box.add(button1);
box.add(button2);
box.add(button3);
box.setSize(400,100);
box1.add(listTag);
box1.add(box);
box1.add(box6);
jFrame.add(box1);
jFrame.setVisible(true);;
jFrame.setSize(400,400);
jFrame.setLocation(440,0);
jFrame.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = textField1.getText();
String comment = textField2.getText();
String amount = textField3.getText();
String dataop = textField4.getText();
Data data = new Data(name,comment,amount,dataop);
hashSetHashMap.put(data.getName(),data);
listTag.add(name);
}
});
listTag.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String target = listTag.getSelectedItem();
Data data = hashSetHashMap.get(target);
textField1.setText(data.getName());
textField2.setText(data.getComment());
textField3.setText(data.getAmount());
textField4.setText(data.getData0f());
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String target = listTag.getSelectedItem();
String title = textField2.getText();
String des = textField3.getText();
String date = textField4.getText();
Data data = new Data(target,title,des,date);
hashSetHashMap.put(data.getName(),data);
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String target = listTag.getSelectedItem();
hashSetHashMap.remove(target);
listTag.remove(target);
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFrame jFrame1 = new JFrame("search");
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("name");
jComboBox.addItem("comment");
jComboBox.addItem("amount");
jComboBox.addItem("data");
TextField tf = new TextField();
Button b1 = new Button("search");
Box boxx = Box.createHorizontalBox();
boxx.add(jComboBox);
boxx.add(tf);
boxx.add(b1);
jFrame1.add(boxx);
jFrame1.setVisible(true);;
jFrame1.setLocation(840,200);
jFrame1.setSize(400,80);
jFrame1.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String target = jComboBox.getSelectedItem().toString();
String m = tf.getText();
if(target==null) {
for(String s : hashSetHashMap.keySet()){
if(s.equals(m)){
Data data = hashSetHashMap.get(m);
textField1.setText(data.getName());
textField2.setText(data.getComment());
textField3.setText(data.getAmount());
textField4.setText(data.getData0f());
}
}
}
switch (target){
case "name":
for(String s : hashSetHashMap.keySet()){
if(s.equals(m)){
Data data = hashSetHashMap.get(m);
textField1.setText(data.getName());
textField2.setText(data.getComment());
textField3.setText(data.getAmount());
textField4.setText(data.getData0f());
}
}
break;
case "comment":
Data data = selectByTitle(m);
textField1.setText(data.getName());
textField2.setText(data.getComment());
textField3.setText(data.getAmount());
textField4.setText(data.getData0f());
break;
case "amount":
Data data1 = selectByDes(m);
textField1.setText(data1.getName());
textField2.setText(data1.getComment());
textField3.setText(data1.getAmount());
textField4.setText(data1.getData0f());
break;
case "data":
Data data2 = selectByDate(m);
textField1.setText(data2.getName());
textField2.setText(data2.getComment());
textField3.setText(data2.getAmount());
textField4.setText(data2.getData0f());
break;
}
}
});
}
});
}
public Data selectByTitle(String title){
for(String key :hashSetHashMap.keySet()){
Data data = hashSetHashMap.get(key);
if(data.getComment().equals(data)){
return data;
}
}
return null;
}
public Data selectByDes(String des){
for(String key :hashSetHashMap.keySet()){
Data data = hashSetHashMap.get(key);
if(data.getAmount().equals(data)){
return data;
}
}
return null;
}
public Data selectByDate(String date){
for(String key :hashSetHashMap.keySet()){
Data data = hashSetHashMap.get(key);
if(data.getData0f().equals(data)){
return data;
}
}
return null;
}
}
class Data{
private String name;
private String comment;
private String amount;
private String data;
public String getName() {
return name;
}
public void setName(String tag) {
this.name = name;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getData0f() {
return data;
}
public void setDataOf(String data) {
this.data = data;
}
public Data() {
}
public Data(String name, String comment, String amount, String data) {
this.name = name;
this.comment = comment;
this.amount = amount;
this.data = data;
}
}
运行结果及报错内容
不知道怎么实现题目输入删除,打印之类的要求
我的解答思路和尝试过的方法
我想要达到的结果
程序能够满足题目要求的运行