Results 1 to 3 of 3
  1. #1
    radiodave is offline Member
    Join Date
    May 2015
    Posts
    15
    Rep Power
    0

    Default Newb JPanel with rectangle and buttons

    I'm trying to create a UI with a rectangle to draw lines on, and some buttons. Trying different approaches, I seem to be able to do one or the other, but not both. I used setLayout(null) because I want the buttons in a specific place. When I run it, I see the rectangle OK, but I only see one button. If I leave out the setLayout(null), then I see both buttons, but they are superimposed on the box. I just want to put the buttons in a specific location, but it seems I need setLayout(null) in order to do that. What am I missing?

    Java Code:
    public class UI extends JPanel {
        private boolean bline;
        JButton ResetButton;
        JButton ZeroButton;
    
    final int iButtonWidth  = 70;
        final int iButtonHeight = 70;
        
        
        final int iZeroButtonStartX = 100;
        final int iZeroButtonStartY = 200;
        final int iResetButtonStartX = 300;
        final int iResetButtonStartY = 200;
    
    public void paint (Graphics g) {
            g.setColor(Color.black);
            g.fillRect(iDisplayStartX, iDisplayStartY, iDisplayWidth, iDisplayHeight);
            
            g.drawLine(20, 20, 40, 40);
                
        }
    
    public UI ()
    {
    setLayout(null);
            ResetButton = new JButton("Reset");
            ResetButton.setBounds(iResetButtonStartX, iResetButtonStartY, iButtonWidth, iButtonHeight);
            ZeroButton = new JButton("0");
            ZeroButton.setBounds(iZeroButtonStartX, iZeroButtonStartY, iButtonWidth, iButtonHeight);
            add(ResetButton);
            add(ZeroButton);
            revalidate();
    
    }
    
    .
    .
    .
    
    public static void main()
    {
    JFrame frame = new JFrame("TITLE");
            frame.add(new UI());
            frame.setSize(iWindowSizeX, iWindowSizeY);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setResizable(false);
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Newb JPanel with rectangle and buttons

    If you use a Null-layout, buttons need a bounds (width, height, x and y). If you set a layout of some sort, the LayoutManager will decide where the buttons go. By default, a JPanel uses a FlowLayout., so there is always a LayoutManager active for each new JPanel. Setting it to null will remove it.

    First of all, you should override paintComponent(Graphics) and NOT paint(Graphics).

    Second, public static void main requires String[] as parameter is you want this class to start.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    radiodave is offline Member
    Join Date
    May 2015
    Posts
    15
    Rep Power
    0

    Default Re: Newb JPanel with rectangle and buttons

    Thanks, I got it figured out. Overriding the paintComponent was the key thing I was missing. Seems to be working now.

Similar Threads

  1. Replies: 3
    Last Post: 12-08-2013, 09:28 PM
  2. Replies: 3
    Last Post: 08-15-2011, 01:56 PM
  3. Replies: 3
    Last Post: 01-12-2011, 10:43 PM
  4. non-rectangle JPanel
    By itaipee in forum AWT / Swing
    Replies: 4
    Last Post: 04-30-2009, 11:58 PM
  5. Replies: 2
    Last Post: 03-22-2009, 01:53 PM

Posting Permissions

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