Swing and AWT are both user interface (UI) toolkits for building graphical user interfaces (GUIs) in Java, but there are some key differences between them.
- Look and feel: AWT uses the native UI widgets of the underlying platform, whereas Swing uses its own set of UI widgets that are implemented entirely in Java. This means that Swing applications have a consistent look and feel across different platforms, while AWT applications may look different on different platforms.
- Features: Swing provides a richer set of UI components and features than AWT, including support for more advanced UI widgets, such as JTree, JTable, JTabbedPane, and JToolBar. Swing also provides more advanced layout managers and event handling mechanisms than AWT.
- Performance: AWT is generally faster than Swing, especially for simple UIs, because it uses the native UI widgets of the underlying platform. However, Swing provides better performance for more complex UIs that require custom graphics or animation.
Here's an example program that uses Swing components to add two numbers entered by the user:
import java.awt.*;
import java.awt.event.*;
public class AddNumbers extends JFrame implements ActionListener {
JTextField num1, num2, result;
JButton addBtn;
public AddNumbers() {
// Set up the frame
setTitle("Add Numbers");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the UI components
num1 = new JTextField(10);
num2 = new JTextField(10);
result = new JTextField(10);
result.setEditable(false);
addBtn = new JButton("Add");
addBtn.addActionListener(this);
// Set up the layout
JPanel inputPanel = new JPanel(new GridLayout(2, 2));
inputPanel.add(new JLabel("Number 1:"));
inputPanel.add(num1);
inputPanel.add(new JLabel("Number 2:"));
inputPanel.add(num2);
JPanel outputPanel = new JPanel(new FlowLayout());
outputPanel.add(new JLabel("Result:"));
outputPanel.add(result);
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(addBtn);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(outputPanel, BorderLayout.CENTER);
mainPanel.add(btnPanel, BorderLayout.SOUTH);
setContentPane(mainPanel);
}
public void actionPerformed(ActionEvent e) {
// Get the numbers entered by the user
double n1 = Double.parseDouble(num1.getText());
double n2 = Double.parseDouble(num2.getText());
// Add the numbers and display the result
double sum = n1 + n2;
result.setText(Double.toString(sum));
}
public static void main(String[] args) {
AddNumbers frame = new AddNumbers();
frame.setVisible(true);
}
}
In this program, we create a JFrame and add three text fields and a button to it using Swing components. When the user clicks the "Add" button, the actionPerformed() method is called, which gets the numbers entered by the user, adds them together, and displays the result in the third text field. The program demonstrates the use of text fields, buttons, and event handling in Swing.
No comments:
Post a Comment
If you have any doubts, please let me know