[英]I want to change combobox color while clicking on their names in java
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();
}
}
Actually when I run this code and to change the color of panel when I click on "RED", "GREEN" or "YELLOW", it requires me to click on "OK" in order to change the color of panel. 实际上,当我运行此代码并在单击“红色”,“绿色”或“黄色”时更改面板的颜色时,需要我单击“确定”才能更改面板的颜色。 I want to change the color while clicking on the name on "RED", "GREEN" or "Yellow" please help me to get rid of this problem.
我想在单击“红色”,“绿色”或“黄色”上的名称时更改颜色,请帮助我摆脱这个问题。
I learn this code from NIIT in today's lecture and when I coded it then many errors has occurred. 我在今天的讲座中从NIIT学习了此代码,当我对其进行编码时,发生了很多错误。 But now I have completed the code but the problem I'm facing now I already written above.
但是现在我已经完成了代码,但是上面已经写过我现在面临的问题。
Help will be appreciated. 帮助将不胜感激。
You need to do two things.. 您需要做两件事。
first add actionlistner to jcombobox 首先将actionlistner添加到jcombobox
cb.addActionListener(this);
second, implement logic for handling the event. 第二,实现处理事件的逻辑。
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);
}
/////
Complete code 完整的代码
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.