IntrepidPig
IntrepidPig

Reputation: 110

Swing - Java program showing blank screen

I'm making a new program that my mom suggested. It's supposed to list items we own, and items we need. Right now I only have one class, the window creation class and it's giving me a blank screen. I don't know why, but I'm probably missing some small vital step. Here's the code so far:

public class ListerWindow {

static JButton addToOwned = new JButton("Add Item To Owned List");
static JButton removeFromOwned = new JButton("Remove Item From Owned List");
static JButton addToNeeded = new JButton("Add Item To Shopping List");
static JButton removeFromNeeded = new JButton("Remove From Shopping List");
static JTextArea neededList = new JTextArea();
static JTextArea ownedList = new JTextArea();
static JFrame frame = new JFrame("Shopping Lister");

static JLabel ownedListLabel = new JLabel("Owned List");
static JLabel neededListLabel = new JLabel("Shopping List");

public static void ListerWindowCreator(String[] args) {
    JPanel windowContent = new JPanel();
    GridLayout gl = new GridLayout(4,3);
    windowContent.setLayout(gl);

    windowContent.add(ownedListLabel);
    windowContent.add(neededListLabel);
    windowContent.add(ownedList);
    windowContent.add(neededList);
    windowContent.add(addToOwned);
    windowContent.add(addToNeeded);
    windowContent.add(removeFromOwned);
    windowContent.add(removeFromNeeded);

    neededList.setEditable(false);
    ownedList.setEditable(false);
    ownedListLabel.setForeground(Color.BLUE);
    neededListLabel.setForeground(Color.BLUE);
    frame.setBackground(Color.BLACK);
    removeFromNeeded.setForeground(Color.RED);
    removeFromOwned.setForeground(Color.RED);
    addToNeeded.setForeground(Color.GREEN);
    addToOwned.setForeground(Color.GREEN);

    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    frame.setVisible(true);

    }
public static void main(String[] args) {
    new ListerWindow();
    ListerWindowCreator(args);
}
}

I'm pretty sure I've gotten all the imports, so far I've got javax.swing.* and java.awt.*

PS: Can I debug in eclipse, and how?

Upvotes: 2

Views: 1639

Answers (2)

Masudul
Masudul

Reputation: 21961

You need to add windowContent with JFrame

   frame.add(windowContent);

For exact size of JFrame you can use frame.pack(); instead of frame.setExtendedState (Frame.MAXIMIZED_BOTH);

Upvotes: 4

Hope this is what you want

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

public class ListerWindow{

    static JButton addToOwned = new JButton("Add Item To Owned List");
    static JButton removeFromOwned = new JButton("Remove Item From Owned List");
    static JButton addToNeeded = new JButton("Add Item To Shopping List");
    static JButton removeFromNeeded = new JButton("Remove From Shopping List");
    static JTextArea neededList = new JTextArea();
    static JTextArea ownedList = new JTextArea();
    static JFrame frame = new JFrame("Shopping Lister");

    static JLabel ownedListLabel = new JLabel("Owned List");
    static JLabel neededListLabel = new JLabel("Shopping List");

    public static void ListerWindowCreator(String[] args) {
        JPanel windowContent = new JPanel();
        GridLayout gl = new GridLayout(4,2);
        windowContent.setLayout(gl);

        windowContent.add(ownedListLabel);
        ownedList.setText("ownedList");
        windowContent.add(ownedList);
        windowContent.add(neededListLabel);
        neededList.setText("NeededList");
        windowContent.add(neededList);
        windowContent.add(addToOwned);
        windowContent.add(addToNeeded);
        windowContent.add(removeFromOwned);
        windowContent.add(removeFromNeeded);

       // neededList.setEditable(false);
       // ownedList.setEditable(false);
        ownedListLabel.setForeground(Color.BLUE);
        neededListLabel.setForeground(Color.BLUE);
        frame.setBackground(Color.BLACK);
        removeFromNeeded.setForeground(Color.RED);
        removeFromOwned.setForeground(Color.RED);
        addToNeeded.setForeground(Color.GREEN);
        addToOwned.setForeground(Color.GREEN);

        frame.setContentPane(windowContent);
        frame.setSize(200,200);
        //frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setVisible(true);

    }
    public static void main(String[] args) {
        new ListerWindow();
        ListerWindowCreator(args);
    }
}

Upvotes: 0

Related Questions