1

オブジェクト指向の方法で GUI を設計するのに問題があります。次のコードは、私の質問をより明確に表現するのに役立ちます。

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

public class QuoteOptionsPanel extends JPanel
{
    private JLabel quote;
    private JRadioButton comedy, philosophy, carpentry;
    private String comedyQuote, philosophyQuote, carpentryQuote;
    //-----------------------------------------------------------------
    // Sets up a panel with a label and a set of radio buttons
    // that control its text.
    //-----------------------------------------------------------------
    public QuoteOptionsPanel()
    {
        comedyQuote = "Take my wife, please.";
        philosophyQuote = "I think, therefore I am.";
        carpentryQuote = "Measure twice. Cut once.";

        quote = new JLabel (comedyQuote);
        quote.setFont (new Font ("Helvetica", Font.BOLD, 24));

        comedy = new JRadioButton ("Comedy", true);
        comedy.setBackground (Color.green);

        philosophy = new JRadioButton ("Philosophy");
        philosophy.setBackground (Color.green);

        carpentry = new JRadioButton ("Carpentry");
        carpentry.setBackground (Color.green);

        ButtonGroup group = new ButtonGroup();
        group.add (comedy);
        group.add (philosophy);
        group.add (carpentry);

        QuoteListener listener = new QuoteListener();
        comedy.addActionListener (listener);
        philosophy.addActionListener (listener);
        carpentry.addActionListener (listener);

        add (quote);
        add (comedy);
        add (philosophy);
        add (carpentry);

        setBackground (Color.green);
        setPreferredSize (new Dimension(300, 100));
    }
    //*****************************************************************
    // Represents the listener for all radio buttons.
    //*****************************************************************
    private class QuoteListener implements ActionListener
    {
        //--------------------------------------------------------------
        // Sets the text of the label depending on which radio
        // button was pressed.
        //--------------------------------------------------------------
        public void actionPerformed (ActionEvent event)
        {
            Object source = event.getSource();
            if (source == comedy)
                quote.setText (comedyQuote);
            else
                if (source == philosophy)
                    quote.setText (philosophyQuote);
                else
                    quote.setText (carpentryQuote);
        }
    }
}

上記のコードは、それぞれが引用符に対応する 3 つのラジオ ボタンを持つパネルを作成するだけです。また、引用を表示するラベルも作成します。ボタンが選択されるたびに、ラベル内のテキストが対応する引用に設定されます。私はこのコードをよく理解しています。変更しようとすると問題が発生します。同じプログラムを作成したいとしましょう。ただし、ラジオ ボタンは互いに垂直に積み上げられています。また、独自の BoxPanel クラスで定義する BoxLayout を使用してパネルにラジオ ボタンを追加することで、これを行うことにしたとしましょう。(その後、BoxPanel を QuoteOptionsPanel に追加しますが、これにはまだ見積もり JLabel が含まれています。)したがって、BoxPanel コードは次のようになります。

import java.awt.*;
import javax.swing.*;

public class BoxPanel extends JPanel
{
    private JRadioButton comedy, philosophy, carpentry;
    public BoxPanel()
    {
        setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
        setBackground (Color.green);

        comedy = new JRadioButton ("Comedy", true);
    comedy.setBackground (Color.green);
    philosophy = new JRadioButton ("Philosophy");
    philosophy.setBackground (Color.green);
    carpentry = new JRadioButton ("Carpentry");
    carpentry.setBackground (Color.green);

    ButtonGroup group = new ButtonGroup();
    group.add (comedy);
    group.add (philosophy);
    group.add (carpentry);

    QuoteListener listener = new QuoteListener();
    comedy.addActionListener (listener);
    philosophy.addActionListener (listener);
    carpentry.addActionListener (listener);

    }
    //*****************************************************************
    // Represents the listener for all radio buttons.
    //*****************************************************************
    private class QuoteListener implements ActionListener
    {
        //--------------------------------------------------------------
        // Sets the text of the label depending on which radio
        // button was pressed.
        //--------------------------------------------------------------
        public void actionPerformed (ActionEvent event)
        {
            Object source = event.getSource();

            I do not know what to do here.
        }
    }
}

ご覧のとおり、QuoteListener クラスを定義する方法がわかりませんでした。投稿した元のプログラムと同じ機能を実行したいのですが、そうする方法がわかりません。引用を表示するラベルは QuoteOptionsPanel にあるため、アクセスできません。本質的に、あるパネルのラベルを、別のパネルのコンポーネントに属するイベントリスナーで変更する最適な方法を求めています。お役に立てれば幸いです。質問を十分に明確に表現していない場合はお知らせください。

4

2 に答える 2

4

これを解決する方法はいくつかありますが、ほとんどの場合、重要なのは参照を取得して使用することです。JLabel をプライベート フィールドとして保持するクラスにパブリック メソッドがあるとします。

public void setQuoteLabelText(String text) {
  quoteLabel.setText(text);
}

setXXX(...)次に、このクラスの視覚化されたオブジェクトへの参照を、コンストラクター パラメーターまたはセッター メソッドを介して BoxPanel クラスに渡す必要があります。その後、ActionListener はこのクラスのオブジェクトのメソッドを呼び出すことができます。

于 2012-07-17T02:09:46.013 に答える
1

1.アクセスする必要があるプライベート インスタンス変数を持つクラスのインスタンスを作成できます。

2.カプセル化の多くの使用方法の 1 つに従います。つまり、プライベート インスタンス変数と、そのインスタンス変数のパブリック ゲッター セッターを使用します。

3.これで、クラスのインスタンスでパブリック メソッドを呼び出して、プライベート メンバーにアクセスできます。

4.もう 1 つ、2005 年に NetBeans チームによって作成されたGroup Layoutを使用してみてください。現在は google から無料の Window Builder Pro を使用してください。

于 2012-07-17T03:11:11.780 に答える