0

ActionListner、FocusListner、および ItemListner を実装するハンドラ クラスがあります。ConfrenceGUI クラスから gui オブジェクトをインスタンス化しました。

      public ConferenceGUI()
   {
      //Create a new JPanel and set its latyout mgr   
      conference = new JPanel(); 
      setLayout(new BorderLayout());  
      //create a RegPanel panel           
      regPanel = new RegPanel();
      //create new WorkshopPanel workshopPanel
      workshopPanel = new WorkshopPanel();      
      //build a buttonpanel
      buildButtonPanel();
      //Create a title panel      
      titlePanel = new JPanel(new FlowLayout());
      //create and set a font object
      titlePanel.setFont(new Font ("sansserif", Font.BOLD, 18));
      //add a label
      titlePanel.add(new Label("Select Registration Options"));      
      //Add the instantiated subpanels to the main conference gui panel
      add(titlePanel,        BorderLayout.NORTH);
      add(regPanel,          BorderLayout.WEST);
      add(workshopPanel,     BorderLayout.EAST);
      add(buttonPanel,       BorderLayout.SOUTH);
      //add an item listener to the combo box
      ConferenceHandler handler = new ConferenceHandler(this);
      regPanel.regComboBox.addItemListener(handler);
      //add a focus listener to the name field
      ConferenceHandler fhandler = new ConferenceHandler(this);
      regPanel.regTextField.addFocusListener(fhandler);
   }

リスナーから情報を取得しようとしています (含まれていない ConferenceGUI クラスの別のメソッドからの 2 つのボタン リスナーを含む)。

これは私のハンドラーから抜粋したコードです:

  public void itemStateChanged(ItemEvent e)
  {
     String name = gui.regPanel.regTextField.getText(); 
     if (e.getSource() == gui.regPanel.regComboBox)
       {
          if (gui.regPanel.getRegType() == "Please select a type")
          JOptionPane.showMessageDialog(null, "Please select a registraion type",
                                        "Type Error", JOptionPane.ERROR_MESSAGE);     
          else gui.textArea.setText(name+" is a " +
                                        gui.regPanel.getRegType()+ " registration");
       }

ボタンの場合:

      public void actionPerformed (ActionEvent e)
  {
    String name = gui.regPanel.regTextField.getText();
    DecimalFormat $ = new DecimalFormat("$#,##0.00");
    if (e.getSource() == gui.calcButton)//if the calculate buttton is pressed
    {
       //dislplay error box if user selects index 0
       if (gui.regPanel.getRegType() == "Please select a type")
         JOptionPane.showMessageDialog(null, "Please select a registraion type",
                                                "Type Error",JOptionPane.ERROR_MESSAGE);
       //prints to textarea if registrant will be attending keynote or not
       if (gui.regPanel.regCheckBox.isSelected())
          gui.textArea.setText("Keynote address will be attended");
            else
              gui.textArea.setText("Keynote address will not be attended");
       //prints to textarea which workshops registrant will be attending
       gui.textArea.setText(name+" is registered in these workshops:" +
                            gui.workshopPanel.getWorkshopList());
       //prints total registration fees to textarea
       gui.textArea.setText("Total charges for" + name + " are " + $.format(calcTotalCharges()));
    }
    else if (e.getSource() == gui.clearButton)//if the clear button is pressed
    {
       //clear the textarea 
       gui.textArea.setText("");  
       //reset the list
       gui.workshopPanel.workshopList.setSelectedIndex(0);
       //reset the combobox to index 0
       gui.regPanel.regComboBox.setSelectedIndex(0);
    }
  }

問題は皆さんには明らかですが、私は始めたばかりなので、GUI の textArea にテキストを書き込めない理由がわかりません。コードの量については申し訳ありませんが、徹底したかったのです。

テキストエリアの由来は次のとおりです (これは、私の ConferenceGUI クラスに含まれる別のメソッドです。

   private void buildButtonPanel()
   {
      //create the buttonpanel
      buttonPanel = new JPanel(new FlowLayout());
      //create the buttons
      calcButton = new JButton("Calculate Charges");
      clearButton = new JButton    ("Clear");
      //add listeners to the buttons
      ConferenceHandler ahandler = new ConferenceHandler(this);
      calcButton.addActionListener(ahandler);  
      clearButton.addActionListener(ahandler);
      //create a text area
      JTextArea textArea = new JTextArea(5,30); 
      textArea.setLineWrap(true); textArea.setWrapStyleWord(true);
      //add everything to the buttonpanel
      buttonPanel.add(calcButton); buttonPanel.add(clearButton); buttonPanel.add(new JScrollPane(textArea));
   }

他に RegPanel と WorkshopPanel の 3 つのクラスがあり、どちらも ConferenceGUI 用のいくつかのパネルを作成し、ConferenceGUI はアプレット (gui) によってインスタンス化されます。

4

2 に答える 2

2

あなたの質問を理解できるかどうかはわかりませんが(何が機能しないのですか?何を期待して何が起こるのですか?)、setText()JTextAreaで複数回呼び出すのは良い考えではありません:setTextテキストのコンテンツ全体を置き換えます範囲。append()テキスト領域に複数行のテキストを追加するために使用する必要があります。

編集済み:

テキスト領域の作成方法を示したので、問題はより明確になります。メソッドでローカル変数textAreaをインスタンス化しますbuildButtonPanelが、GUIのインスタンス変数は別のテキスト領域(またはnull)を指します。

再度編集:

コードが複雑すぎて修正するには十分ではありませんが、状況は次のようになります。

public class Bug extends JPanel {
    private JTextArea textArea = new JTextArea(); // first text area

    private void build()  {
        JTextArea textArea = new JTextArea(); // second text area. Inaccessible outside of this method
        this.add(new JScrollPane(textArea));
    }

    public void actionPerformed(ActionEvent e) {
        this.textArea.setText("foo"); // here, we modify the first text area, but it hasn't been added to the GUI, so it's invisible
    }
}

これを修正するには、次のように変更する必要があります。

public class NoBug extends JPanel {
    private JTextArea textArea = new JTextArea(); // unique text area

    private void build()  {
        this.add(new JScrollPane(this.textArea));
    }

    public void actionPerformed(ActionEvent e) {
        this.textArea.setText("foo"); // here, we modify the unique text area, which has been added to the GUI in the build() method        }
}
于 2011-02-13T16:49:33.043 に答える
2

コードの量についてはお詫びしますが、徹底したいと思いました

実際、あなたが投稿したコードはあまり役に立ちません。なぜなら、コードがどのように使用されるかのコンテキストがわからないからです。

コードは「gui」オブジェクトを参照していますが、この変数がいつどのように作成されるかはわかりません。「textArea」オブジェクトと同じコメントも参照します。したがって、投稿したコードは不完全です。

テキスト領域にテキストを追加できない場合は、次の 2 つのいずれかが発生しています。

a) コードが実行されていない (これを確認するために System.out.println(...) を追加できます)。b) GUI に追加された textArea コンポーネントへの参照が間違っています。これには多くの理由が考えられます。同じ名前のクラス変数とローカル変数の両方を持っている可能性があります。

さらに支援が必要な場合は、問題を示すSSCCEを投稿してください。

于 2011-02-13T16:45:04.603 に答える