Note that there are some explanatory texts on larger screens.

plurals
  1. POJButton won't show?
    primarykey
    data
    text
    <p>So I'm looking at a tutorial here: <a href="http://zetcode.com/tutorials/javagamestutorial/basics/" rel="nofollow">http://zetcode.com/tutorials/javagamestutorial/basics/</a></p> <p>At the very end, they show how to set an image as a background (which is simple). They use 2 codes: Board.java and Image.java (which I've copied below for your convenience).</p> <p>For some reason, I can't seem to add a JButton to my GUI?</p> <p>I tried adding the following code to <strong>Image.java</strong> after the line <code>Image Main = new Image();</code> , but I can't figure out why it doesn't work. Normally I can just add a JButton to a JPanel using the <code>add</code> command and set the panel to visible with <code>setVisible(true)</code>.</p> <pre><code> JButton start; start = new JButton("Click Me"); Main.getContentPane().add(start); </code></pre> <p>I also tried adding a similar code to Board.java instead just to see if it'd work - and it of course didn't. How come my JButton won't show up? I know I'm doing something wrong, but I can't figure it out. Can someone help?</p> <hr> <p>(1) Board.java</p> <pre><code>package bardejov; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JButton; import javax.swing.JFrame; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JPanel; public class Board extends JPanel { Image bardejov; public Board() { ImageIcon ii = new ImageIcon(this.getClass().getResource("bardejov.jpg")); bardejov = ii.getImage(); } public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.drawImage(bardejov, 10, 10, null); } } </code></pre> <p>(2) Image.java</p> <pre><code>package bardejov; import javax.swing.JFrame; import javax.swing.JButton; public class Image extends JFrame { public Image() { add(new Board()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(280, 240); setLocationRelativeTo(null); setTitle("Bardejov"); setVisible(true); } public static void main(String[] args) { Image Main = new Image(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload