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

    phone.setTitle("Dialer");

    phone.setSize(200,250);

    phone.setDefaultCloseOperation(JFrame.EXIT_ON_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

    panel3.add(oneButton);

    panel3.add(twoButton);

    panel3.add(threeButton);

    panel3.add(fourButton);

    panel3.add(fiveButton);

    panel3.add(sixButton);

    panel3.add(sevenButton);

    panel3.add(eightButton);

    panel3.add(nineButton);

    panel3.add(dashButton);

    panel3.add(zeroButton);

  }

}

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

1 Answer

0 votes
answered Jan 13 by zemiak (430 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:
    panel1.setBorder(border1);
    panel2.add(myLabel);
    panel3.setBorder(border2);
    
    panel1.add(panel2);    
    panel1.add(panel3);  
    
    phone.add(panel1);
    phone.setLocationRelativeTo(null); //center window
    phone.setVisible(true);
  } // 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.
...