我正在编写一个简单的浏览器,其中包含两个类:SimpleBrowser和MyTabbedPane。
 SimpleBrowser具有一些Gui组件,例如JButton和TextField,而MyTabbedPane具有带有JEditorPane的tabbedpane。
我想将在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);
            }
        });
    }
}

最佳答案

您是正确的-使用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();
         }
      });
   }
}

10-08 01:40