2017-12-08 8 views
0
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class combodemo implements ActionListener { 
    JFrame f; 
    JPanel p; 
    JTextField tf; 
    JButton b1; 
    JButton b2; 
    JRadioButton rb1; 
    JRadioButton rb2; 
    JLabel l; 
    JComboBox cb; 
    JCheckBox c1, c2, c3; 
    ButtonGroup bg; 

    combodemo() { 
     String[] h = { 
      "red", 
      "yellow", 
      "green" 
     }; 
     cb = new JComboBox(h); 
     f = new JFrame(); 
     p = new JPanel(); 
     tf = new JTextField(30); 
     b1 = new JButton("OK"); 
     b2 = new JButton("Clear"); 
     rb1 = new JRadioButton("male"); 
     rb2 = new JRadioButton("Female"); 
     l = new JLabel("Enter Text"); 
     c1 = new JCheckBox("java"); 
     c2 = new JCheckBox("C++"); 
     c3 = new JCheckBox("Microsoft"); 
     bg = new ButtonGroup(); 

     f.add(p); 
     p.add(l); 
     p.add(tf); 
     b1.addActionListener(this); 
     b2.addActionListener(this); 
     p.add(b1); 
     p.add(b2); 
     bg.add(rb1); 
     bg.add(rb2); 
     p.add(rb1); 
     p.add(rb2); 
     p.add(cb); 
     p.add(c1); 
     p.add(c2); 
     p.add(c3); 

     f.setVisible(true); 
     f.pack(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
    public void actionPerformed(ActionEvent ae) 

    { 
     String str = (String) cb.getSelectedItem(); 
     ' 
     if (str.equals("red")) 
      p.setBackground(Color.red); 

     if (str.equals("green")) 
      p.setBackground(Color.green); 

     if (str.equals("yellow")) 
      p.setBackground(Color.yellow); 

     if (ae.getActionCommand() == "OK") { 
      tf.setText("This is example of swing"); 
     } 
     if (ae.getActionCommand() == "Clear") { 
      tf.setText(""); 
     } 
    } 
    public static void main(String[] args) 

    { 
     combodemo cd = new combodemo(); 
    } 
} 

実際にこのコードを実行し、「赤」、「緑」または「黄色」をクリックしたときにパネルの色を変更すると、私はパネルの色を変更するために "OK"をクリックしてください。 "RED"、 "GREEN"または "Yellow"の名前をクリックしながら色を変更したいのですが、この問題を取り除くのを助けてください。コンボボックスの色をJavaで変更したいのですが

私は今日の講義でNIITからこのコードを学び、コード化すると多くのエラーが発生しました。しかし、今ではコードを完成させましたが、私は既に上で書いた問題に直面しています。

お願いします。

答えて

0

あなたは

最初のイベントを処理するためのロジックを実装し、

cb.addActionListener(this); 

秒をJComboBoxのためにactionlistnerを追加... 2つのことを行う必要があります。

public void actionPerformed(ActionEvent ae) 

{ 
    if(ae.getSource() instanceof JComboBox) { 
      String str = (String)cb.getSelectedItem(); 
      if(str.equals("red")) 
      p.setBackground(Color.red); 

      if(str.equals("green")) 
      p.setBackground(Color.green); 

      if(str.equals("yellow")) 
      p.setBackground(Color.yellow); 
     } 
      ///// 

完全なコード

package test; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class combodemo implements ActionListener 
{ 
JFrame f; 
JPanel p; 
JTextField tf; 
JButton b1; 
JButton b2; 
JRadioButton rb1; 
JRadioButton rb2; 
JLabel l; 
JComboBox cb; 
JCheckBox c1,c2,c3; 
ButtonGroup bg; 

combodemo() 
{ 
String []h = {"red","yellow","green"}; 
cb = new JComboBox(h); 
f = new JFrame(); 
p = new JPanel(); 
tf = new JTextField(30); 
b1 = new JButton("OK"); 
b2 = new JButton("Clear"); 
rb1 = new JRadioButton("male"); 
rb2 = new JRadioButton("Female"); 
l = new JLabel("Enter Text"); 
c1 = new JCheckBox("java"); 
c2 = new JCheckBox("C++"); 
c3 = new JCheckBox("Microsoft"); 
bg = new ButtonGroup(); 

f.add(p); 
p.add(l); 
p.add(tf); 
b1.addActionListener(this); 
b2.addActionListener(this); 
cb.addActionListener(this); 
p.add(b1); 
p.add(b2); 
bg.add(rb1); 
bg.add(rb2); 
p.add(rb1); 
p.add(rb2); 
p.add(cb); 
p.add(c1); 
p.add(c2); 
p.add(c3); 

f.setVisible(true); 
f.pack(); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 
public void actionPerformed(ActionEvent ae) 

{ 
    if(ae.getSource() instanceof JComboBox) { 
     String str = (String)cb.getSelectedItem(); 
     if(str.equals("red")) 
     p.setBackground(Color.red); 

     if(str.equals("green")) 
     p.setBackground(Color.green); 

     if(str.equals("yellow")) 
     p.setBackground(Color.yellow); 
    } 


if(ae.getActionCommand()=="OK") 
{ 
tf.setText("This is example of swing"); 
} 
if(ae.getActionCommand()=="Clear") 
{ 
tf.setText(""); 
} 
} 
public static void main (String [] args) 

{ 
combodemo cd = new combodemo(); 
} 
} 
+1

おかげで私はあなたのプログラムの実行を発見しました。 –