User4356
User4356

Reputation: 3

Jlabel cant display int

i am fairly new to java and i am trying to display multiplication table using JLabel. The user will input two integers. m will be the number of times the multiplication should show and n will be the times table. p = answer that should be displayed when m x n This is my code

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class TimesTable extends JFrame
{
     public TimesTable(int m, int n, int p)
     {
          setTitle("TimesTable");
          //Container contents = getContentsPane();
          //contents.setLayout(new GridLayout(0, 3, 20, 10));          

          //for loop to calculate the times table
           for(int num = 1; num >= m; m++)
           {
                 multiplyAnswer = numOfMultiply * multiplyBy;
                 Container contents = getContentsPane();
                 contents.setLayout(new GridLayout(0, 3, 20, 10)); 
                 contents.add(new JLabel(m));
                 contents.add(new JLabel("x"));
                 contents.add(new JLabel(n));
                 contents.add(new JLabel("="));
                 contents.add(new JLabel(p));
           }

          setDefaultCloseOperation(EXIT_ON_CLOSE);
          pack();
      }

      public static void main(String[] args)
      {
           int m = Integer.parseInt(args[0]);
           int n = Integer.parseInt(args[1]);

          TimesTable theTimesTable = new TimesTable(m, n, p);
          theTimesTable.setVisible(true);  
      }

but i keep getting an error. What should i do?

Upvotes: 0

Views: 2696

Answers (3)

Domi
Domi

Reputation: 24528

You need to convert your int to string because the label constructor that you are looking for wants a string. There are multiple ways of doing that. For example, you can use Integer.toString:

contents.add(new JLabel(Integer.toString(m)));

Alternatively, you can convert it implicitly by concatenating it with an empty string, as the other answers suggest, i.e.:

contents.add(new JLabel("" + m));

Upvotes: 2

subash
subash

Reputation: 3140

Try this..

             contents.add(new JLabel(m+""));
             contents.add(new JLabel("x"));
             contents.add(new JLabel(n+""));
             contents.add(new JLabel("="));
             contents.add(new JLabel(p+""));

Upvotes: 0

sanket
sanket

Reputation: 789

contents.add(new JLabel(""+m));

Upvotes: 0

Related Questions