简体   繁体   中英

Why is my JPanel not showing on a JFrame?

I have made a calculator by referencing to another's source code, but as I try to compile the file, the frame will display, leaving the panel and simply showing nothing but a blank frame. I use a basic frame.add(new Calculator()); for the methods within the file. Here is the code:

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

public class Calculator2 extends JFrame {
    public static void main(String[]args) {
        JFrame frame = new JFrame("Test Calculator");
        Windows c = new Windows();
        c.setVisible(true);
        frame.add(c);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class Windows extends JPanel {
    static boolean[] function = new boolean[4];
    static double[] temporary = {0, 0}; 
    static JTextArea display = new JTextArea(1,20);

    public static JPanel Windows() {
        JPanel panel = new JPanel();
        panel.setSize(200, 300);
        GridLayout grid = new GridLayout(5, 5);
        panel.setLayout(grid);

        JPanel[] row = new JPanel[5];
        JButton[] buttons = new JButton[19];
        String[] buttonString = {"7","8","9","+",
                                 "4", "5", "6", "-",
                                 "1", "2", "3", "*",
                                 ".", "/", "C", "v",
                                 "+/-", "=", "0"};
        int[] dimW = {300, 45, 100, 90};
        int[] dimH = {35, 40};
        Dimension dispDim = new Dimension(dimW[0], dimH[0]);
        Dimension regDim = new Dimension(dimW[1], dimH[1]);
        Dimension rColDim = new Dimension(dimW[2], dimH[1]);
        Dimension zeroButDim = new Dimension(dimW[3], dimH[1]);
        Font font = new Font("Times new Roman", Font.BOLD, 14);

        for(int i = 0; i < 4; i++)
            function[i] = false;

        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
        FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
        for(int x = 0; x < 5; x++)
            row[x] = new JPanel();
        row[0].setLayout(f1);
        for(int x = 0; x < 5; x++)
            row[x].setLayout(f2);

        for(int i = 0; i < 19; i++) {
            buttons[i] = new JButton();
            buttons[i].setText(buttonString[i]);
            buttons[i].setFont(font);
            buttons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent b) {
                    if(b.getSource() == buttons[0])
                        display.append("7");
                    if(b.getSource() == buttons[1])
                        display.append("8");
                    if(b.getSource() == buttons[2])
                        display.append("9");
                    if(b.getSource() == buttons[3]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[0] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[4])
                        display.append("4");
                    if(b.getSource() == buttons[5])
                        display.append("5");
                    if(b.getSource() == buttons[6])
                        display.append("6");
                    if(b.getSource() == buttons[7]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[1] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[8])
                        display.append("1");
                    if(b.getSource() == buttons[9])
                        display.append("2");
                    if(b.getSource() == buttons[10])
                        display.append("3");
                    if(b.getSource() == buttons[11]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[2] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[12])
                        display.append(".");
                    if(b.getSource() == buttons[13]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[3] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[14])
                        clear();
                    if(b.getSource() == buttons[15])
                        getSqrt();
                    if(b.getSource() == buttons[16])
                        getResult();
                    if(b.getSource() == buttons[17])
                        display.append("0");
                }
            });
        }

        display.setFont(font);          
        display.setEditable(false);
        display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        display.setPreferredSize(dispDim);
        for(int i = 0; i < 14; i++)
            buttons[i].setPreferredSize(regDim);
        for(int i = 14; i < 18; i++) 
            buttons[i].setPreferredSize(rColDim);
        buttons[18].setPreferredSize(zeroButDim);

        row[0].add(display);
        panel.add(row[0]);

        for(int i = 0; i < 4; i++)
            row[1].add(buttons[i]);
        row[1].add(buttons[14]);
        panel.add(row[1]);

        for(int i = 4; i < 8; i++)
            row[2].add(buttons[i]);
        row[2].add(buttons[15]);
        panel.add(row[2]);

        for(int i = 8; i < 12; i++)
            row[3].add(buttons[i]);
        row[3].add(buttons[16]);
        panel.add(row[3]);

        row[4].add(buttons[18]);
        for(int i = 12; i < 14; i++)
            row[4].add(buttons[i]);
        row[4].add(buttons[17]);
        panel.add(row[4]);

        panel.setVisible(true);

        return panel;
    }

    public static void clear() {
        try {
            display.setText("");
            for(int i = 0; i < 4; i++)
                function[i] = false;
            for(int i = 0; i < 2; i++)
                temporary[i] = 0;
        } catch(NullPointerException e) {
        }
    }
    public static void getSqrt() {
        try {
            double value = Math.sqrt(Double.parseDouble(display.getText()));
            display.setText(Double.toString(value));
        } catch(NumberFormatException e) {
        }
    }
    public static void getPosNeg() {
        try {
            double value = Double.parseDouble(display.getText());
            if(value != 0) {
                value = value * (-1);
                display.setText(Double.toString(value));
            }
            else {
            }
        } catch(NumberFormatException e) {
        }
    }
    public static void getResult() {
        double result = 0;
        temporary[1] = Double.parseDouble(display.getText());
        String temp0 = Double.toString(temporary[0]);
        String temp1 = Double.toString(temporary[1]);
        try {
            if(temp0.contains("-")) {
                String[] temp00 = temp0.split("-", 2);
                temporary[0] = (Double.parseDouble(temp00[1]) *-1);
            }
            if(temp1.contains("-")) {
                String[] temp11 = temp1.split("-", 2);
                temporary[1] = (Double.parseDouble(temp11[1]) * -1);
            }
        } catch(ArrayIndexOutOfBoundsException e) {
        }
        try {
            if(function[2] == true)
                result = temporary[0] * temporary[1];
            else if(function[3] == true)
                result = temporary[0] / temporary[1];
            else if(function[0] == true)
                result = temporary[0] + temporary[1];
            else if(function[1] == true)
                result = temporary[0] - temporary[1];
            display.setText(Double.toString(result));
            for(int i = 0; i < 4; i++)
                function[i] = false;
        } catch(NumberFormatException e) {
        }
    }
    public final void setDesign() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception e) {
        }
    }
    public static void main(String[] args) {
        JPanel pane = new JPanel();
        pane.add(Windows());
        pane.setSize(500, 500);
        pane.setVisible(true);
    }
}

FIXED: Had to change the Windows c = new Windows(); to JPanel c = Windows.Windows(); as it's a method returning a JPanel, rather than a constructor. Credit to: @EdwinTorres & @DirkyJerky

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

public class Calculator2 extends JFrame {
    public static void main(String[]args) {
        JFrame frame = new JFrame("Test Calculator");
        Windows c = new Windows();
        c.setVisible(true);
        frame.add(c);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class Windows extends JPanel {
    static boolean[] function = new boolean[4];
    static double[] temporary = {0, 0}; 
    static JTextArea display = new JTextArea(1,20);

    public static JPanel Windows() {
        JPanel panel = new JPanel();
        panel.setSize(200, 300);
        GridLayout grid = new GridLayout(5, 5);
        panel.setLayout(grid);

        JPanel[] row = new JPanel[5];
        JButton[] buttons = new JButton[19];
        String[] buttonString = {"7","8","9","+",
                                 "4", "5", "6", "-",
                                 "1", "2", "3", "*",
                                 ".", "/", "C", "v",
                                 "+/-", "=", "0"};
        int[] dimW = {300, 45, 100, 90};
        int[] dimH = {35, 40};
        Dimension dispDim = new Dimension(dimW[0], dimH[0]);
        Dimension regDim = new Dimension(dimW[1], dimH[1]);
        Dimension rColDim = new Dimension(dimW[2], dimH[1]);
        Dimension zeroButDim = new Dimension(dimW[3], dimH[1]);
        Font font = new Font("Times new Roman", Font.BOLD, 14);

        for(int i = 0; i < 4; i++)
            function[i] = false;

        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
        FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
        for(int x = 0; x < 5; x++)
            row[x] = new JPanel();
        row[0].setLayout(f1);
        for(int x = 0; x < 5; x++)
            row[x].setLayout(f2);

        for(int i = 0; i < 19; i++) {
            buttons[i] = new JButton();
            buttons[i].setText(buttonString[i]);
            buttons[i].setFont(font);
            buttons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent b) {
                    if(b.getSource() == buttons[0])
                        display.append("7");
                    if(b.getSource() == buttons[1])
                        display.append("8");
                    if(b.getSource() == buttons[2])
                        display.append("9");
                    if(b.getSource() == buttons[3]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[0] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[4])
                        display.append("4");
                    if(b.getSource() == buttons[5])
                        display.append("5");
                    if(b.getSource() == buttons[6])
                        display.append("6");
                    if(b.getSource() == buttons[7]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[1] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[8])
                        display.append("1");
                    if(b.getSource() == buttons[9])
                        display.append("2");
                    if(b.getSource() == buttons[10])
                        display.append("3");
                    if(b.getSource() == buttons[11]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[2] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[12])
                        display.append(".");
                    if(b.getSource() == buttons[13]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[3] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[14])
                        clear();
                    if(b.getSource() == buttons[15])
                        getSqrt();
                    if(b.getSource() == buttons[16])
                        getResult();
                    if(b.getSource() == buttons[17])
                        display.append("0");
                }
            });
        }

        display.setFont(font);          
        display.setEditable(false);
        display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        display.setPreferredSize(dispDim);
        for(int i = 0; i < 14; i++)
            buttons[i].setPreferredSize(regDim);
        for(int i = 14; i < 18; i++) 
            buttons[i].setPreferredSize(rColDim);
        buttons[18].setPreferredSize(zeroButDim);

        row[0].add(display);
        panel.add(row[0]);

        for(int i = 0; i < 4; i++)
            row[1].add(buttons[i]);
        row[1].add(buttons[14]);
        panel.add(row[1]);

        for(int i = 4; i < 8; i++)
            row[2].add(buttons[i]);
        row[2].add(buttons[15]);
        panel.add(row[2]);

        for(int i = 8; i < 12; i++)
            row[3].add(buttons[i]);
        row[3].add(buttons[16]);
        panel.add(row[3]);

        row[4].add(buttons[18]);
        for(int i = 12; i < 14; i++)
            row[4].add(buttons[i]);
        row[4].add(buttons[17]);
        panel.add(row[4]);

        panel.setVisible(true);

        return panel;
    }

    public static void clear() {
        try {
            display.setText("");
            for(int i = 0; i < 4; i++)
                function[i] = false;
            for(int i = 0; i < 2; i++)
                temporary[i] = 0;
        } catch(NullPointerException e) {
        }
    }
    public static void getSqrt() {
        try {
            double value = Math.sqrt(Double.parseDouble(display.getText()));
            display.setText(Double.toString(value));
        } catch(NumberFormatException e) {
        }
    }
    public static void getPosNeg() {
        try {
            double value = Double.parseDouble(display.getText());
            if(value != 0) {
                value = value * (-1);
                display.setText(Double.toString(value));
            }
            else {
            }
        } catch(NumberFormatException e) {
        }
    }
    public static void getResult() {
        double result = 0;
        temporary[1] = Double.parseDouble(display.getText());
        String temp0 = Double.toString(temporary[0]);
        String temp1 = Double.toString(temporary[1]);
        try {
            if(temp0.contains("-")) {
                String[] temp00 = temp0.split("-", 2);
                temporary[0] = (Double.parseDouble(temp00[1]) *-1);
            }
            if(temp1.contains("-")) {
                String[] temp11 = temp1.split("-", 2);
                temporary[1] = (Double.parseDouble(temp11[1]) * -1);
            }
        } catch(ArrayIndexOutOfBoundsException e) {
        }
        try {
            if(function[2] == true)
                result = temporary[0] * temporary[1];
            else if(function[3] == true)
                result = temporary[0] / temporary[1];
            else if(function[0] == true)
                result = temporary[0] + temporary[1];
            else if(function[1] == true)
                result = temporary[0] - temporary[1];
            display.setText(Double.toString(result));
            for(int i = 0; i < 4; i++)
                function[i] = false;
        } catch(NumberFormatException e) {
        }
    }
    public final void setDesign() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception e) {
        }
    }
    public static void main(String[] args) {
        JPanel pane = new JPanel();
        pane.add(Windows());
        pane.setSize(500, 500);
        pane.setVisible(true);
    }
}

(May not be correct)

When you do pane.add(Windows()); it does the constructor for the Windows clas (because the method is static), not the public static JPanel Windows() method you are looking for.

EDIT: Try: pane.add(Windows.Windows())

public static JPanel Windows() { is a method , and in Windows c = new Windows(); you are invoking the constructor (that does nothing), not the method you are looking for.

Change public static JPanel Windows() to public Windows()

Double Edit:

Just change public static JPanel Windows() to public static JPanel newWindows() to fix the compiler warning,

And change Windows c = new Windows(); to JPanel c = Windows.newWindows();

You define Windows() as a static method, not a constructor. So you need to call it correctly.

On Line 10, you need to do this:

frame.add(Windows.Windows()); 

On Line 28, set this to final:

final JButton[] buttons = new JButton[19];

I did a bit of refactoring, your Windows class lacked a constructor (and its static Windows() method was meant to be, I guess).

This should work.

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

public class Calculator2 implements Runnable {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Calculator2());
    }

    @Override
    public void run() {
        JFrame f = new JFrame();
        Windows c = new Windows();
        c.setVisible(true);
        f.add(c);
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
class Windows extends JPanel {
    static boolean[] function = new boolean[4];
    static double[] temporary = {0, 0}; 
    static JTextArea display = new JTextArea(1,20);

    public Windows() {
        super(new GridLayout(5, 5));
        this.setSize(200, 300);
        GridLayout grid = new GridLayout(5, 5);
        this.setLayout(grid);

        JPanel[] row = new JPanel[5];
        final JButton[] buttons = new JButton[19];
        String[] buttonString = {"7","8","9","+",
                                 "4", "5", "6", "-",
                                 "1", "2", "3", "*",
                                 ".", "/", "C", "v",
                                 "+/-", "=", "0"};
        int[] dimW = {300, 45, 100, 90};
        int[] dimH = {35, 40};
        Dimension dispDim = new Dimension(dimW[0], dimH[0]);
        Dimension regDim = new Dimension(dimW[1], dimH[1]);
        Dimension rColDim = new Dimension(dimW[2], dimH[1]);
        Dimension zeroButDim = new Dimension(dimW[3], dimH[1]);
        Font font = new Font("Times new Roman", Font.BOLD, 14);

        for(int i = 0; i < 4; i++)
            function[i] = false;

        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
        FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
        for(int x = 0; x < 5; x++)
            row[x] = new JPanel();
        row[0].setLayout(f1);
        for(int x = 0; x < 5; x++)
            row[x].setLayout(f2);

        for(int i = 0; i < 19; i++) {
            buttons[i] = new JButton();
            buttons[i].setText(buttonString[i]);
            buttons[i].setFont(font);
            buttons[i].addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent b) {
                    if(b.getSource() == buttons[0])
                        display.append("7");
                    if(b.getSource() == buttons[1])
                        display.append("8");
                    if(b.getSource() == buttons[2])
                        display.append("9");
                    if(b.getSource() == buttons[3]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[0] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[4])
                        display.append("4");
                    if(b.getSource() == buttons[5])
                        display.append("5");
                    if(b.getSource() == buttons[6])
                        display.append("6");
                    if(b.getSource() == buttons[7]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[1] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[8])
                        display.append("1");
                    if(b.getSource() == buttons[9])
                        display.append("2");
                    if(b.getSource() == buttons[10])
                        display.append("3");
                    if(b.getSource() == buttons[11]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[2] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[12])
                        display.append(".");
                    if(b.getSource() == buttons[13]) {
                        temporary[0] = Double.parseDouble(display.getText());
                        function[3] = true;
                        display.setText("");
                    }
                    if(b.getSource() == buttons[14])
                        clear();
                    if(b.getSource() == buttons[15])
                        getSqrt();
                    if(b.getSource() == buttons[16])
                        getResult();
                    if(b.getSource() == buttons[17])
                        display.append("0");
                }
            });
        }

        display.setFont(font);          
        display.setEditable(false);
        display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        display.setPreferredSize(dispDim);
        for(int i = 0; i < 14; i++)
            buttons[i].setPreferredSize(regDim);
        for(int i = 14; i < 18; i++) 
            buttons[i].setPreferredSize(rColDim);
        buttons[18].setPreferredSize(zeroButDim);

        row[0].add(display);
        this.add(row[0]);

        for(int i = 0; i < 4; i++)
            row[1].add(buttons[i]);
        row[1].add(buttons[14]);
        this.add(row[1]);

        for(int i = 4; i < 8; i++)
            row[2].add(buttons[i]);
        row[2].add(buttons[15]);
        this.add(row[2]);

        for(int i = 8; i < 12; i++)
            row[3].add(buttons[i]);
        row[3].add(buttons[16]);
        this.add(row[3]);

        row[4].add(buttons[18]);
        for(int i = 12; i < 14; i++)
            row[4].add(buttons[i]);
        row[4].add(buttons[17]);
        this.add(row[4]);

        this.setVisible(true);
    }

    public static void clear() {
        try {
            display.setText("");
            for(int i = 0; i < 4; i++)
                function[i] = false;
            for(int i = 0; i < 2; i++)
                temporary[i] = 0;
        } catch(NullPointerException e) {
        }
    }
    public static void getSqrt() {
        try {
            double value = Math.sqrt(Double.parseDouble(display.getText()));
            display.setText(Double.toString(value));
        } catch(NumberFormatException e) {
        }
    }
    public static void getPosNeg() {
        try {
            double value = Double.parseDouble(display.getText());
            if(value != 0) {
                value = value * (-1);
                display.setText(Double.toString(value));
            }
            else {
            }
        } catch(NumberFormatException e) {
        }
    }
    public static void getResult() {
        double result = 0;
        temporary[1] = Double.parseDouble(display.getText());
        String temp0 = Double.toString(temporary[0]);
        String temp1 = Double.toString(temporary[1]);
        try {
            if(temp0.contains("-")) {
                String[] temp00 = temp0.split("-", 2);
                temporary[0] = (Double.parseDouble(temp00[1]) *-1);
            }
            if(temp1.contains("-")) {
                String[] temp11 = temp1.split("-", 2);
                temporary[1] = (Double.parseDouble(temp11[1]) * -1);
            }
        } catch(ArrayIndexOutOfBoundsException e) {
        }
        try {
            if(function[2] == true)
                result = temporary[0] * temporary[1];
            else if(function[3] == true)
                result = temporary[0] / temporary[1];
            else if(function[0] == true)
                result = temporary[0] + temporary[1];
            else if(function[1] == true)
                result = temporary[0] - temporary[1];
            display.setText(Double.toString(result));
            for(int i = 0; i < 4; i++)
                function[i] = false;
        } catch(NumberFormatException e) {
        }
    }
    public final void setDesign() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception e) {
        }
    }
}

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