Java 单击按钮时用另一个按钮更改图像,选择radiobutton时更改jtextarea
所以,我还是java的初学者,我的项目几天就要到期了 我仍然困在这两个问题中 我搜索了很多,读到它应该有用 但不会 我有一个图像的框架和几个按钮,我想要当一个按钮被按下时,图像会改变 公共类动物扩展JFrame实现ActionListener{Java 单击按钮时用另一个按钮更改图像,选择radiobutton时更改jtextarea,java,colors,jtextarea,imageicon,jradiobutton,Java,Colors,Jtextarea,Imageicon,Jradiobutton,所以,我还是java的初学者,我的项目几天就要到期了 我仍然困在这两个问题中 我搜索了很多,读到它应该有用 但不会 我有一个图像的框架和几个按钮,我想要当一个按钮被按下时,图像会改变 公共类动物扩展JFrame实现ActionListener{ static JPanel buttons =new JPanel(); static JLabel intro =new JLabel("Guess what the animal that makes the noise and press to
static JPanel buttons =new JPanel();
static JLabel intro =new JLabel("Guess what the animal that makes the noise and press to find out!");
Icon birdy=new ImageIcon(getClass().getResource("bird.png"));
JLabel bird=new JLabel(birdy,SwingConstants.CENTER);
Icon eleph=new ImageIcon(getClass().getResource("elephant.jpg"));
JLabel elephant=new JLabel(eleph,SwingConstants.CENTER);
Icon tig=new ImageIcon(getClass().getResource("tiger.png"));
JLabel tiger=new JLabel(tig,SwingConstants.CENTER);
Icon wol=new ImageIcon(getClass().getResource("wolf.png"));
JLabel wolf=new JLabel(wol,SwingConstants.CENTER);
Icon caat=new ImageIcon(getClass().getResource("caat.jpg"));
JLabel cat=new JLabel(caat,SwingConstants.CENTER);
Icon coww=new ImageIcon(getClass().getResource("cow.png"));
JLabel cow=new JLabel(coww,SwingConstants.CENTER);
Icon dogy=new ImageIcon(getClass().getResource("dog.png"));
JLabel dog=new JLabel(dogy,SwingConstants.CENTER);
Icon ducky=new ImageIcon(getClass().getResource("duck.jpg"));
JLabel duck=new JLabel(ducky,SwingConstants.CENTER);
Icon horsey=new ImageIcon(getClass().getResource("horse.png"));
JLabel horse=new JLabel(horsey,SwingConstants.CENTER);
Icon moonkey=new ImageIcon(getClass().getResource("monkey.png"));
JLabel monkey=new JLabel(moonkey,SwingConstants.CENTER);
Icon poke=new ImageIcon(getClass().getResource("pokemons.jpg"));
JLabel pokemon=new JLabel(poke,SwingConstants.CENTER);
Icon pika=new ImageIcon(getClass().getResource("pikachu.png"));
JLabel pikachu=new JLabel(pika,SwingConstants.CENTER);
Icon chari=new ImageIcon(getClass().getResource("charizard.png"));
JLabel charizard=new JLabel(chari,SwingConstants.CENTER);
Icon pic=new ImageIcon();
JLabel picture=new JLabel(pic,SwingConstants.CENTER);
static JButton catb=new JButton("Meow~~");
static JButton dogb=new JButton("Woof Woof~~");
static JButton horseb=new JButton("Hihi-n~~");
static JButton duckb=new JButton("Quack~~");
static JButton monkeyb=new JButton("Ki-Ki~~");
static JButton elephantb=new JButton("Pao--n~~");
static JButton cowb=new JButton("Moo~~");
static JButton birdb=new JButton("pichu pichu~~");
static JButton wolfb=new JButton("Awwooo~~");
static JButton tigerb=new JButton("Rawr~~");
public void animals()
{
animals add =new animals();
add.setLayout(new FlowLayout());
add.setVisible(true);
add.setTitle("Letters");
add.setSize(500,550);
add.getContentPane().setBackground(Color.WHITE);
add.add(intro);
add.add(pokemon);
buttons.setBackground(Color.WHITE);
buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
buttons.setLayout(new GridLayout(5, 5));
add.add(catb);
buttons.add(horseb);
buttons.add(duckb);
buttons.add(elephantb);
buttons.add(dogb);
buttons.add(birdb);
buttons.add(tigerb);
buttons.add(monkeyb);
buttons.add(wolfb);
buttons.add(cowb);
buttons.add(pikachu);
buttons.add(charizard);
add.add(picture);
add.add(buttons);
catb.addActionListener(add);
horseb.addActionListener(add);
elephantb.addActionListener(add);
duckb.addActionListener(add);
dogb.addActionListener(add);
birdb.addActionListener(add);
tigerb.addActionListener(add);
monkeyb.addActionListener(add);
wolfb.addActionListener(add);
cowb.addActionListener(add);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==catb)
{
picture.setIcon(caat);
}
if(ae.getSource()==horseb)
{
pokemon.setIcon(horsey);
}
}
}
另一件事是无线电按钮
我想当它被按下时,文本区域的颜色会改变
再读一遍,它应该是有效的,但事实并非如此
public class colors extends JFrame implements ItemListener {
JPanel pc=new JPanel();
JRadioButton red=new JRadioButton("Red");
JRadioButton blue=new JRadioButton("Blue");
JRadioButton black=new JRadioButton("Black");
JRadioButton pink=new JRadioButton("Pink");
JRadioButton yellow=new JRadioButton("Yellow");
JRadioButton green=new JRadioButton("Green");
JRadioButton orange=new JRadioButton("Orange");
Icon meow=new ImageIcon(getClass().getResource("meowtwo.png"));
JLabel meowtwo=new JLabel(meow,SwingConstants.CENTER);
JLabel hi=new JLabel("explore the colors !!!");
JTextArea color=new JTextArea(20,40);
static ButtonGroup col=new ButtonGroup();
public void colors()
{
colors add =new colors();
add.setLayout(new FlowLayout());
add.setVisible(true);
add.setTitle("Letters");
add.setSize(500,550);
add.getContentPane().setBackground(Color.DARK_GRAY);
pc.setBackground(Color.GRAY);
hi.setFont(new Font("Serif",Font.BOLD,40));
color.setEditable(false);
col.add(red);
col.add(blue);
col.add(black);
col.add(pink);
col.add(yellow);
col.add(green);
col.add(orange);
color.setBackground(color.getBackground());
pc.add(hi);
pc.add(meowtwo);
add.add(pc);
add.add(red);
add.add(blue);
add.add(black);
add.add(pink);
add.add(yellow);
add.add(green);
add.add(orange);
add.add(color);
red.addItemListener(add);
blue.addItemListener(add);
black.addItemListener(add);
pink.addItemListener(add);
yellow.addItemListener(add);
green.addItemListener(add);
orange.addItemListener(add);
}
@Override
public void itemStateChanged(ItemEvent ie) {
colors add=new colors();
if(red.isSelected())
{
add.getContentPane().setBackground(Color.yellow);
}
else
color.setBackground(Color.yellow);
}
}
我知道我的代码不是最好的,但我仍然在学习
请帮助这个可怜的挣扎的学生动物添加=新动物;删除此行
添加颜色=新颜色;然后拆下这条线
因为您说过您的类扩展了JFrame,所以您不需要创建任何类的实例。相反,用关键字this替换add,一切都应该正常
另外,因为类实现了ActionListener接口,所以可以说wolfb.addActionListenerthis或orange.addItemListenerthis;这将告诉编译器在该类中查找itemStateChanged或actionPerformed方法
当您进行了这些更改后,您的代码应该可以按预期工作:哦,天哪!!真不敢相信我竟然忘了这个!你真的救了我,非常感谢你: