简体   繁体   中英

changing an image with another when button is clicked and changing jtextarea when radiobutton is selected

so, i'm still a beginners in java and my project is due few days and i'm still stuck in those 2 problems i'v searched a lot and read that it should work but it won't I have an image in the frame and few buttons i want when a button is pressed the image would change.

public class animals extends JFrame implements ActionListener {

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);
    }



}

}

the other thing is the radiobutton i want when it's pressed, the text area color will change again read it should work, but it doesn't

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);
}

}

i know my code is not the best but again i'm still learning please help this poor struggling student

animals add =new animals(); remove this line
colors add = new colors(); and remove this line.

because you have said your classes extend JFrame you don't need to create any instances of your classes. instead replace add with the keyword this instead and everything should work.

also because your classes implement the ActionListener interface you can say wolfb.addActionListener(this) or orange.addItemListener(this); this will tell the compiler to look for either itemStateChanged or actionPerformed methods in that class.

when you have made those changes your code should work as expected :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM