单击按钮时如何显示图像?

niwlg2el  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(200)

嗨,新来的是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)
    {
    }*/
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题