java swing actionlistener JButton

时间:2014-01-12 21:23:02

标签: java swing hashmap jbutton jtextfield

我有JPanel,其中存在JTextFiled。我想在此JTextFiled中显示属于hashmap集合的信息。

我的hashmap集合是:HashMap<String,Job>jobs = new HashMap < String,Job>();

我在另一个类(Branch)中的方法有获取所有Jobs的方法:

    public String getAllJobs()
 {       
        String result_jobs;
        result_jobs = " ";

        Collection<Job> jobValues = jobs.values();
        Iterator<Job> Jobiter =  jobValues.iterator();

       while(Jobiter .hasNext())
       {
       Job jo = Jobiter.next();
       result_jobs += jo.toString()+ '\n' ; 
       }

       return result_jobs; 

    }

在这个JTextFiled中,应该输入hashmap的键值,表示在上面显示的HashMap集合中声明为String的Customer Name。按下添加作业JButton时,JTextFiled中列出了属于hashmap集合的信息。

数字如下;

enter image description here

enter image description here

我试图写下actionPerformed(ActionEvent e)的方法。

由于我是Java新手,因此我很难写下这种方法。

 private class AddJobButtonHandler implements ActionListener{

          public void actionPerformed(ActionEvent e) { }
      } 

EDITED: 如果有如下所示的菜单,则选择“添加作业”菜单项; 代码将如何变化?

enter image description here

如果您建议/推荐任何示例,方法或任何内容,我将不胜感激。  提前致谢,  塞尔维亚

1 个答案:

答案 0 :(得分:3)

  • 您需要JTextArea来显示Job
  • 中的信息
  • 我还建议您覆盖toString()类中的Job方法。像

    这样的东西
    public String toString() {
        return "Job No: " + jobNum +
               "\nCustomer: " + customer +
               "\nCredit Limit: " + creditLimit
        ....
    }
    
  • 然后在actionPerformed所有你需要做的,检查文本字段中的值,然后从地图中获取值并将其显示在文本区域中。

    public void actionPerformed(ActionEvent e) {
        String customer = textField.getText();
        if (map.containsKey(customer)) {
             jta.append(String.valueOf(map.get(customer)));
             jta.append("\n***********************\n\n");
        }
    }
    

运行此示例。看看我的意思。只需在地图中键入其中一个名称,然后按按钮

enter image description here

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestMap {

    private JTextArea jta = new JTextArea(15, 30);
    private JTextField jtf = new JTextField(30);
    private JButton button = new JButton("Show Job");
    private Map<String, Job> map;

    public TestMap() {
        map = getMap();

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(jta, BorderLayout.CENTER);
        panel.add(jtf, BorderLayout.NORTH);
        panel.add(button, BorderLayout.SOUTH);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!"".equals(jtf.getText())) {
                    String customer = jtf.getText();
                    if (map.containsKey(customer)) {
                        jta.append(String.valueOf(map.get(customer)));
                        jta.append("\n***********************\n\n");
                    }
                    jtf.setText("");
                }
            }
        });

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    private Map<String, Job> getMap() {
        Map<String, Job> map = new HashMap<>();
        map.put("Paul", new Job(100, "Paul", 10000.00));
        map.put("Jim", new Job(101, "Jim", 20000.00));
        map.put("John", new Job(102, "John", 30000.00));
        map.put("Sean", new Job(103, "Sean", 40000.00));
        map.put("Shane", new Job(104, "Shane", 50000.00));
        map.put("Mike", new Job(105, "Mike", 60000.00));

        return map;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestMap();
            }
        });
    }
}

class Job {

    int jobNo;
    String customer;
    double creditLimit;

    public Job(int jobNo, String customer, double creditLimit) {
        this.jobNo = jobNo;
        this.customer = customer;
        this.creditLimit = creditLimit;
    }

    public String toString() {
        return "Job No: " + jobNo
                + "\nCustomer: " + customer
                + "\nCredit Limit: " + creditLimit;
    }
}