0

I am creating a Java GUI & graphics, but am having an issue with the size of a jpanel, and cannot figure out what I am messing up. The jpanel appears extremely small. I cannot find the issue. Below is the code & image of how the project currently appears:

LePanel.java

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;

public class LePanel extends JPanel {

    public LePanel() {
        //Initialize

        //Configure
            //this.setSize(100,100);

        //Add

        //Run

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g); //Not sure if this is necessary
        this.setBackground(Color.RED);
        g.setColor(Color.BLUE);
        g.fillRect(5, 5, 70, 70);
    }

}

LeFrame.java

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

public class LeFrame extends JFrame {
    private LePanel graphicsPractice;
    private GridBagConstraints gbc;

    public LeFrame() {
        super("Frame");

        //Initialize
        graphicsPractice = new LePanel();
        gbc = new GridBagConstraints();

        //Configure
        this.getContentPane().setLayout(new GridBagLayout());


        //Add
        //gbc.gridx = 0;
        //gbc.gridy = 0;
        this.getContentPane().add(graphicsPractice);

        //Finalizing JFrame
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1000,700);
        this.setVisible(true);

    }

}

Driver.java

import javax.swing.SwingUtilities;

public class Driver {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new LeFrame();
            }
        });
    }
}

Image Current State Of GUI

chromechris
  • 215
  • 4
  • 9
  • 1
    What does the title have to do with the question? Please edit the title to match. – Jim Garrison Feb 26 '17 at 00:37
  • 1
    Override the panels getPreferredSize method and return the desired size you want use – MadProgrammer Feb 26 '17 at 00:42
  • @Jim Garrison Title edited.I was going to post a separate post previously so the title was saved for the next post, & I forgot to update it. – chromechris Feb 26 '17 at 01:24
  • @MadProgrammer Thanks! I instead setPreferredSize(dimension). I set the sizing within the dimension. – chromechris Feb 26 '17 at 01:25
  • 1
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513) – trashgod Feb 26 '17 at 03:30
  • @trashgod Cool, I instead overrode getPreferredSize(), getMaximumSize(), & getMinimumSize(). Thanks! – chromechris Feb 26 '17 at 18:34
  • @chromechris: You can [answer your own question](http://meta.stackoverflow.com/q/17463/163188). In your [mcve], access posted images via `URL`, as shown [here](http://stackoverflow.com/a/10862262/230513); use synthetic images as shown [here](http://stackoverflow.com/a/15982915/230513); or use `UIManager` icons, as shown [here](http://stackoverflow.com/a/12228640/230513). – trashgod Feb 27 '17 at 07:50

0 Answers0