2016-01-21 93 views
1

我正在嘗試將ActionListeners添加到JButtons NewDeleteSearch。 我嘗試過幾種方法,但仍然無法完成這項工作。將聽衆添加到JButton

P.S我已經添加了按鈕New的動作,似乎工作,但它不會添加任何數組。

感謝您的幫助!提前致謝。

package datingrun; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class Main extends javax.swing.JFrame { 

    public static void main(String[] args) { 

     JFrame frame = new JFrame("Onomata"); 

     JLabel ll = new JLabel("Êáôá÷ùñÞóåéò"); 
     ll.setBounds(150, 20, 300, 15); 

     JLabel l2 = new JLabel("Onoma"); 
     l2.setBounds(155, 80, 300, 15); 

     JLabel l3 = new JLabel("Filo"); 
     l3.setBounds(270, 80, 100, 15); 

     JLabel l4 = new JLabel("Ilikia"); 
     l4.setBounds(280, 80, 100, 15); 

     JButton b1 = new JButton(); 
     b1.setText("New"); 
     b1.setBounds(105, 400, 80, 40); 



     JButton b2 = new JButton(); 
     b2.setText("Search"); 
     b2.setBounds(205, 400, 80, 40); 



     JButton b3 = new JButton(); 
     b3.setText("Delete"); 
     b3.setBounds(305, 400, 80, 40); 



     JTextField tf1 = new JTextField(20); 
     tf1.setSize(15, 20); 
     tf1.setBounds(130, 100, 100, 20); 

     JTextField tf2 = new JTextField(20); 
     tf2.setSize(15, 20); 
     tf2.setBounds(250, 100, 100, 20); 

     JTextArea t1 = new javax.swing.JTextArea(); 
     t1.setColumns(20); 
     t1.setRows(5); 
     t1.setEditable(false); 
     t1.setBounds(125, 200, 120, 150); 

     JTextArea t2 = new javax.swing.JTextArea(); 
     t2.setColumns(20); 
     t2.setRows(5); 
     t2.setEditable(false); 
     t2.setBounds(245, 200, 120, 150); 

     JTextArea t3 = new javax.swing.JTextArea(); 
     t3.setColumns(30); 
     t3.setRows(5); 
     t3.setEditable(false); 
     t3.setBounds(245, 220, 100, 100); 

     frame.add(ll); 
     frame.add(l2); 
     frame.add(l3); 
     frame.add(tf1); 
     frame.add(tf2); 
     frame.add(t1); 
     frame.add(t2); 
     frame.add(t3); 
     frame.add(b1); 
     frame.add(b2); 
     frame.add(b3); 

     frame.setSize(500, 500); 
     frame.setLayout(null); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 

     final ArrayList Onomata = new ArrayList(); 

     Onomata.add("Stelios Mavridis"); 
     Onomata.add("Nikos Tzouvelekis"); 
     Onomata.add("Andreas Paraskevas"); 

     final ArrayList Filo = new ArrayList(); 

     Filo.add("M"); 
     Filo.add("M"); 
     Filo.add("M"); 

     final ArrayList Ilikia = new ArrayList(); 

     Ilikia.add("24"); 
     Ilikia.add("30"); 
     Ilikia.add("40"); 

     t1.append("Onoma \n"); 
     t2.append("Filo \n"); 
     t3.append("Ilikia \n"); 

     for (int i = 0; i < Onomata.size() && i < Filo.size() && i < Ilikia.size(); i++) { 
      t1.append((String) Onomata.get(i) + "\n"); 
      t2.append((String) Filo.get(i) + "\n"); 
      t3.append(Ilikia.get(i).toString() + "\n"); 



     } 
     b1.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 

       String text = JOptionPane.showInputDialog("Insert"); 
       String item = null; 

       if (text != null) { 
        item = text.trim(); 
       } else { 
        return; 
       } 

       if (!item.isEmpty()) { 
        Onomata.add(item); 
        Filo.add(item); 
        Ilikia.add(item); 
       } 
      } 
     }); 


    } 
} 
+1

避免'空佈局,像素完美的佈局是現代UI設計中的幻想。影響組件的個體大小的因素太多,其中沒有一個可以控制。 Swing旨在與佈局經理一起工作,放棄這些將導致問題和問題的終結,您將花費越來越多的時間來嘗試糾正 – MadProgrammer

回答

3

首先查看How to Write an Action Listeners

由於您已成功獲得ActionListener註冊JButton,我們可以假設問題已解決。

測試代碼後,item被添加到您的ArrayList S,你只是不符合新的內容更新UI,也許像...

b1.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     String text = JOptionPane.showInputDialog("Insert"); 
     String item = null; 

     if (text != null) { 
      item = text.trim(); 
     } else { 
      return; 
     } 

     if (!item.isEmpty()) { 
      Onomata.add(item); 
      Filo.add(item); 
      Ilikia.add(item); 
      t1.setText(null); 
      t2.setText(null); 
      t3.setText(null); 
      for (int i = 0; i < Onomata.size() && i < Filo.size() && i < Ilikia.size(); i++) { 
       t1.append((String) Onomata.get(i) + "\n"); 
       t2.append((String) Filo.get(i) + "\n"); 
       t3.append(Ilikia.get(i).toString() + "\n"); 

      } 
     } 
    } 
}); 

現在,在運行代碼,JTextArea不適合您似乎試圖完成的任務,相反,可能考慮使用JTable,請參閱How to Use Tables以瞭解更多詳細信息。

避免使用null佈局,像素完美的佈局是現代UI設計中的幻想。影響組件的個體大小的因素太多,其中沒有一個可以控制。搖擺設計爲核心與佈局管理工作,丟棄這些會導致沒有問題,問題是,你將花費更多的時間,試圖結束整頓

使用詳情請參閱Laying Out Components Within a Container

+0

非常感謝您的快速響應!但我還有一個問題JTextArea3在哪裏應該顯示ArrayList「Ilikia」...但是當我運行該程序時,它不顯示在那裏...你能幫我解決這個問題嗎?我不認爲我錯過了一些東西,但不知道發生了什麼 – Stelios

+0

您需要在事件線程上運行所有GUI東西,而您並未這​​樣做。 – FredK

+0

使用佈局管理器,null佈局會給你這麼多的頭痛。正如我所說,你似乎在做什麼並不是最好的主意,相反,你需要使用像JTable這樣的東西 – MadProgrammer