Write a java program to create a GUI that resembles a telephone keypad.

 

import java.awt.*;
import javax.swing.*;

public class Telephone {
 public static void main(String[] args) {
 JFrame frame = new JFrame();
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(new Dimension(250, 200));
 frame.setTitle("Telephone");

 frame.setLayout(new BorderLayout());

 // main phone buttons
 JPanel centerPanel = new JPanel(new GridLayout(4, 3));
 for (int i = 1; i <= 9; i++) {
 centerPanel.add(new JButton("" + i));
 }
 centerPanel.add(new JButton("*"));
 centerPanel.add(new JButton("0"));
 centerPanel.add(new JButton("#"));
frame.add(centerPanel, BorderLayout.CENTER);

// south status panel
 JPanel southPanel = new JPanel(new FlowLayout());
 southPanel.add(new JLabel("Number to dial: "));
 southPanel.add(new JTextField(10));
 frame.add(southPanel, BorderLayout.SOUTH);
 frame.setVisible(true);
 }
}

Output :


 

You can construct a JPanel object with no parameters, or you can specify the layout manager to use. Once you’ve constructed the panel, you can add components to it using its add method:


// creates a JPanel container with a FlowLayout
JPanel panel = new JPanel(new FlowLayout());
panel.add(new JButton("button 1"));
panel.add(new JButton("button 2"));

A common strategy is to use a BorderLayout for the frame, then add panels to some or all of its five regions. For example, the following code produces a window that looks like a telephone keypad, using a panel with a GridLayout in the center region of the frame and a second panel with a FlowLayout in the south region of the frame.


java gui projects,java gui programming,java gui framework,java gui swing,java gui application,java gui awt,java gui code,java gui development,java gui exercises,java gui exit button,java gui elements,gui java program,java gui hello world,java gui how to display text,java gui jframe

Post a Comment

0 Comments