0

sorry for the poorly worded question. I was working on a java swing project to recreate a phone dialer but for some reason, I can't figure out why 'Buttons(digits of phonenumber) are not being displayed on the JFrame. When I run the program, it displays JFrame with a red background but without the numbers or the dial buttons. If someone can explain it at a beginner level, that would be appreciated.

    enter code hereimport javax.swing.*;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class phoneDialer implements ActionListener {

    JButton zeroButton;
    JButton oneButton;
    JButton twoButton;
    JButton threeButton;
    JButton fourButton;
    JButton fiveButton;
    JButton sixButton;
    JButton sevenButton;
    JButton eightButton;
    JButton nineButton;
    JButton dashButton;
    JButton dialButton;
    String DigitHolder = ("");

    String input;
    JFrame myFrame;
    JPanel myPanel;
    JPanel digitPanel;
    JPanel buttonPanel;
    JLabel myLabel;
    JTextField display;

    GridLayout myGridLayout;

    public static void main(String[] args) {
        new phoneDialer();

    }

    public phoneDialer() {
        myFrame = new JFrame();

        myPanel = new JPanel();

        digitPanel = new JPanel();

        buttonPanel = new JPanel();

        myFrame.setContentPane(myPanel);

        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

        myPanel.setLayout(new BorderLayout(5, 10));

        myFrame.setLayout(new BorderLayout());

        myLabel = new JLabel("phoneDialer");

        display = new JTextField(20);
        //  topPanel.setLayout(new FlowLayout());

        myLabel = new JLabel("Enter the number to dial");

        myPanel.add(myLabel);

        myGridLayout = new GridLayout(4, 3, 5, 5);


        oneButton = new JButton("1");

        oneButton.addActionListener(this);

        twoButton = new JButton("2");

        twoButton.addActionListener(this);

        threeButton = new JButton("3");

        threeButton.addActionListener(this);

        fourButton = new JButton("4");

        fourButton.addActionListener(this);

        fiveButton = new JButton("5");

        fiveButton.addActionListener(this);

        sixButton = new JButton("6");

        sixButton.addActionListener(this);

        sevenButton = new JButton("7");

        sevenButton.addActionListener(this);

        eightButton = new JButton("8");

        eightButton.addActionListener(this);

        nineButton = new JButton("9");

        nineButton.addActionListener(this);

        zeroButton = new JButton("0");

        zeroButton.addActionListener(this);

        dashButton = new JButton("-");

        dashButton.addActionListener(this);

        dialButton = new JButton("Dial");

        dialButton.addActionListener(this);

        myFrame.setLayout(new BorderLayout());
        // myFrame.add(myPanel,BorderLayout.NORTH);
        myFrame.add(buttonPanel, BorderLayout.CENTER);
        myFrame.add(digitPanel, BorderLayout.SOUTH);

        myPanel.setLayout(new BorderLayout());
        myPanel.add(myLabel, BorderLayout.NORTH);
        myPanel.add(display, BorderLayout.CENTER);
        myPanel.setBackground(Color.RED);



        buttonPanel.setLayout(new FlowLayout());

        //          buttonPanel.add(redialButton);
        //          sendButton.addActionListener(new SendButtonListener());
        //          clearButton.addActionListener(new ClearButtonListener());
        //          endButton.addActionListener(new EndButtonListener());
        //          redialButton.addActionListener(new RedialButtonListener());
        buttonPanel.setBackground(Color.BLUE);

        digitPanel.setLayout(new GridLayout(4, 3));
        digitPanel.add(oneButton);
        digitPanel.add(twoButton);
        digitPanel.add(threeButton);
        digitPanel.add(fourButton);
        digitPanel.add(fiveButton);
        digitPanel.add(sixButton);
        digitPanel.add(sevenButton);
        digitPanel.add(eightButton);
        digitPanel.add(nineButton);
        digitPanel.add(zeroButton);
        digitPanel.add(dashButton);
        //  myFrame.add(myPanel, BorderLayout.CENTER);

        // south status panel



        myFrame.setTitle("Phone Dilaer");
        myFrame.setSize(200, 250);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
        myPanel.add(digitPanel);
        myPanel.add(buttonPanel);

    }

    public void actionPerformed(ActionEvent event) {
        input = display.getText();

        if (event.getSource() == oneButton) {
            display.setText("1");
        } else if (event.getSource() == twoButton) {
            display.setText("2");
        } else if (event.getSource() == threeButton) {
            display.setText("3");
        } else if (event.getSource() == fourButton) {
            display.setText("4");
        } else if (event.getSource() == fiveButton) {
            display.setText("5");
        } else if (event.getSource() == sixButton) {
            display.setText("6");
        } else if (event.getSource() == sevenButton) {
            display.setText("7");
        } else if (event.getSource() == eightButton) {
            display.setText("8");
        } else if (event.getSource() == nineButton) {
            display.setText("9");
        } else if (event.getSource() == zeroButton) {
            display.setText("0");
        } else if (event.getSource() == dashButton) {
            display.setText("*");
        }


    }



}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 3
    1) Class names SHOULD start with an upper case character. 2) Why are you trying to write your entire application all at once? Coding should be done one step at a time. So start by displaying one panel or one button. If you have a problem it is easier to debug when you only have a couple of line of code. Why are you writing ActionListener code, if you can't even display the components??? – camickr Nov 15 '20 at 02:31
  • 1
    There is too much code for us to look at in detail. One comment is that all components should be added to the frame BEFORE using setVisible(true). If you need more help, then simplify the code an post a proper [mre] demonstrating the problem. See: https://stackoverflow.com/a/33739732/131872 for an example of an "MRE" that is similar to what you want. – camickr Nov 15 '20 at 02:32
  • Where do you add digitPanel to its parent panel? – NomadMaker Nov 15 '20 at 04:53

