gpt4 book ai didi

java - 使用带有 JList 和静态数组的 ActionListener

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

今天,我再次寻求您对我工作中正在进行的项目的帮助/支持。现在我已经创建了一个 GUI,允许用户上传“ach”格式的文件。 (这只是一个具有特定格式的文本文件)。上传后,程序将使用缓冲读取器将文本行分解为四个可能的 JList。每个文本文件中最多有四个“批标题”(以 5 开头的数字),以及任意数量的“条目详细信息”(以 6 开头的数字)。 ach 文件可能如下所示:

500000000000000000001  
600000000000000000001
600000000000000000002
600000000000000000003
555555555555555555551
622222222222222222221
622222222222222222223
622222222222222222224

因此,第一个“五”数字和随后的所有“六”将是“批处理一”并显示在第一个 JList 中,第二个“五”数字和随后的所有“六”将显示在第二个 JList。在这种情况下,另外两个 JList 将为空,因为只有两个批处理。

现在这就是我想要实现的目标:

一旦用户上传文件并填充字段(我已成功完成),用户需要能够单击列表中的“六个”数字之一。一旦他们单击它,JOptionsPane 将询问他们原因代码。这是我的第一个问题,如何实现监听器。因为 JList 字段是使用字符串数组填充的,而该字符串数组稍后在程序中使用缓冲读取器填充,所以我不确定如何完成此操作。

我现在不会讨论第二个问题,因为我的重点是这个。以下是我的代码,任何帮助将不胜感激!

package nacha;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.filechooser.FileNameExtensionFilter;

public class NachaVersion3

{

final static boolean RIGHT_TO_LEFT = false;
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
static int entryDetailCountOne;
static int entryDetailCountTwo;
static int entryDetailCountThree;
static int entryDetailCountFour;
static String batchHeaderDisplayOne=" ";
static String batchHeaderDisplayTwo=" ";
static String batchHeaderDisplayThree=" ";
static String batchHeaderDisplayFour=" ";
static ArrayList<String> entryDetailListGUI = new ArrayList<String>();
static ArrayList<String> entryDetailList = new ArrayList<String>();
static ArrayList<String> entryDetailList2 = new ArrayList<String>();
static ArrayList<String> entryDetailList3 = new ArrayList<String>();
static ArrayList<String> entryDetailList4 = new ArrayList<String>();

public static void addComponentsToPane(Container pane){
if (RIGHT_TO_LEFT){
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}

Border border = BorderFactory.createRaisedBevelBorder();

JButton submit;

JLabel labelTitle;
JLabel labelEntryDetailHeader1;
JLabel labelEntryDetailHeader2;
JLabel labelEntryDetailHeader3;
JLabel labelEntryDetailHeader4;
final JList listEntryDetail1;
JList listEntryDetail2;
JList listEntryDetail3;
JList listEntryDetail4;


pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill){
c.fill = GridBagConstraints.HORIZONTAL;
}




labelTitle = new JLabel("ACH File Converter");
labelTitle.setBorder(border);
labelTitle.setFont(new java.awt.Font("Arial", Font.BOLD, 60));
labelTitle.setHorizontalAlignment(SwingConstants.CENTER);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=3;
c.gridx=0;
c.gridy=0;
pane.add(labelTitle, c);

labelEntryDetailHeader1 = new JLabel("Entry Detail Files Under Header: "+batchHeaderDisplayOne.substring(0,50));
labelEntryDetailHeader1.setBorder(border);
labelEntryDetailHeader1.setFont(new java.awt.Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 14));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=0;
c.gridy=1;
pane.add(labelEntryDetailHeader1, c);

labelEntryDetailHeader2 = new JLabel("Entry Detail Files Under Header: "+batchHeaderDisplayTwo.substring(0,50));
labelEntryDetailHeader2.setBorder(border);
labelEntryDetailHeader2.setFont(new java.awt.Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 14));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=1;
c.gridy=1;
pane.add(labelEntryDetailHeader2, c);

listEntryDetail1 = new JList(entryDetailList.toArray());
listEntryDetail1.setBorder(border);
listEntryDetail1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listEntryDetail1.setLayoutOrientation(JList.VERTICAL);
listEntryDetail1.setVisibleRowCount(10);
listEntryDetail1.setSize(200,200);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=0;
c.gridy=2;
JScrollPane scrollPane1 = new JScrollPane(listEntryDetail1);
pane.add(scrollPane1, c);

listEntryDetail2 = new JList(entryDetailList2.toArray());
listEntryDetail2.setBorder(border);
listEntryDetail2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listEntryDetail2.setLayoutOrientation(JList.VERTICAL);
listEntryDetail2.setVisibleRowCount(10);
listEntryDetail2.setSize(200,200);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=1;
c.gridy=2;
JScrollPane scrollPane2 = new JScrollPane(listEntryDetail2);
pane.add(scrollPane2, c);

labelEntryDetailHeader3 = new JLabel("Entry Detail Files Under Header: "+batchHeaderDisplayThree.substring(0,50));
labelEntryDetailHeader3.setBorder(border);
labelEntryDetailHeader3.setFont(new java.awt.Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 14));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=0;
c.gridy=3;
pane.add(labelEntryDetailHeader3, c);

labelEntryDetailHeader4 = new JLabel("Entry Detail Files Under Header: "+batchHeaderDisplayFour.substring(0,50));
labelEntryDetailHeader4.setBorder(border);
labelEntryDetailHeader4.setFont(new java.awt.Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 14));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=1;
c.gridy=3;
pane.add(labelEntryDetailHeader4, c);

listEntryDetail3 = new JList(entryDetailList3.toArray());
listEntryDetail3.setBorder(border);
listEntryDetail3.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listEntryDetail3.setLayoutOrientation(JList.VERTICAL);
listEntryDetail3.setVisibleRowCount(10);
listEntryDetail3.setSize(200,200);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=0;
c.gridy=4;
JScrollPane scrollPane3 = new JScrollPane(listEntryDetail3);
pane.add(scrollPane3, c);

listEntryDetail4 = new JList(entryDetailList4.toArray());
listEntryDetail4.setBorder(border);
listEntryDetail4.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listEntryDetail4.setLayoutOrientation(JList.VERTICAL);
listEntryDetail4.setVisibleRowCount(10);
listEntryDetail4.setSize(200,200);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=1;
c.gridy=4;
JScrollPane scrollPane4 = new JScrollPane(listEntryDetail4);
pane.add(scrollPane4, c);



submit = new JButton("Submit");
submit.setFont(new java.awt.Font("Arial", Font.BOLD, 40));
submit.setHorizontalAlignment(SwingConstants.CENTER);
submit.setVerticalAlignment(SwingConstants.CENTER);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=2;
c.gridheight=8;
c.gridx=0;
c.gridy=5;
c.ipady=153;
pane.add(submit,c);

}


private static void createAndShowGUI(){
JFrame frame = new JFrame("ACH File Converter");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addComponentsToPane(frame.getContentPane());

frame.pack();
frame.setVisible(true);

}

public static void main(String args[]){

BufferedReader br = null;
BufferedWriter bw = null;

int countFive = 0;

ArrayList<String> batchHeaderList = new ArrayList<String>();

JFileChooser chooser = new JFileChooser();
UIManager.put("ScrollPane.background", Color.BLACK);
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Please choose ACH file to upload");
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION)
{

try{
String sCurrentLine;
br = new BufferedReader(new FileReader(chooser.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"//NachaOutput.ACH"));


while((sCurrentLine = br.readLine()) !=null)
{

if (sCurrentLine.startsWith("5")){
batchHeaderList.add(sCurrentLine);
countFive++;
if (countFive == 1){
batchHeaderDisplayOne=sCurrentLine;
}else if (countFive ==2 ){
batchHeaderDisplayTwo=sCurrentLine;
}else if (countFive == 3){
batchHeaderDisplayThree=sCurrentLine;
}else if (countFive == 4){
batchHeaderDisplayFour=sCurrentLine;
}
}else if (sCurrentLine.startsWith("6") && countFive==1){
entryDetailList.add(sCurrentLine);
}else if (sCurrentLine.startsWith("6") && countFive==2){
entryDetailList2.add(sCurrentLine);
}else if (sCurrentLine.startsWith("6") && countFive==3){
entryDetailList3.add(sCurrentLine);
}


}

} catch (IOException e){
e.printStackTrace();
} finally {
try {
br.close();
bw.close();
} catch (IOException e){
e.printStackTrace();
}
}

javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGUI();
}
});

}

}
}

如果您想测试此代码以帮助我,您只需将上面的 ACH 示例文本复制并粘贴到文本文件中并将其另存为 .ACH

最佳答案

That is my first problem: how to implemenet the listener.

添加ListSelectionListener给每个JList那需要一个。在处理程序中,您可以根据选择自定义原因代码列表。然后,在模态对话框中请求相关原因代码,如 Getting the User's Input from a Dialog 所示。 .

listEntryDetail.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {

String code = (String)JOptionPane.showInputDialog(…);

}
});

另请参阅How to Write a List Selection Listener ,以及这些examples .

关于java - 使用带有 JList 和静态数组的 ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32764258/

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