简体   繁体   中英

JPanels will not show up in JFrame

I'm making a simple GUI calculator. Before I insert the guts, I'm trying to get the GUI down. The problem that I'm having is that my JPanels containing the keypad and display will not show up in the main JFrame for the program. What am I missing?

Main:

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

public class Main extends JFrame {

    public Main() {
        calc();
    }

    public final void calc() {
        JFrame main = new JFrame("Alex's Calculator");
        Display disp = new Display();
        Keypad keyp = new Keypad();
        JPanel c = new JPanel();

        c.setLayout(new BorderLayout());
        c.add(disp, BorderLayout.NORTH);
        c.add(keyp, BorderLayout.SOUTH);

        main.setDefaultCloseOperation(EXIT_ON_CLOSE);
        main.setSize(400, 300);
        c.setVisible(true);
        main.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Main();
            }
        });
    }
}

Keypad: (Yes, I know. I'm going to add a loop to create an array of buttons.)

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


public class Keypad extends JPanel{

    public Keypad() {

        int num = 1;
        JPanel Keypad = new JPanel();
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton bDec = new JButton(".");
        JButton b0 = new JButton("0");
        JButton bPlus = new JButton("+");
        JButton bMinus = new JButton("-");
        JButton bDiv = new JButton("/");
        JButton bMult = new JButton("*");
        JButton bBOR = new JButton("|");
        JButton bBAND = new JButton("&");
        JButton bModu = new JButton("%");
        JButton bEq = new JButton("=");
        JButton bC = new JButton("Clear");   

        Keypad.add(b1);
        b1.setMnemonic('1');

        Keypad.add(b2);
        b2.setMnemonic('2');

        Keypad.add(b3);
        b3.setMnemonic('3');

        Keypad.add(bPlus);
        bPlus.setMnemonic('+');

        Keypad.add(bBOR);
        bBOR.setMnemonic('|');

        Keypad.add(b4);
        b4.setMnemonic('4');

        Keypad.add(b5);
        b5.setMnemonic('5');

        Keypad.add(b6);
        b6.setMnemonic('6');

        Keypad.add(bMinus);
        bMinus.setMnemonic('-');

        Keypad.add(bBAND);
        bBAND.setMnemonic('&');

        Keypad.add(b7);
        b7.setMnemonic('7');

        Keypad.add(b8);
        b8.setMnemonic('8');

        Keypad.add(b9);
        b9.setMnemonic('9');

        Keypad.add(bMult);
        bMult.setMnemonic('*');

        Keypad.add(bModu);
        bModu.setMnemonic('%');

        Keypad.add(b0);
        b0.setMnemonic('0');

        Keypad.add(bDec);
        bDec.setMnemonic('.');

        Keypad.add(bC);
        bC.setMnemonic('C');

        Keypad.add(bDiv);
        bDiv.setMnemonic('/');

        Keypad.add(bEq);
        bEq.setMnemonic('=');  

        Keypad.setLayout(new GridLayout(4, 5));

        Keypad.setSize(400, 200);
        Keypad.setVisible(true);
    }  
}

Display:

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

public class Display extends JPanel{

    JTextField result = new JTextField(20);

    public Display() {
        JPanel display = new JPanel();

        display.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));
        display.add(result);
        display.setSize(400, 100);
        result.setHorizontalAlignment(JTextField.RIGHT);
        result.setEditable(false);
        display.setVisible(true);
    }
}

I'm fairly new to Java, and to programming as a whole.
Any help would be greatly appreciated. Thanks!

There are two problems in your code:

  1. You need to call main.getContentPane().add(c, BorderLayout.CENTER); to add JPanel c to your frame. Make sure to do this as one of the first things you do to main .
  2. In your JPanels, you are creating objects locally that are never returned from the classes.

More discussion on #2 below. But first, your Main class itself is already a JFrame, so you really don't need to create a separate JFrame called main . Instead, do this:

    Display disp = new Display();
    Keypad keyp = new Keypad();

    JPanel c = new JPanel();
    c.setLayout(new BorderLayout());
    c.add(disp, BorderLayout.NORTH);
    c.add(keyp, BorderLayout.SOUTH);

    this.setTitle("Alex's Calculator");
    this.getContentPane().add(c, BorderLayout.CENTER);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(400, 300);
    this.setVisible(true);

Now back to those JPanels. In both cases, you create JPanel objects ( Keypad and display ) but those are never returned from the classes. Instead, make sure you're working with the fact that your classes extend JPanel - in other words, work on this instead of creating a separate object. For example, your new Display should look like:

public Display() {
    this.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));
    this.add(result);
    this.setSize(400, 100);
    result.setHorizontalAlignment(JTextField.RIGHT);
    result.setEditable(false);
    this.setVisible(true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM