terraform
terraform

Reputation: 1

frame.getContentPane().add(); not working properly?

I've been having some (very annoying) trouble with these scripts that I've created.

Sburb.java

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

public class Sburb
{
public static void main (String[] args)
{
    JFrame frame = new JFrame ("Welcome to Sburb");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    spirograph page = new spirograph();
    progressbar bar = new progressbar();

    frame.getContentPane().add(page);
    frame.getContentPane().add(bar);
    frame.pack();
    frame.setVisible(true);
    frame.setResizable(true);
    }
}

progressbar.java

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

public class progressbar extends JPanel
{

JProgressBar current;
JTextArea out;
JButton find;
Thread runner;
int num = 1;

progressbar() 
{
    super();

    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pane = new JPanel();
    pane.setLayout(new FlowLayout());
    current = new JProgressBar(0, 2000);
    current.setStringPainted(false);
    pane.add(current);
    //setContentPane(pane);
}


public void iterate() {
    while (num < 2000) {
        current.setValue(num);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) { }
        num += 5;
    }
}

//    public static void main(String[] arguments) {
//        progressbar frame = new progressbar();
//        frame.pack();
//        frame.setVisible(true);
//        frame.iterate();
//   }
}

spirograph.java

import java.awt.Color;

import java.awt.FlowLayout;


import javax.swing.ImageIcon;

import javax.swing.JLabel;
import javax.swing.JPanel;


public class spirograph extends JPanel
{
private ImageIcon image;
private JLabel label;
private JLabel frame = new JLabel();
private JPanel panel = new JPanel();

 spirograph() 
{

    this.setOpaque(true);
    this.setBackground(Color.BLACK);

    setLayout(new FlowLayout());
    image = new ImageIcon(getClass().getResource("Gate.gif"));
    label = new JLabel(image);
    add(label);

    progressbar bar = new progressbar();
    }
}

I'm trying to call the file "progressbar" to the Sburb file but when I do, it gives me just the simple JFrame of this (not fixed):

https://i.sstatic.net/GHZTl.jpg

And when I get rid of the "frame.getContentPane().add(bar);" in Sburb.java, it gives me this (fixed, kind-of):

https://i.sstatic.net/Brmke.jpg

How do I fix this? I've looked everywhere and yet I still can't figure it out! I also can't seem to figure out how to align the bar directly below the gif.

Upvotes: 0

Views: 1427

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168835

frame.getContentPane().add(page);
frame.getContentPane().add(bar);

The content pane of a JFrame is a set to a BorderLayout which can only accept one component in any one of the border layout constraints. Given no constraints were supplied here, the JRE will try to put them both in the CENTER.

For this, and a variety of other reasons, I would advise to ignore the existing content pane, arrange the entire GUI (as many panels as it consists of) into another panel (let's call it ui) then call

frame.setContentPane(ui);

Upvotes: 4

Related Questions