Note that there are some explanatory texts on larger screens.

plurals
  1. POJava JFrame gui - Why won't the buttons show up?
    text
    copied!<p>I've just started learning Swing/JFrame, basically making a GUI. I've been doing Java for a month now, just using the console, making a sin/true or false games and it is pretty easy for me now.</p> <p>I decided to take a further step, and I must say it's totally a pain, different logic.</p> <p>That's what I've done so far:</p> <p>Main.java:</p> <pre><code>import java.awt.*; import javax.swing.*; import java.io.*; class Main { public static void main(String[] args) { final Gui gui = new Gui(); SwingUtilities.invokeLater(new Runnable() { public void run() { gui.createMyGui(); } }); } } </code></pre> <p>gui.java</p> <pre><code>class Gui { protected JFrame j = new JFrame("My First window"); protected JPanel p = new JPanel(); protected Container c; public Gui() { j.setSize(500, 400); p.setSize(j.getSize()); this.c = j.getContentPane(); } public void createMyGui() { setButtons(); setGuiBackground(); j.setVisible(true); p.setVisible(true); this.c.add(p); } private void setGuiBackground() { this.c.setBackground(Color.green); } private void setButtons() { p.add(new JButton("Hey")); } } </code></pre> <p><strong>Problem</strong></p> <p>I can't really get the button to show up, people are telling me to use setBounds but I am not really sure on how to start as I can't even place a button there. I've tried searching about my problem, but no luck actually.</p> <p>Basically what happens is a 500x400 green GUI opens, and that's it.</p> <p><em><strong>Why won't the button show?</em></strong></p>
 

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