Button buttonSp = new Button("");
Button buttonMp = new Button("");
buttonSp.setSize(397, 61);
buttonMp.setSize(397, 61);
buttonSp.setLocation(231, 162);
buttonMp.setLocation(231, 262);
buttonSp.setVisible(false);
buttonMp.setVisible(false);
buttonSp.addActionListener(this);
buttonMp.addActionListener(this);
add(buttonSp);
add(buttonMp);
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource() == buttonSp)
{
statics.mode = "Sp";
}
if(event.getSource() == buttonMp)
{
statics.mode = "Mp";
}
repaint();
}
ImageIcon bild = new ImageIcon(getClass().getResource("/bilder/knopf.jpg"));//hier natürlich den Pfad zu deinem Bild angeben
JButton btn = new JButton(bild);
btn.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//mach hier was du willst :P
}
});
Code:Button buttonSp = new Button(""); Button buttonMp = new Button("");
class MyClass extends Panel {
Button buttonSp;
Button buttonMp;
public MyClass() {
buttonSp = new Button("");
buttonMp = new Button("");
buttonSp.setSize(397, 61);
buttonMp.setSize(397, 61);
buttonSp.setLocation(231, 162);
buttonMp.setLocation(231, 262);
buttonSp.setVisible(false);
buttonMp.setVisible(false);
buttonSp.addActionListener(this);
buttonMp.addActionListener(this);
add(buttonSp);
add(buttonMp);
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource() == buttonSp)
{
statics.mode = "Sp";
}
if(event.getSource() == buttonMp)
{
statics.mode = "Mp";
}
repaint();
}
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource() == buttonSp)
{
statics.mode = "Sp";
}
if(event.getSource() == buttonMp)
{
statics.mode = "Mp";
}
repaint();
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getActionCommand().equals("anyCommand1"))
{
statics.mode = "Sp";
}
if(event.getActionCommand().equals("anyCommand2"))
{
statics.mode = "Mp";
}
repaint();
}