1.11M Members

I need help making a Phone input

 
0
 

I have to make a program that outputs something similar to the appearance of a phone. With intractable buttons, (0 - 9) a T button that outputs "Calling" and an E button that outputs "End-Call"

Now i have that much done

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Font;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.EventQueue;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

public class TEST extends JPanel {

    private static final int WINDOW_WIDTH = 300;
    private static final int WINDOW_HEIGHT = 450;

    private Timer tickTimer;

    private JButton Button1;
    private JButton Button2;
    private JButton Button3;
    private JButton Button4;
    private JButton Button5;
    private JButton Button6;
    private JButton Button7;
    private JButton Button8;
    private JButton Button9;
    private JButton ButtonT;
    private JButton Button0;
    private JButton ButtonE;
    private JLabel msgLabel;
    private Font theFont = new Font("verdana", Font.BOLD, 18);

    private int clicked;
    private int holder = 0;
    private int Counter = 1;

    public TEST() {
        super( new GridLayout(2, 2, 5, 5) );
        setPreferredSize( new Dimension( WINDOW_WIDTH, WINDOW_HEIGHT ) );

        Button1 = new JButton( "1" );
        Button1.setFont( theFont );

        Button2 = new JButton( "2" );
        Button2.setFont( theFont );

        Button3 = new JButton( "3" );
        Button3.setFont( theFont );

        Button4 = new JButton( "4" );
        Button4.setFont( theFont );

        Button5 = new JButton( "5" );
        Button5.setFont( theFont );

        Button6 = new JButton( "6" );
        Button6.setFont( theFont );

        Button7 = new JButton( "7" );
        Button7.setFont( theFont );

        Button8 = new JButton( "8" );
        Button8.setFont( theFont );

        Button9 = new JButton( "9" );
        Button9.setFont( theFont );

        ButtonT = new JButton( "T" );
        ButtonT.setFont( theFont );

        Button0 = new JButton( "0" );
        Button0.setFont( theFont );

        ButtonE = new JButton( "E" );
        ButtonE.setFont( theFont );

        JPanel buttons = new JPanel( new GridLayout(4, 3) );
        buttons.add( Button1 );
        buttons.add( Button2 );
        buttons.add( Button3 );
        buttons.add( Button4 );
        buttons.add( Button5 );
        buttons.add( Button6 );
        buttons.add( Button7 );
        buttons.add( Button8 );
        buttons.add( Button9 );
        buttons.add( ButtonT );
        buttons.add( Button0 );
        buttons.add( ButtonE );
        add(  buttons );

        msgLabel = new JLabel( "0" );
        msgLabel.setHorizontalAlignment( SwingConstants.CENTER );
        msgLabel.setFont( theFont );
        add( msgLabel );

        Button1.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 1;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button2.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 2;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button3.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 3;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button4.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 4;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button5.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 5;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button6.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 6;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button7.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 7;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button8.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 8;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button9.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 9;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        ButtonT.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 0;
                msgLabel.setText( String.valueOf( "Calling" ) );
            }
        });

        Button0.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 0;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });


        ButtonE.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 0;
                msgLabel.setText( String.valueOf( "End Call" ) );
            }
        });
    }
    public static void runApplication( final JPanel app ) {
        EventQueue.invokeLater(
            new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setSize( app.getPreferredSize() );
                    frame.setTitle( app.getClass().getName() );
                    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                    frame.add( app );
                    frame.setVisible( true );
                }
            } );
    }

    public static void main( String[] args ) {
        TEST application = new TEST();
        runApplication( application );
    }
}

Not the prettiest code i admit, but what i'm having trouble with is this
- I need the msg JLabel to appear on top of the buttons not below them
- I need to make it capable of holding more than one number at a time, anytime you push one now the thing is just reset.

- Any Suggestions???

 
0
 

I need to make it capable of holding more than one number at a time, anytime you push one now the thing is just reset.

What is the "it"? Is that the JLabel.
What String do you pass to the setLabel() method? Is that what you want to change?

 
0
 

I have to make a program that outputs something similar to the appearance of a phone. With intractable buttons, (0 - 9) a T button that outputs "Calling" and an E button that outputs "End-Call"

Now i have that much done

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Font;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.EventQueue;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

public class TEST extends JPanel {

    private static final int WINDOW_WIDTH = 300;
    private static final int WINDOW_HEIGHT = 450;

    private Timer tickTimer;

    private JButton Button1;
    private JButton Button2;
    private JButton Button3;
    private JButton Button4;
    private JButton Button5;
    private JButton Button6;
    private JButton Button7;
    private JButton Button8;
    private JButton Button9;
    private JButton ButtonT;
    private JButton Button0;
    private JButton ButtonE;
    private JLabel msgLabel;
    private Font theFont = new Font("verdana", Font.BOLD, 18);

    private int clicked;
    private int holder = 0;
    private int Counter = 1;

    public TEST() {
        super( new GridLayout(2, 2, 5, 5) );
        setPreferredSize( new Dimension( WINDOW_WIDTH, WINDOW_HEIGHT ) );

        Button1 = new JButton( "1" );
        Button1.setFont( theFont );

        Button2 = new JButton( "2" );
        Button2.setFont( theFont );

        Button3 = new JButton( "3" );
        Button3.setFont( theFont );

        Button4 = new JButton( "4" );
        Button4.setFont( theFont );

        Button5 = new JButton( "5" );
        Button5.setFont( theFont );

        Button6 = new JButton( "6" );
        Button6.setFont( theFont );

        Button7 = new JButton( "7" );
        Button7.setFont( theFont );

        Button8 = new JButton( "8" );
        Button8.setFont( theFont );

        Button9 = new JButton( "9" );
        Button9.setFont( theFont );

        ButtonT = new JButton( "T" );
        ButtonT.setFont( theFont );

        Button0 = new JButton( "0" );
        Button0.setFont( theFont );

        ButtonE = new JButton( "E" );
        ButtonE.setFont( theFont );

        JPanel buttons = new JPanel( new GridLayout(4, 3) );
        buttons.add( Button1 );
        buttons.add( Button2 );
        buttons.add( Button3 );
        buttons.add( Button4 );
        buttons.add( Button5 );
        buttons.add( Button6 );
        buttons.add( Button7 );
        buttons.add( Button8 );
        buttons.add( Button9 );
        buttons.add( ButtonT );
        buttons.add( Button0 );
        buttons.add( ButtonE );
        add(  buttons );

        msgLabel = new JLabel( "0" );
        msgLabel.setHorizontalAlignment( SwingConstants.CENTER );
        msgLabel.setFont( theFont );
        add( msgLabel );

        Button1.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 1;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button2.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 2;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button3.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 3;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button4.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 4;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button5.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 5;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button6.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 6;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button7.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 7;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button8.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 8;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        Button9.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 9;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });

        ButtonT.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 0;
                msgLabel.setText( String.valueOf( "Calling" ) );
            }
        });

        Button0.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 0;
                msgLabel.setText( String.valueOf( clicked ) );
            }
        });


        ButtonE.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
		clicked = 0;
                msgLabel.setText( String.valueOf( "End Call" ) );
            }
        });
    }
    public static void runApplication( final JPanel app ) {
        EventQueue.invokeLater(
            new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setSize( app.getPreferredSize() );
                    frame.setTitle( app.getClass().getName() );
                    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                    frame.add( app );
                    frame.setVisible( true );
                }
            } );
    }

    public static void main( String[] args ) {
        TEST application = new TEST();
        runApplication( application );
    }
}

Not the prettiest code i admit, but what i'm having trouble with is this
- I need the msg JLabel to appear on top of the buttons not below them
- I need to make it capable of holding more than one number at a time, anytime you push one now the thing is just reset.

- Any Suggestions???

To make the number not disappear as you type try this for each:

String prevText=msgLabel.getText();
                msgLabel.setText( prevText+String.valueOf( clicked ) );
 
0
 

What is the "it"? Is that the JLabel.
What String do you pass to the setLabel() method? Is that what you want to change?

Yeah, the JLabel is what shows the numbers, which in this case appear at the bottom of the frame.
I need it too appear at the Top of the frame, above the buttons.
I also need the JLabel to output the buttons i press,
so that as i press 5 than 1 than 2 it would show 512.
right now if i press 5, 1, and 2 it covers over the previous number, so the JLabel would show just 2.

 
0
 

Think of it this way: you have a variable with a value (5) and you want to add 7 to it:
var = 5;
would you do this:
var = 7; // set it to the value to be added
or this
var = var + 7; // add the 7 to the current value

 
0
 

Wouldn't that add the values Mathematically,
Then hitting 5 and 7 would print out 13.

 
0
 

Wouldn't that add the values Mathematically,
Then hitting 5 and 7 would print out 13.

yes but think about it theoretically, you would use the old text of the label and add it to the new text.. have you tried what i suggested
see here also:http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#getText() and http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setText(java.lang.String)

 
0
 

Change the variable type from int to String.
Then what would be the results?

 
0
 

Ah! I can't believe i never thought of changing it to a String.
I am however now getting "null" before my string of numbers when i push a button.
Also, how can i reset a string after i hit "E" or "End Call" ?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: