2017-10-29 13 views
0

ユーザーがJComboBoxから色を選択し、JButtonを押してJPanelの色をユーザーが選択する色に変更するプログラムを作成しようとしています。 アクションイベントロジックが正しく動作するように見えません。JComboBoxとJButtonのリッスンイベントに関する問題

アクションリスナーを選択して、選択した項目値を取得し、パネルの選択した背景として適用しますが、コンパイルしません。

私の試み

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


public class EventTest extends JFrame implements ActionListener 
{ 

    EventTest() 
    { 

     super("My First Frame"); 

     //Create a JPanel and content frame 
     JPanel panel = new JPanel(); 
     Container contentPane = getContentPane(); 

     //Create a list of colours for our JCombo Box 
     String colours[] = { "Red", "Yellow", "Green", "Blue", "Orange" }; 

     //Create a JCombo Box 
     JComboBox colourSelector = new JComboBox (colours); 
     colourSelector.addActionListener (this); //Add listener to the box 
     panel.add (colourSelector); //Add combobox to the panel 

     //Create a JButton 
     JButton changeColour = new JButton ("Change Colour"); //Create a new JButton. 
     changeColour.addActionListener (this); //Add listener to the button. 
     panel.add (changeColour); //Add button to the panel. 

     //Add the content to the pane 
     contentPane.add (panel); 

     //Set window parameters 
     setTitle ("Lab4"); 
     setSize (800, 600); 
     setVisible (true); 

    }//End Constructor 


    public static void main (String args[]) 
    { 
     EventTest myFrame = new EventTest(); 
    } 

    //Action Listener - An action listener that changes the colour of our JPanel to the 
    //colour selected by the user. 
    public void actionPerformed (ActionEvent e) 
    { 

     int selectedItem = colourSelector.getSelectedItem(); 
     panel.setBackground (Color.selectedItem); //Set the background colour based on our list. 

    } 


}//End Class 
+1

「Color.selectedItem "これはどうやって意味をなすことができますか? – Ansharja

+0

あなたのコンポーネントはローカル変数として定義されているので、コンストラクタの外でアクセスするスコープはありません – MadProgrammer

答えて

2

あなたの基本的な問題は、変数のスコープの一つです。コンストラクターで定義している変数には、コンストラクターのスコープ外でアクセスすることはできません。

あなたは匿名のリスナーを使用して問題を解決することができますが、教育のために、あなたはおそらく、たとえば、あなたはさまざまな方法のインスタンスフィールドにアクセスするこれらの変数を作成する必要があります。

import java.awt.Color; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class EventTest extends JFrame implements ActionListener { 

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

    private JComboBox<String> colourSelector; 
    private String colours[] = {"Red", "Yellow", "Green", "Blue", "Orange"}; 
    private JPanel panel; 

    EventTest() { 

     super("My First Frame"); 

     //Create a JPanel and content frame 
     Container contentPane = getContentPane(); 
     panel = new JPanel(); 

     //Create a JCombo Box 
     colourSelector = new JComboBox(colours); 
     colourSelector.addActionListener(this); //Add listener to the box 
     panel.add(colourSelector); //Add combobox to the panel 

     //Create a JButton 
     JButton changeColour = new JButton("Change Colour"); //Create a new JButton. 
     changeColour.addActionListener(this); //Add listener to the button. 
     panel.add(changeColour); //Add button to the panel. 

     //Add the content to the pane 
     contentPane.add(panel); 

     //Set window parameters 
     setTitle("Lab4"); 
     setSize(800, 600); 
     setVisible(true); 

    }//End Constructor 

    //Action Listener - An action listener that changes the colour of our JPanel to the 
    //colour selected by the user. 
    public void actionPerformed(ActionEvent e) { 

//  private String colours[] = {"Red", "Yellow", "Green", "Blue", "Orange"}; 

     String selectedItem = (String)colourSelector.getSelectedItem(); 
     System.out.println(selectedItem); 
     switch (selectedItem) { 
      case "Red": 
       panel.setBackground(Color.RED); 
       break; 
      case "Yellow": 
       panel.setBackground(Color.YELLOW); 
       break; 
      case "Green": 
       panel.setBackground(Color.GREEN); 
       break; 
      case "Blue": 
       panel.setBackground(Color.BLUE); 
       break; 
      case "Orange": 
       panel.setBackground(Color.ORANGE); 
       break; 
     } 
    } 

}//End Class 
+0

ありがとうございます、それは動作しますが、メニューが選択されているときに色の変更がアクティブになっていますが、プッシュ。だから私はオレンジ色を選択してから、Change Colorボタンを押して変更を加える必要があります。 – Ninja2k

+1

@ Ninja2k 'colourSelector.addActionListener(this);を削除してください。 – MadProgrammer

+0

これは完全に機能しました。私はこれについていくつかの種類の本が必要だと思います。 – Ninja2k

関連する問題