gpt4 book ai didi

java - 尝试向另一个类中的 GUI 显示 HashMap 值

转载 作者:行者123 更新时间:2023-12-01 07:34:44 26 4
gpt4 key购买 nike

问题是这样的:我想检查我的所有客户名单。因此,我将使用 HashMap 并输出它们的 ic 编号和名称。如果我尝试使用终端,输出运行正常。

但现在我想将这些 HashMap 的结果输出到 GUI 中

这是到目前为止我的编码。您可以直接跳到 viewNumCust 的操作事件。这就是我尝试将 haspmap 输出到 jtextarea 的地方。

import javax.swing.*;
import java.io.*;
import java.util.*;

import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;

public class manageClientAccGui extends JFrame
{
ImageIcon image;
JLabel imgLabel,clientLabel,helloLabel,greetingLabel;
JButton createCustProfileButt,editClientAccButt,deleteClientButt,backClientButt,viewNumCust;
JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7;
JTextArea viewListCust;

public manageClientAccGui()
{

super("Team Garoking Manage Customer");

Container con = getContentPane();
con.setLayout(new BorderLayout());

image = new ImageIcon("images/garokingLogo.jpg");
imgLabel = new JLabel(image);

clientLabel = new JLabel("Manage Client Account");
clientLabel.setForeground(Color.WHITE);

helloLabel = new JLabel("Hello! ");
helloLabel.setForeground(Color.WHITE);

greetingLabel = new JLabel("What would you like to do? ");
greetingLabel.setForeground(Color.WHITE);

JScrollPane scrollPane = new JScrollPane(viewListCust,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(100, 100));

JLabel empty = new JLabel("");
JLabel empty1 = new JLabel("");
JLabel empty2 = new JLabel("");
JLabel empty3 = new JLabel("");
JLabel empty4 = new JLabel("");

createCustProfileButt = new JButton("Create Customer");
editClientAccButt = new JButton("Edit Customer Details");
deleteClientButt = new JButton("Remove Customer");
viewNumCust = new JButton("View Number of Customers");
backClientButt = new JButton("Back");
viewListCust = new JTextArea();
viewListCust.setEditable(false);

panel1 = new JPanel();
panel2 = new JPanel(new GridLayout(12,3));
panel3 = new JPanel(new GridLayout(12,3,0,3));
panel4 = new JPanel();
panel5 = new JPanel();

panel6 = new JPanel(new GridLayout(1,2));
panel7 = new JPanel(new GridLayout(1,2));

panel1.add(imgLabel);
panel1.setBackground(new Color(90,18,18));

panel2.add(clientLabel);
panel2.add(empty);
panel2.add(createCustProfileButt);
panel2.add(empty1);
panel2.add(editClientAccButt);
panel2.add(empty2);
panel2.add(deleteClientButt);
panel2.add(empty3);
panel2.add(viewNumCust);
panel2.add(empty4);
panel2.add(backClientButt);
panel2.setBackground(new Color(90,18,18));

panel6.add(helloLabel);
panel6.setBackground(new Color(90,18,18));

panel7.add(greetingLabel);
panel7.setBackground(new Color(90,18,18));

panel3.add(panel6);
panel3.add(panel7);
panel3.setBackground(new Color(90,18,18));
panel3.add(scrollPane);

panel5.add(panel3);
panel5.setBackground(new Color(90,18,18));

panel4.add(panel2);
panel4.setBackground(new Color(90,18,18));

con.add(panel4, BorderLayout.WEST);
con.add(panel1, BorderLayout.NORTH);
con.add(panel5, BorderLayout.CENTER);

Action act = new Action();

createCustProfileButt.addActionListener(act);
editClientAccButt.addActionListener(act);
deleteClientButt.addActionListener(act);
backClientButt.addActionListener(act);
viewNumCust.addActionListener(act);

setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(((dim.width-1024)/2), ((dim.height-650)/2));
setSize(1024,650);

}

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

class Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == createCustProfileButt)
{
dispose();
new Log("Accessing create customer");
new addCustGui();
}

if(e.getSource() == editClientAccButt)
{
dispose();
new Log("Accessing edit customer");
new editCustGui();
}

if(e.getSource() == deleteClientButt)
{
dispose();
new Log("Accessing remove customer");
new removeCustGui();
}

if(e.getSource() == backClientButt)
{
dispose();
new Log("Accessing main menu");
new menuGarokingGui();
}

if(e.getSource() == viewNumCust)
{
int enumer = 0;
HashMap<String, String> map = new HashMap<String, String>();

try
{
Scanner scanner = new Scanner(new FileReader("IndividualDetails.txt"));

while (scanner.hasNextLine()) {
String[] columns = scanner.nextLine().split("\t");
map.put(columns[1,], columns[2])
enumer++;
}
}catch (FileNotFoundException ex)
{
}
new Log("View latest");
viewListCust.append("The clients are : " + map.values());
viewListCust.append("The total number of clients are : " + enumer);
}
}
}
}

最佳答案

Map#values 返回对象的Collection

调用 viewListCust.append("Theclients are : "+ map.values()) 只是调用返回集合的 Collection#toString 方法,这不太可能提供内容的漂亮 View 。

相反,您必须自己迭代该集合。

viewListCust.append("The clients are :");
for (String value : map.values()) {
viewListCust.append("\n" + value);
}

现在请记住,当您读取文件并附加内容时,您正在阻塞事件调度线程,这将使您的程序看起来像是挂起的。

您可能希望在更新 UI 之前利用 SwingWorker 在后台加载文件。查看Concurrency in Swing

关于java - 尝试向另一个类中的 GUI 显示 HashMap 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13618298/

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