如何使用Listener将字符串传递给另一个类

时间:2012-03-03 12:14:57

标签: java swing listener jbutton setter

我正在编写一个简单的浏览器,它有两个类,SimpleBrowser和MyTabbedPane。  SimpleBrowser有一些Gui组件,如JButton和TextField,而MyTabbedPane有tabbedpane,它有JEditorPane。 我想将在SimpleBrowser JTextField中输入的搜索字符串传递给MyTabbedPane中的JEditorPane 我不知道如何使用getter和setter 这里有一些我的代码

 package com.mysimplebrowser;  

import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import javax.swing.*;  

 class SimpleBrowser extends JFrame{  
    private JTextField myField;  
    private JButton myButton;    
    private JPanel myPanel;  
    private MyTabbedPane myTabbedPane;  

    public SimpleBrowser(){  
        setSize(400, 400);  

        myPanel = new JPanel(); // panel  
        myField = new JTextField();  
        myPanel.add(myField);  

        myButton = new JButton("Search");  
        myButton.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                // how do i go about here  
              }  
        });  
        myPanel.add(myButton);  
        // i have left out some code  

        add(myPanel);  
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
    }  

    class MyTabbedPane extends JTabbedPane{  
        private String searchWord;  
        private JEditorPane myEditorPane;  

        public MyTabbedPane(){  
            // i have left some code out  
            myEditorPane = new JEditorPane();  
            // i have left out some code for the HTML kit  

            // i want later to add google Ajax api code or the depreciated  
            // google SOAP jar apis but am stuck on how to pass the String  
            // entered in the class SimpleBrowser JTextField into  
            // MyTabbedPane private String SearchWord  

            String myUrl = "http://localhost/Good" + searchWord + "html";  
            myEditorPane.setPage(myUrl); // left out some try ... catch code  
        }  
    }  

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

2 个答案:

答案 0 :(得分:3)

你是对的 - 使用getter和setter你也是正确的,你不希望SimpleBrowser的按钮的ActionListener在其actionPerformed中创建一个新的MyTabbedPane对象,因为你的程序应该只有一个MyTabbedPane对象。在下面的例子中,我调用了“setter”方法sendUrlText(...),因为它就是它所做的(无论如何)。在类之间传递信息对于Swing代码和非GUI代码没有区别:

class SimpleBrowser extends JFrame {
   private JTextField myField;
   private JButton myButton;
   private JPanel myPanel;
   private MyTabbedPane myTabbedPane = new MyTabbedPane(this);

   public SimpleBrowser() {
      setSize(400, 400); // should be setting preferredSize!

      myPanel = new JPanel(); // panel
      myField = new JTextField();
      myPanel.add(myField);

      myButton = new JButton("Search");
      myButton.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            String urlText = myField.getText();
            myTabbedPane.sendUrlText(urlText);
         }
      });
      myPanel.add(myButton);
      // i have left out some code

      add(myPanel);
      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   }

   class MyTabbedPane extends JTabbedPane {
      private String searchWord;
      private JEditorPane myEditorPane = new JEditorPane();
      private SimpleBrowser simpleBrowser; // you'll need this later, 
      // if  you separate these two classes -- which you should do.

      public MyTabbedPane(SimpleBrowser simpleBrowser) {
         this.simpleBrowser = simpleBrowser; 
      }

      public void sendUrlText(String urlText) {
         // TODO set editor pane's page here using urlText
         try {
            myEditorPane.setPage(urlText);
         } catch (IOException e) {
            e.printStackTrace();
         }

      }
   }

   private static void createAndShowGui() {
      SimpleBrowserTest mainPanel = new SimpleBrowserTest();

      JFrame frame = new SimpleBrowser();
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

答案 1 :(得分:2)

SimpleBrowser类中添加两个方法,如

String searchText = new String();

public void setSearchText(String text)
{
    searchText = text;
}

public String getSearchText()
{
    return searchText;
}

在创建actionPerformed(...)类的对象之前,从SimpleBrowserMyTabbedPane中调用此方法,如setSearchText(myField.getText())。在MyTabbedPane的构造函数中,第一行是searchWord = getSearchText();