Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why wont this program compile?

0 votes
asked Nov 14, 2019 by Semilore
import javax.swing.*;

import java.awt.*;

import javax.swing.border.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

class Main{

  // declairs the buttons.

  JButton zeroButton;

  JButton oneButton;

  JButton twoButton;

  JButton threeButton;

  JButton fourButton;

  JButton fiveButton;

  JButton sixButton;

  JButton sevenButton;

  JButton eightButton;

  JButton nineButton;

  JButton dashButton;

  JButton callButton;

  // holds users number that the input.

  String numberDialed = "";

  public static void main(String[] args)


  public main(){

    // creates new JFrame

    JFrame phone = new JFrame();

    // sets the JFrame's title, size, and makes the JFrame close




    // the first JPanel.

    JPanel panel1 = new JPanel();

    panel1.setLayout(new BoxLayout(phone, BoxLayout.Y_AXIS));

    Border border1 = BorderFactory.createEmptyBorder(10,10,10,10);

    // the second JPanel.

    JPanel panel2 = new JPanel();

    panel2.setLayout(new FlowLayout());

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

    // the third JPanel.

    JPanel panel3 = new JPanel();

    panel3.setLayout(new GridLayout(4,3,5,5));

    Border border2 = BorderFactory.createEmptyBorder(5,5,5,5);

    // creates buttons for panel3

    oneButton = new JButton("1");

    twoButton = new JButton("2");

    threeButton = new JButton("3");

    fourButton = new JButton("4");

    fiveButton = new JButton("5");

    sixButton = new JButton("6");

    sevenButton = new JButton("7");

    eightButton = new JButton("8");

    nineButton = new JButton("9");

    dashButton = new JButton("-");

    zeroButton = new JButton("0");

    // puts buttons on panel3














This is my code to make a phone dialer panel, why doesnt it compile?

1 Answer

0 votes
answered Jan 13, 2020 by zemiak (550 points)

  public static void main(String[] args)
  { // added missing left bracket
    new Main().main(); // added
  public void main() { // added void
    // ...
    // panel1.setLayout(new BoxLayout(phone, BoxLayout.Y_AXIS));   // wrong
       panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));  // ok
    // ...
    // at the end of method add:
    phone.setLocationRelativeTo(null); //center window
  } // end of main()
}   // end of class

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.