Results 1 to 5 of 5
  1. #1
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default How to put dice on a JPanel?

    Hello,

    I am trying to put dice on a JPanel and JPanel on a frame, but i can't see the dice when I run my code. However, when I put dice directly on a frame it shows.Also buttons aren't shown. This is the code:

    import java.awt.*;
    import javax.swing.*;


    public class Main {

    public static void main(String[] args) {
    JFrame frame;
    frame = new JFrame("my frame");
    frame.setSize(800,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);


    Box1 b1 = new Box1();
    b1.repaint();

    JPanel mainP = new JPanel();
    mainP.setLayout(null);
    JButton but = new JButton("new");
    JButton but1 = new JButton("new1");
    try {
    mainP.add(but);
    mainP.add(but1);
    mainP.add( b1);
    } catch(Exception ex) {
    ex.printStackTrace();
    }

    frame.setContentPane(mainP);
    b1.repaint();
    frame.validate();



    }
    }



    import java.awt.*;
    import javax.swing.*;


    public class Box1 extends JPanel {

    public void paintComponent(Graphics g) {
    g.setColor(Color.red);
    g.fillRect(20,100,100,100);
    g.setColor(Color.black);
    g.fillOval(50,130,30,30);
    System.out.println("adad");
    }
    }

    Thanks a lot

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,706
    Rep Power
    5

    Default Re: How to put dice on a JPanel?

    Java Code:
    frame.setContentPane(mainP);
    Don't do that - ADD the panel to the frame's content pane.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to put dice on a JPanel?

    I changed, still not working

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,706
    Rep Power
    5

    Default Re: How to put dice on a JPanel?

    You should give your panel a layout.

    That's it, I already see that this is going to turn into a ping-pong thread. I gave you two mistakes in your code already, if after that it still doesn't work try to see if you can find something more yourself by reading through the Swing tutorial.

    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Ivan is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: How to put dice on a JPanel?

    It works with boxlayout and borderlayout but don't with flowlayout, I tried only those three. I don't know the reason of that.
    Thanks for help!

Similar Threads

  1. Dice Game
    By DD13 in forum New To Java
    Replies: 3
    Last Post: 03-29-2013, 04:12 AM
  2. Replies: 0
    Last Post: 02-17-2013, 09:21 PM
  3. Change JPanel text of Parent JPanel from JDialog
    By bikashlama in forum AWT / Swing
    Replies: 7
    Last Post: 12-09-2011, 03:47 AM
  4. Dice Game
    By Rachel1991 in forum New To Java
    Replies: 1
    Last Post: 11-24-2011, 11:10 PM
  5. Pig Dice Game Help
    By pvccstudent in forum New To Java
    Replies: 3
    Last Post: 07-10-2011, 02:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •