Reputation: 67
import java.awt.*;
import javax.swing.*;
public class Game
{
MyDrawPanel draw;
public static void main(String[] args)
{
new Game().go();
}
public void go()
{
JFrame frame=new JFrame("Revenge");
JPanel panel=new JPanel();
draw=new MyDrawPanel();
frame.getContentPane().add(BorderLayout.CENTER,draw);
frame.setSize(750,360);
frame.setVisible(true);
}
public class MyDrawPanel extends JPanel
{
@Override
public void paintComponent(Graphics g)
{
Image image=new ImageIcon("C:\\Users\\zep\\Desktop\\title.png").getImage();
g.drawImage(image,0,0,this);
}
}
}
So basically I need help trying to add a button. I am extremely new to java and I need to do this for my CPT in my class. Please help you will be my hero :3
Upvotes: 3
Views: 3912
Reputation: 11
Use this code:
JButton button = new JButton("Text goes here");
drawPanel.add(button, BorderLayout.NORTH);
Upvotes: 0
Reputation: 14816
Well...just create new button and add it on your drawPanel. Like this:
JButton button = new JButton("Text goes here");
drawPanel.add(button);
Default layout for drawPanel is FlowLayout
so your button should be centered on top of that panel.
Or, if you want your button above your picture, add it on panel
:
panel.add(button);
frame.getContentPane().add(panel,BorderLayout.NORTH);
Upvotes: 4