Advertisement
thotfrnk

swingPhoneApp.java

Jun 14th, 2023 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. // school work
  2.  
  3. import java.awt.*;       //using awt's layouts
  4. import java.awt.event.*; //using awt's event classes and listener interfaces
  5. import javax.swing.*;    //using swing components and containers
  6.  
  7. //a swing application extends from javax.swing.JFrame
  8. public class SwingPhoneApp extends JFrame {
  9.     private JButton[] btnNumbers;  // array
  10.     private JButton btnCall;
  11.     private JButton btnClear;
  12.     private JTextField tfDisplay;
  13.     private String strPhoneNumber = "";
  14.     private boolean isCalling = false;
  15.  
  16.     public SwingPhoneApp() {
  17.         //get the content-pane of top-level container JFrame. components are added onto content pane
  18.         Container cp = getContentPane();
  19.         cp.setLayout(new BorderLayout());
  20.  
  21.         //set up the top display panel
  22.         JPanel pnlDisplay = new JPanel(new FlowLayout());
  23.         cp.add(pnlDisplay, BorderLayout.NORTH);
  24.  
  25.         //set up the display text field
  26.         tfDisplay = new JTextField(30);
  27.         tfDisplay.setEditable(false);
  28.         tfDisplay.setHorizontalAlignment(JTextField.RIGHT);
  29.         pnlDisplay.add(tfDisplay);
  30.  
  31.         //set up the center button panel
  32.         JPanel pnlButtons = new JPanel(new GridLayout(4, 3, 3, 3));
  33.         cp.add(pnlButtons, BorderLayout.CENTER);
  34.  
  35.         //set up a common listener for all number buttons
  36.         MyNumberButtonListener lis = new MyNumberButtonListener();
  37.  
  38.         //allocate the JButton array for number buttons
  39.         btnNumbers = new JButton[10];
  40.  
  41.         //allocate each of the item (JButton) of the array
  42.         for (int i = 0; i < btnNumbers.length; ++i) {
  43.             btnNumbers[i] = new JButton(i + "");
  44.             //add the common listener to each of the JButton
  45.             btnNumbers[i].addActionListener(lis);
  46.         }
  47.  
  48.         //add the JButtons in the right order
  49.         for (int i = 1; i <= 9; ++i) {
  50.             pnlButtons.add(btnNumbers[i]);
  51.         }
  52.  
  53.         btnClear = new JButton("CLEAR");
  54.         pnlButtons.add(btnClear);
  55.         btnClear.addActionListener(new ActionListener() {
  56.             @Override
  57.             public void actionPerformed(ActionEvent evt) {
  58.                 strPhoneNumber = "";
  59.                 tfDisplay.setText(strPhoneNumber);
  60.             }
  61.         });
  62.  
  63.         pnlButtons.add(btnNumbers[0]);
  64.  
  65.         btnCall = new JButton("CALL");
  66.         pnlButtons.add(btnCall);
  67.         btnCall.addActionListener(new ActionListener() {
  68.             @Override
  69.             public void actionPerformed(ActionEvent evt) {
  70.                 if (isCalling) {
  71.                     isCalling = false;
  72.                     btnCall.setText("CALL");
  73.                     //call(strPhoneNumber)
  74.                 } else {
  75.                     isCalling = true;
  76.                     btnCall.setText("HANG UP");
  77.                     //hangup()
  78.                 }
  79.             }
  80.         });
  81.  
  82.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  83.         //exit program if JFrame's close-window button clicked
  84.         setSize(400, 350);
  85.         setTitle("Swing Phone App");
  86.         setVisible(true);    //show it
  87.     }
  88.  
  89.     // Named inner listener class for the number buttons
  90.     private class MyNumberButtonListener implements ActionListener {
  91.         @Override
  92.         public void actionPerformed(ActionEvent evt) {
  93.             strPhoneNumber += evt.getActionCommand();
  94.             tfDisplay.setText(strPhoneNumber);
  95.         }
  96.     }
  97.  
  98.     public static void main(String[] args) {
  99.         //recommended to run the GUI construction in event Dispatching thread for thread-safe operations
  100.         SwingUtilities.invokeLater(new Runnable() {
  101.             @Override
  102.             public void run() {
  103.                 new SwingPhoneApp(); //Let the constructor does the job
  104.             }
  105.         });
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement