I'm writing a class to create a fraction object that I can display along with some text. I have a black background panel and two white panels on top oriented vertically with a 3 pixel spacer between them. Each white panel contains a JLabel.
This creates the numerator, denominator, and fraction bar. Everything works fine when the numerator and denominator are the same length (for example 1/2). But once the numerator and denominator are different sizes (for example 1/12) then one of the panels is too small and gets black along the sides. I'm trying to force the small panel to take up the full width of the background panel but can't seem to get this to work right. Here is my class.
public class MakeFraction2 {
private JLabel jlabelNum;
private JLabel jlabelDen;
JPanel mainpanel = new JPanel();
JPanel numpanel = new JPanel();
JPanel denpanel = new JPanel();
private Font font3 = new Font("Monospaced", Font.BOLD, 17);
private Font font4 = new Font ("SansSerif", Font.BOLD, 17);
MakeFraction2(){
jlabelNum = new JLabel("Num");
jlabelDen = new JLabel("Den");
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.Y_AXIS));
numpanel.setLayout(new BoxLayout(numpanel, BoxLayout.X_AXIS));
denpanel.setLayout(new BoxLayout(denpanel, BoxLayout.X_AXIS));
mainpanel.setBackground(Color.BLACK);
numpanel.setBackground(Color.WHITE);
denpanel.setBackground(Color.WHITE);
numpanel.add(jlabelNum);
denpanel.add(jlabelDen);
mainpanel.add(numpanel);
mainpanel.add(Box.createRigidArea(new Dimension(1,2)));
mainpanel.add(denpanel);
}
public void setnumden(String num, String den) {
mainpanel.setBackground(Color.BLACK);
jlabelNum.setText(num);
jlabelDen.setText(den);
sizepanels();
}
void sizepanels(){
int width = mainpanel.getWidth();
int h1 = numpanel.getHeight();
int h2 = denpanel.getHeight();
int height = Math.max(h1, h2);
numpanel.setSize(width,height);
denpanel.setSize(width,height);
}
}