单击按钮时用另一个图像更改图像,选择单选按钮时更改jtextarea

时间:2015-05-06 18:59:25

标签: java colors jtextarea imageicon jradiobutton

所以,我仍然是java的初学者,我的项目需要几天时间 我仍然坚持这两个问题 我经常搜索并读到它应该有效 但它不会 我按下一个按钮时,框架中有一个图像和几个按钮,图像会改变。

public class animals extends JFrame实现了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);
    }



}

}

另一件事是radiobutton 我想按下它时,文本区域颜色会改变 再次阅读它应该工作,但它没有

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

}

我知道我的代码不是最好的,但我仍然在学习 请帮助这个可怜的苦苦挣扎的学生

1 个答案:

答案 0 :(得分:0)

animals add =new animals();删除此行
colors add = new colors();并删除此行。

因为您已经说过您的类扩展JFrame,所以您不需要创建任何类的实例。而是将add替换为关键字this而不是一切都应该有效。

也是因为您的类实现了ActionListener接口,您可以说wolfb.addActionListener(this)orange.addItemListener(this);这将告诉编译器查找itemStateChangedactionPerformed方法在那堂课。

当您进行这些更改时,您的代码应该按预期工作:)