2011-10-20 11 views
3

私は学習目的のための簡単なキーパッドと表示プログラムを作成しようとしています。しかし、私はそれが働いている私は別のクラスのオブジェクトと対話するためのより良い方法があるのだろうかと思っています。Java異なるクラスのオブジェクトとのやりとり

KeypadPanelという名前のクラスが作成されました。これは、グリッドレイアウトのパネルで、すべてのボタンが追加されています。次に、ActionListenerを作成し、それを各ボタンのイベントとともにボタンにアタッチしました。私はこのパネルをボーダーレイアウトパネルの中央に追加しました。

次に、境界線レイアウトパネルのNorthセクションに追加されるラベルを保持するDisplayPanelクラスを作成しました。しかし私はまだ新しくJavaを学んでいるので、DisplayPanelのラベルとやり取りできる唯一の方法は、ラベルを静的にし、setDisplayメソッドを静的にすることです。次に、ButtonListener内から呼​​び出すことができ、表示を変更することができます。

はここにあなたがpublicないprivateとしてそれらを宣言する必要があり、他のクラス内のオブジェクトを操作するには、キーパッド、ディスプレイおよびメインクラス

public class KeypadPanel extends JPanel { 

// setup button objects 
private JButton b1 = new JButton("1"); 
private JButton b2 = new JButton("2"); 
private JButton b3 = new JButton("3"); 
private JButton b4 = new JButton("4"); 
private JButton b5 = new JButton("5"); 
private JButton b6 = new JButton("6"); 
private JButton b7 = new JButton("7"); 
private JButton b8 = new JButton("8"); 
private JButton b9 = new JButton("9"); 
private JButton b0 = new JButton("0"); 
private JButton bStar = new JButton("*"); 
private JButton bPound = new JButton("#"); 

public KeypadPanel() { 

    // setup panel 
    setLayout(new GridLayout(4, 3)); 
    setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.black)); 

    setPreferredSize(new Dimension(150, 150)); 

    // add listeners to buttons 
    b1.addActionListener(new ButtonListener()); 
    b2.addActionListener(new ButtonListener()); 
    b3.addActionListener(new ButtonListener()); 
    b4.addActionListener(new ButtonListener()); 
    b5.addActionListener(new ButtonListener()); 
    b6.addActionListener(new ButtonListener()); 
    b7.addActionListener(new ButtonListener()); 
    b8.addActionListener(new ButtonListener()); 
    b9.addActionListener(new ButtonListener()); 
    b0.addActionListener(new ButtonListener()); 
    bStar.addActionListener(new ButtonListener()); 
    bPound.addActionListener(new ButtonListener()); 

    // add buttons to panel 
    add(b1); 
    add(b2); 
    add(b3); 
    add(b4); 
    add(b5); 
    add(b6); 
    add(b7); 
    add(b8); 
    add(b9); 
    add(bPound); 
    add(b0); 
    add(bStar); 

} 

// setup listener for buttons 
private class ButtonListener implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 

     // determine which button raised event and call setDisplay 
     if (e.getSource() == b1) { 
      DisplayPanel.setDisplay("1"); 
     } else if (e.getSource() == b2) { 
      DisplayPanel.setDisplay("2"); 
     } else if (e.getSource() == b3) { 
      DisplayPanel.setDisplay("3"); 
     } else if (e.getSource() == b4) { 
      DisplayPanel.setDisplay("4"); 
     } else if (e.getSource() == b5) { 
      DisplayPanel.setDisplay("5"); 
     } else if (e.getSource() == b6) { 
      DisplayPanel.setDisplay("6"); 
     } else if (e.getSource() == b7) { 
      DisplayPanel.setDisplay("7"); 
     } else if (e.getSource() == b8) { 
      DisplayPanel.setDisplay("8"); 
     } else if (e.getSource() == b9) { 
      DisplayPanel.setDisplay("9"); 
     } else if (e.getSource() == b0) { 
      DisplayPanel.setDisplay("0"); 
     } else if (e.getSource() == bStar) { 
      DisplayPanel.setDisplay("*"); 
     } else if (e.getSource() == bPound) { 
      DisplayPanel.setDisplay("#"); 
     } else { 
      return; 
     } 
    } 

} 

------------ 

} 
public class DisplayPanel extends JPanel { 

// setup display label object 
private static JLabel display = new JLabel(""); 

public DisplayPanel() { 

    // setup panel and add label 
    setPreferredSize(new Dimension(200, 25)); 
    setBorder(BorderFactory.createLineBorder(Color.black, 3)); 
    add(display); 

} 
/** 
* @param incoming 
*   the text to add to the display 
*/ 
public static void setDisplay(String incoming) { 

    // get the incoming string and add to existing display string 

    String current, changed; 

    current = display.getText(); 
    changed = current.concat(incoming); 
    display.setText(changed); 

} 
/** 
* 
* clears the current text from the display 
*/ 
public static void clearDisplay() { 
    // clears display 
    display.setText(""); 

} 
} 
----------------- 
public class KeypadTest { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    //setup frame 
    JFrame frame = new JFrame ("Keypad Demo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //setup main panel and layout 
    JPanel panel = new JPanel(); 
    panel.setLayout(new BorderLayout(3, 3)); 

    //create panel objects to fill frame and panel 
    KeypadPanel keypad = new KeypadPanel(); 
    ClearPanel clear = new ClearPanel(); 
    DisplayPanel display = new DisplayPanel(); 

    // add to main panel 
    panel.add(display, BorderLayout.NORTH); 
    panel.add(keypad, BorderLayout.CENTER); 
    panel.add(clear, BorderLayout.EAST); 


    // setup frame 
    frame.getContentPane().add(panel); 
    frame.pack(); 
    frame.setVisible(true); 
} 

} 
+1

本当にわかりません。具体的にどの部分についてお考えですか?私は、その反復ボタン定義/処理コードをリファクタリングするかもしれません。 –

答えて

2

次いでタッチパネルのコンストラクタへの引数として渡し、表示パネルを構築します。次に、コンストラクタは表示パネルをインスタンスフィールド(属性)に格納し、リスナはこの属性を使用してsetDisplayメソッドを呼び出します。もちろん、setDisplayclearDisplayのメソッド(およびlabelフィールド)は静的であってはいけません。それについて考える:アプリケーションでDisplayPanelの2つの異なるインスタンスが必要だった場合、どのように動作しますか?

あなたのコードは他の領域で強化することができます。たとえば、ButtonListenerのインスタンスを1つだけ作成し、この一意のインスタンスを各ボタンのリスナーとして追加することができます。

setPreferredSizeはほとんど決して呼び出されません。好ましいサイズは、パネルのコンポーネントに基づいてレイアウトマネージャによって自動的に計算されます。

最後に、mainメソッド内のすべてのコードはSwingUtilities.invokeLaterを使用して、イベントディスパッチスレッドで実行する必要があります

SwingUtilities.invokeLater(new Runnable() { 
    @Override 
    public void run() { 
     // put the main code here 
    } 
}); 
+0

優れたアドバイスをいただき、ありがとうございました。私はJavaについて非常に新しく、表示パネルの別のインスタンスを作成した場合、静的メソッドを正しく作成したとは思わなかったと思います。私はDisplayPanelを渡すとref(オブジェクトなので)によって渡されるので、ローカルで利用できるようになります...素晴らしい! – Adam

0

ための私のコードです。

public JButton b1 = new JButton("1");

+3

情報を隠すためにプライベートフィールドを公開することを推奨します。外部アクセスをまったく提供しない、プライベートフィールドに影響を与える高レベルメソッドを提供する、最悪の場合にパブリックゲッターメソッドを提供する –

0

あなたは依存性注入を使用して検討する必要があります。それはもう少し高度なものですが、いったん入手すれば、建設パラメータで作業するよりもはるかに汎用性が高く、柔軟性があり便利です。

最も簡単な方法はあること、(そのためグーグル)あなたのクラスパスにJavaの春を追加し、すべてのあなたの豆を読み込むところ、その後(下記参照)を使用すると、アプリケーションの起動時にロードアプリケーションのcontext.xmlファイルを使用することです(例えば、@Component)注釈付きクラスです。

今、Springはあなたのためのオブジェクト管理を担当します。それらを作成または処分する必要はありません。最も重要なのは、好きな場所に注入することができます。

@Component  
    public class DisplayPanel extends JPanel { 


     private JLabel display = new JLabel(""); 

     public DisplayPanel() { 
      // ... 
     } 
     public void setDisplay(String value){ 
      this.display.setText(value); 
     } 
    } 

    @Component 
    public class ButtonListener implements ActionListener { 

     @Inject 
     private DisplayPanel displayPanel; 

     public void actionPerformed(ActionEvent e) { 

      if (e.getSource() == b1) { 
       displayPanel.setDisplay("1"); 
      // ... 
      } 
     } 
    } 

単純なアプリケーションコンテキスト。XML:起動時に

<beans xmlns="http://www.springframework.org/schema/beans" 

    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    default-autowire="byName"> 

    <context:annotation-config /> 
    <!-- scan your package for annotated class --> 
    <context:component-scan base-package="your.base.package.name" /> 

</beans> 

がロードそれを:あなたが求めているもの

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application-context.xml"); 
関連する問題