Java Applet - 如何在单击按钮时显示图像?

时间:2015-01-15 13:18:43

标签: java applet

嗨,这是Stack Overflow的新人。好吧,我正在尝试制作一个Applet,它将显示所选按钮的图像包,并且是java编程的新手。有人可以帮我如何使用代码来显示图像?这是我的代码:

public void init()
{
    // Buttons/Labels/Etc
    setBackground(Color.white);
    this.setLayout(null);
    packageB = new Button("Budget");
    add(packageB);
    packageB.setBounds(200, 20, 80, 20);
    packageP = new Button("Premium");
    add(packageP);
    packageP.setBounds(300, 20, 80, 20);
    sButton = new Button("Silver");
    add(sButton);
    sButton.setBounds(100, 20, 80, 20);
    sButton.setVisible(false);
    gButton = new Button("Gold");
    add(gButton);
    gButton.setBounds(200, 20, 80, 20);
    gButton.setVisible(false);
    pButton = new Button("Platinum");
    add(pButton);
    pButton.setBounds(300, 20, 80, 20);
    pButton.setVisible(false);
    dButton = new Button("Diamon");
    add(dButton);
    dButton.setBounds(400, 20, 80, 20);
    dButton.setVisible(false);
    backButton = new Button("<-- Back");
    add(backButton);
    backButton.setBounds(500, 20, 80, 20);
    backButton.setVisible(false);
    notice = new Label("CHOOSE YOUR PACKAGE!");
    add(notice);
    notice.setBounds(215, 0, 200, 20);
    notice2 = new Label("WHAT PACKAGE DO YOU WANT?");
    add(notice2);
    notice2.setBounds(205, 0, 200, 20);
    notice2.setVisible(false);
    sLabel = new Label("Test Label");
    add(sLabel);
    sLabel.setBounds(205, 0, 200, 20);
    sLabel.setVisible(false);

    //adds an ActionListener to all of the buttons to be able to receive commands
    packageB.addActionListener(this);
    packageP.addActionListener(this);
    backButton.addActionListener(this);
    sButton.addActionListener(this);
    gButton.addActionListener(this);
    pButton.addActionListener(this);
    dButton.addActionListener(this);

}

public void paint(Graphics g)
{
    this.resize(600,400);
    g.setColor(Color.black);
    g.fillRect(60,50,460,300); 
}

// receiver of the ActionListener method
public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == packageB)
    {
        packageB.setVisible(false);
        packageP.setVisible(false);
        notice.setVisible(false);
        notice2.setVisible(true);
        backButton.setVisible(true);
    }
    else if (e.getSource() == packageP)
    {
        packageB.setVisible(false);
        packageP.setVisible(false);
        backButton.setVisible(true);
        notice.setVisible(false);
        notice2.setVisible(true);
        sButton.setVisible(true);
        gButton.setVisible(true);
        pButton.setVisible(true);
        dButton.setVisible(true);
    }
    else if (e.getSource() == backButton)
    {
        packageB.setVisible(true);
        packageP.setVisible(true);
        notice2.setVisible(false);
        notice.setVisible(true);
        backButton.setVisible(false);
        sButton.setVisible(false);
        gButton.setVisible(false);
        pButton.setVisible(false);
        dButton.setVisible(false);
    }
    else if (e.getSource() == sButton)
    {
        sButton.setBackground(Color.red);
        gButton.setBackground(Color.lightGray);
        pButton.setBackground(Color.lightGray);
        dButton.setBackground(Color.lightGray);
        sLabel.setVisible(true);
    }
    else if (e.getSource() == gButton)
    {
        sButton.setBackground(Color.lightGray);
        gButton.setBackground(Color.red);
        pButton.setBackground(Color.lightGray);
        dButton.setBackground(Color.lightGray);
    }
    else if (e.getSource() == pButton)
    {
        sButton.setBackground(Color.lightGray);
        gButton.setBackground(Color.lightGray);
        pButton.setBackground(Color.red);
        dButton.setBackground(Color.lightGray);
    }
    else if (e.getSource() == dButton)
    {
        sButton.setBackground(Color.lightGray);
        gButton.setBackground(Color.lightGray);
        pButton.setBackground(Color.lightGray);
        dButton.setBackground(Color.red);
    }
    // Add New ActionListener Here!
    /*else if (e.getSource() == TestButton)
    {
    }*/
}

}

0 个答案:

没有答案