2012-08-08 83 views
3

我寫了下面的代碼,其中有一個文本字段和一個按鈕。只要輸入一個字符並按下該按鈕,就會創建一個標籤,其標題與字段中輸入的內容相同。如何以及在哪裏添加ActionListener在我的代碼中?

幾個選項卡可以創造同樣的方式.....現在又新標籤,文本字段和按鈕存在長期使用文本窗格中顯示結果....

我想在每個標籤的文本窗格中顯示輸入到文本字段中的文本...

現在請帶領我瞭解如何以及在哪裏放置了標籤按鈕的偵聽器....並且推薦任何其他必需的聽衆(我認爲應該有另一個聽衆來引導我到焦點或選定的標籤)。

應該提到的是,我已經將這些標籤添加到數組列表中以供重用,但我不知道我是否正確或可以如何使用它?

package test; 

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.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextField; 
import javax.swing.JTextPane; 

public class TestGUI extends JFrame { 


    private JTextField jTextField1; 
    private JButton jButton1; 
    static ArrayList<JPanel> ary = new ArrayList<JPanel>(); 
    private int tabIndex = 0; 
    static int index = 0; 
    private JTabbedPane tabbedPane; 

    /** 
    * @param args 
    */ 
    public TestGUI() { 

     super("Testing Tab Frame"); 
     setLayout(null); 

     Handler but1 = new Handler(); 

     jTextField1 = new JTextField(); 
     jTextField1.setVisible(true); 
     jTextField1.setBounds(12, 12, 85, 30); 
     add(jTextField1); 

     jButton1 = new JButton("Button1"); 
     jButton1.setVisible(true); 
     jButton1.setBounds(130, 12, 85, 30); 
     add(jButton1); 
     jButton1.addActionListener(but1); 

     tabbedPane = new JTabbedPane(); 
     tabbedPane.setBounds(12, 54, 200, 150); 
     tabbedPane.setVisible(false); 
     add(tabbedPane); 
     pack(); 
     setSize(250, 110); 
     setLocationRelativeTo(null); 

    } 

    private class Handler implements ActionListener { 

     public void actionPerformed(ActionEvent evt) { 
      String input = jTextField1.getText(); 
      if (!input.isEmpty()) { 
       setSize(250, 250); 
       JPanel inst = createPanel(input); 
       inst.setVisible(true); 
       tabbedPane.addTab(Integer.toString(tabIndex), inst); 
       tabbedPane.setVisible(true); 
      } 

     } 
    } 

    protected JPanel createPanel(String input) { 
     JPanel inst = new JPanel(); 
     inst.setVisible(true); 
     JTextField textField = new JTextField(); 
     textField.setVisible(true); 
     textField.setBounds(12, 12, 80, 30); 
     JButton button = new JButton(); 
     button.setVisible(true); 
     button.setBounds(100, 12, 80, 30); 
     JTextPane textPane = new JTextPane(); 
     textPane.setBounds(12, 54, 168, 40); 
     inst.add(textPane); 
     textPane.setVisible(true); 
     inst.setLayout(null); 
     inst.add(button); 
     inst.add(textField); 
     ary.add(inst); 
     tabIndex = index; 
     index++; 
     return inst; 
    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     TestGUI inst = new TestGUI(); 
     inst.setVisible(true); 
    } 

} 
+4

請設置您的職位,以一個具體的問題。 – Keppil 2012-08-08 23:30:50

+0

@ MadProgramer&Keppil:我刪除了關於體系結構的問題......請僅考慮聽衆......;) – msc87 2012-08-08 23:42:27

+0

@ msc87你是什麼意思?「我想顯示輸入到其文本字段的文本每個選項卡中的文本窗格......「 - 哪個文本字段?主框架上還是標籤中的主框架? – MadProgrammer 2012-08-08 23:44:25

回答

6

您可以將ActionListener添加到createPanel方法中的按鈕。所以,你的方法是這樣的(做什麼,你居然想用文字做一些假設,因爲它是不明確):

protected JPanel createPanel(String input) { 
    JPanel inst = new JPanel(); 
    inst.setVisible(true); 
    final JTextField textField = new JTextField(); 
    textField.setVisible(true); 
    textField.setBounds(12, 12, 80, 30); 
    JButton button = new JButton();   
    button.setVisible(true); 
    button.setBounds(100, 12, 80, 30); 
    final JTextPane textPane = new JTextPane(); 
    textPane.setBounds(12, 54, 168, 40); 
    inst.add(textPane); 
    textPane.setVisible(true); 

    button.addActionListener(new ActionListener(){ 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      textPane.setText(textPane.getText() + textField.getText()); 
     }}); 

    inst.setLayout(null); 
    inst.add(button); 
    inst.add(textField); 
    ary.add(inst); 
    tabIndex = index; 
    index++; 
    return inst; 
} 
4

here顯示TabComponentsDemo的修改示出了一個方法來重命名標籤。它在每個窗格上收聽JButton,但JTextField上的ActionListener也應該發送。

+0

我讀過它,但我不知道如何在我的代碼中使用它...對不起,但我是一個初學者在JAVA ... – msc87 2012-08-09 00:44:26

+0

我明白。修改[示例](http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#eg)是我學習自定義組件的方式。我希望你會發現我的實驗很有用。 – trashgod 2012-08-09 00:50:30

3

不同點,你可以用它讓你的用戶界面更穩健:

  • Absolute Positioning開始真的不是一個好主意。 請仔細閱讀鏈接的第一段,以獲得更多有關 信息的信息,說明爲何不使用此方法使用 LayoutManagers
  • 沒有必要對各種 JComponent小號明確使用setVisible(true);因爲你是在你的代碼做什麼,因爲當你做的 父可見,所有的子組件將被同樣設置爲可見。
  • 呼籲像pack()/setVisible(true/false)必須在 EDT-事件分派線程來完成,而不是從主 方法調用它們。欲瞭解更多信息,請閱讀Concurrency in Swing

看一看你這個修改後的代碼,如果你需要更深入地瞭解這個請不要問:

import java.awt.BorderLayout; 
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.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextField; 
import javax.swing.JTextPane; 
import javax.swing.SwingUtilities; 

public class TestGUI extends JFrame { 

    private JPanel contentPane; 
    private JTextField jTextField1; 
    private JButton jButton1; 
    static ArrayList<JPanel> ary = new ArrayList<JPanel>(); 
    private int tabIndex = 0; 
    static int index = 0; 
    private JTabbedPane tabbedPane; 

    public TestGUI() { 

     super("Testing Tab Frame"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     contentPane = new JPanel(); 
     contentPane.setLayout(new BorderLayout(5, 5)); 

     Handler but1 = new Handler(); 
     JPanel footerPanel = new JPanel(); 

     jTextField1 = new JTextField(10); 
     footerPanel.add(jTextField1); 

     jButton1 = new JButton("Create TAB"); 
     footerPanel.add(jButton1); 
     jButton1.addActionListener(but1); 

     tabbedPane = new JTabbedPane(); 

     contentPane.add(tabbedPane, BorderLayout.CENTER); 
     contentPane.add(footerPanel, BorderLayout.PAGE_END); 

     setContentPane(contentPane);  
     setSize(300, 300); 
     setLocationRelativeTo(null); 
    } 

    private class Handler implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
      String input = jTextField1.getText(); 
      if (!input.isEmpty()) { 

       JPanel inst = createPanel();     
       tabbedPane.addTab(input, inst); 
       ary.add(inst);    

       jTextField1.setText(""); 
       contentPane.revalidate(); 
       contentPane.repaint();    
      } 
     } 
    } 

    protected JPanel createPanel() { 

     JPanel inst = new JPanel(); 
     inst.setLayout(new BorderLayout(5, 5)); 

     final JTextPane textPane = new JTextPane(); 

     JPanel footerPanel = new JPanel(); 
     final JTextField textField = new JTextField(10); 
     JButton button = new JButton("SHOW"); 
     button.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent ae) 
      { 
       if (textField.getDocument().getLength() > 0) 
        textPane.setText(textField.getText()); 
       textField.setText(""); 
      } 
     }); 
     footerPanel.add(textField); 
     footerPanel.add(button); 

     inst.add(textPane, BorderLayout.CENTER); 
     inst.add(footerPanel, BorderLayout.PAGE_END);  

     return inst; 
    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       TestGUI inst = new TestGUI(); 
       inst.setVisible(true); 
      } 
     }); 
    } 

} 
相關問題