1 Answers1

1

the problem is with the elements which you added to the layout and then LayoutMangers:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class phoneDialer implements ActionListener {

    JButton zeroButton;
    JButton oneButton;
    JButton twoButton;
    JButton threeButton;
    JButton fourButton;
    JButton fiveButton;
    JButton sixButton;
    JButton sevenButton;
    JButton eightButton;
    JButton nineButton;
    JButton dashButton;
    JButton dialButton;
    String DigitHolder = ("");

    String input;
    JFrame myFrame;
    JPanel myPanel;
    JPanel digitPanel;
    JPanel buttonPanel;
    JLabel myLabel;
    JTextField display;

    GridLayout myGridLayout;

    public static void main(String[] args) {
        new phoneDialer();

    }

    public phoneDialer() {
        myFrame = new JFrame();

        myPanel = new JPanel();

        digitPanel = new JPanel();

        buttonPanel = new JPanel();

        myFrame.setContentPane(myPanel);

        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

//      myPanel.setLayout(new BorderLayout(5, 10));
        
//      myFrame.setLayout(new BorderLayout());

        myLabel = new JLabel("phoneDialer");

        display = new JTextField(20);
        // topPanel.setLayout(new FlowLayout());

        myLabel = new JLabel("Enter the number to dial");

        myPanel.add(myLabel);

        myGridLayout = new GridLayout(4, 3, 5, 5);

        oneButton = new JButton("1");

        oneButton.addActionListener(this);

        twoButton = new JButton("2");

        twoButton.addActionListener(this);

        threeButton = new JButton("3");

        threeButton.addActionListener(this);

        fourButton = new JButton("4");

        fourButton.addActionListener(this);

        fiveButton = new JButton("5");

        fiveButton.addActionListener(this);

        sixButton = new JButton("6");

        sixButton.addActionListener(this);

        sevenButton = new JButton("7");

        sevenButton.addActionListener(this);

        eightButton = new JButton("8");

        eightButton.addActionListener(this);

        nineButton = new JButton("9");

        nineButton.addActionListener(this);

        zeroButton = new JButton("0");

        zeroButton.addActionListener(this);

        dashButton = new JButton("-");

        dashButton.addActionListener(this);

        dialButton = new JButton("Dial");

        dialButton.addActionListener(this);

//      myFrame.setLayout(new BorderLayout());
        // myFrame.add(myPanel,BorderLayout.NORTH);
//      myFrame.add(buttonPanel, BorderLayout.CENTER);
//      myFrame.add(digitPanel, BorderLayout.SOUTH);

//      myPanel.setLayout(new BorderLayout());
        myPanel.add(myLabel, BorderLayout.NORTH);
        myPanel.add(display, BorderLayout.CENTER);
        myPanel.setBackground(Color.RED);

        buttonPanel.setLayout(new FlowLayout());

        // buttonPanel.add(redialButton);
        // sendButton.addActionListener(new SendButtonListener());
        // clearButton.addActionListener(new ClearButtonListener());
        // endButton.addActionListener(new EndButtonListener());
        // redialButton.addActionListener(new RedialButtonListener());
        buttonPanel.setBackground(Color.BLUE);

        digitPanel.setLayout(new GridLayout(4, 3));
        digitPanel.add(oneButton);
        digitPanel.add(twoButton);
        digitPanel.add(threeButton);
        digitPanel.add(fourButton);
        digitPanel.add(fiveButton);
        digitPanel.add(sixButton);
        digitPanel.add(sevenButton);
        digitPanel.add(eightButton);
        digitPanel.add(nineButton);
        digitPanel.add(zeroButton);
        digitPanel.add(dashButton);
        // myFrame.add(myPanel, BorderLayout.CENTER);

        // south status panel

        myFrame.setTitle("Phone Dilaer");
        myFrame.setSize(200, 250);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
        myPanel.add(digitPanel);
        myPanel.add(buttonPanel);

    }

    public void actionPerformed(ActionEvent event) {
        input = display.getText();

        if (event.getSource() == oneButton) {
            display.setText("1");
        } else if (event.getSource() == twoButton) {
            display.setText("2");
        } else if (event.getSource() == threeButton) {
            display.setText("3");
        } else if (event.getSource() == fourButton) {
            display.setText("4");
        } else if (event.getSource() == fiveButton) {
            display.setText("5");
        } else if (event.getSource() == sixButton) {
            display.setText("6");
        } else if (event.getSource() == sevenButton) {
            display.setText("7");
        } else if (event.getSource() == eightButton) {
            display.setText("8");
        } else if (event.getSource() == nineButton) {
            display.setText("9");
        } else if (event.getSource() == zeroButton) {
            display.setText("0");
        } else if (event.getSource() == dashButton) {
            display.setText("*");
        }

    }
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36