2014-01-12 91 views
1

我有一个JPanel其中一个JTextFiled存在。我想在此JTextFiled中显示属于哈希图集合的信息。java swing addactionlistener JButton

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

我在其他类(分公司)方法用于获取方法的所有工作:

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。当按下Add Job JButton时,属于hashmap集合的信息列在JTextFiled中。

数字如下;

enter image description here

enter image description here

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

由于我在Java中很新,所以我很难写下这个方法。

private class AddJobButtonHandler implements ActionListener{ 

      public void actionPerformed(ActionEvent e) { } 
     } 

编辑: 如果有类似下面并通过选择“添加任务”菜单项的菜单; 代码将如何更改?

enter image description here

,如果你建议/推荐任何例子,methodoligies或任何我将不胜感激。 由于提前, 塞尔维亚

回答

3
  • 你需要JTextAreaJob
  • 还什么我会推荐是覆盖在Job类的toString()方法显示的信息。像

    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; 
    } 
} 
+0

彻底解决这个问题,非常感谢。 – serb

+0

我在想什么; – serb

+0

那是什么?...... –