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???