Why is my JPanel taking half of the screen and how can i fix this?

375 Views Asked by At

I am having an issue where it seem like my JPanel is taking half of my screen no matter what, even though it doesn't need to.

I am using the acm package to make a graphical simulation, with sliders to adjust the parameters of a simulation

import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import acm.gui.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Test extends GraphicsProgram{

sliderBox X_MIN = new sliderBox("X MIN", 10, 300, 50);

public void init() {

    JPanel myPanel = new JPanel();
    JLabel myLabel = new JLabel("Test");
    TableLayout myLayout = new TableLayout(5, 1);
    myPanel.setVisible(true);
    myPanel.setLayout(myLayout);
    myPanel.add(myLabel);
    myPanel.add(X_MIN.myPanel);
    add(myPanel);
    println(myPanel.getPreferredSize());

}


public void run() {

    resize(1500, 900);
    add(new GRect(10, 10, 60, 50));

}
}

Though I can't upload pictures on my post due to me being new, the resulting output is a window where half of the screen is the white canvas from the acm.graphics and the second half of the screen is an unnecessarily big gray panel that doesn't optimize the containment of the few components that are actually in the panel

Thanks in advance

**Edit here is my sliderBox class

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

public class sliderBox {

JPanel myPanel;
JLabel nameLabel;
JLabel minLabel;
JSlider mySlider;
JLabel maxLabel;
JLabel sReadout;
int imin;
int imax;
double dmin;
double dmax;

public sliderBox (String name, Integer min, Integer max, Integer iniValue) {

    myPanel = new JPanel();
    nameLabel = new JLabel(name);
    minLabel = new JLabel(min.toString());
    mySlider = new JSlider(min, max, iniValue);
    maxLabel = new JLabel(max.toString());
    sReadout = new JLabel(iniValue.toString());
    sReadout.setForeground(Color.BLUE);
    myPanel.setLayout(new TableLayout(1, 5));
    myPanel.add(nameLabel, "width = 80");
    myPanel.add(minLabel, "width = 20");
    myPanel.add(mySlider, "width = 120");
    myPanel.add(maxLabel, "width = 60" );
    myPanel.add(sReadout, "width = 80");
    imin = min;
    imax = max;

}


}
0

There are 0 best solutions below