Java 试图向另一个类中的GUI显示hashmap值
问题是: 我想查看我所有的客户名单。因此,我将使用哈希映射并输出它们的ic num和名称。如果我尝试使用终端,输出运行正常 但现在我想将这些散列映射的结果输出到GUI中 这是我到目前为止的编码。您可以跳过viewNumCust的“操作权限”事件。这就是我试图将haspmap输出到jtextarea的地方Java 试图向另一个类中的GUI显示hashmap值,java,swing,user-interface,hash,hashmap,Java,Swing,User Interface,Hash,Hashmap,问题是: 我想查看我所有的客户名单。因此,我将使用哈希映射并输出它们的ic num和名称。如果我尝试使用终端,输出运行正常 但现在我想将这些散列映射的结果输出到GUI中 这是我到目前为止的编码。您可以跳过viewNumCust的“操作权限”事件。这就是我试图将haspmap输出到jtextarea的地方 import javax.swing.*; import java.io.*; import java.util.*; import java.awt.*; import java.awt.e
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);
}
}
}
}
Mapvalues返回对象的集合
调用viewListCust.appendThe Client are:+map.values只需调用返回集合的CollectiontoString方法,该方法不可能提供内容的漂亮视图
相反,您必须自己迭代集合
viewListCust.append("The clients are :");
for (String value : map.values()) {
viewListCust.append("\n" + value);
}
现在请记住,当您读取文件并附加内容时,您正在阻止事件调度线程,这将使您的程序看起来好像挂起了
您可能希望在更新UI之前利用SwingWorker在后台加载文件。签出在将viewListCust传递给JScrollPane之前,您需要实例化它,如下所示:
当您将viewListCust传递到JScrollPane构造函数时,它当前为空。1将}catch FileNotFoundException ex{}更改为}catch FileNotFoundException ex{ex.printStackTrace;}并报告输出。2潜在的长时间运行任务,如文件I/O,不应在EDT中完成。相反,对于长时间运行的任务,使用SwingWorker。请参阅更多细节。还考虑JTALE,如图所示。谢谢安得烈和TrasVald。我目前正在学习printstacktrace,我对GUI还比较陌生。作为记录,我的客户名单不会增长那么多,但我知道你的意思。目前正在研究swingworker@SalehinSuhaimi如果某个答案解决了您的问题,请确保将其标记为已接受:我是GUI新手,所以我现在就来看看swingworker。但在此之前,我尝试了ur编码,它说我看不到“字符串值:map.values”行中的map.values{'它说AbstractMap.values字段不可见?您需要在new LogView latest下替换两行;在action performed方法中…我确实在日志下替换了它。我得到了错误。可能默认情况下这些值是私有的?抱歉,我的错误,键入错误。for循环应该为字符串value:map.values读取{.Updated Answer非常感谢。它现在运行得很好。我确实实例化了它,代码只是乱七八糟:或者我错过了什么?你在创建JScrollPane后实例化了它。你需要在之前完成它。换句话说,只需复制你的viewListCust代码,然后按照我显示的顺序粘贴到创建滚动窗格的地方。啊,我忘了它一定很整齐。非常感谢
viewListCust = new JTextArea();
viewListCust.setEditable(false);
JScrollPane scrollPane = new JScrollPane(viewListCust,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(100, 100));