0% found this document useful (0 votes)
95 views

Java AWT Dialog

This document contains examples of using Java AWT for dialogs, toolkits, and action listeners. It shows how to create dialog boxes, get screen resolution, play beep sounds, change title bar icons, and add an action listener to a button to update text when clicked.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Java AWT Dialog

This document contains examples of using Java AWT for dialogs, toolkits, and action listeners. It shows how to create dialog boxes, get screen resolution, play beep sounds, change title bar icons, and add an action listener to a button to update text when clicked.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java AWT Dialog

1. import java.awt.*;
2. public class PanelExample {
3. PanelExample()
4. {
5. Frame f= new Frame("Panel Example");
6. Panel panel=new Panel();
7. panel.setBounds(40,80,200,200);
8. panel.setBackground(Color.gray);
9. Button b1=new Button("Button 1");
10. b1.setBounds(50,100,80,30);
11. b1.setBackground(Color.yellow);
12. Button b2=new Button("Button 2");
13. b2.setBounds(100,100,80,30);
14. b2.setBackground(Color.green);
15. panel.add(b1); panel.add(b2);
16. f.add(panel);
17. f.setSize(400,400);
18. f.setLayout(null);
19. f.setVisible(true);
20. }
21. public static void main(String args[])
22. {
23. new PanelExample();
24. }
25. }

Java AWT Dialog Example

1. import java.awt.*;
2. import java.awt.event.*;
3. public class DialogExample {
4. private static Dialog d;
5. DialogExample() {
6. Frame f= new Frame();
7. d = new Dialog(f , "Dialog Example", true);
8. d.setLayout( new FlowLayout() );
9. Button b = new Button ("OK");
10. b.addActionListener ( new ActionListener()
11. {
12. public void actionPerformed( ActionEvent e )
13. {
14. DialogExample.d.setVisible(false);
15. }
16. });
17. d.add( new Label ("Click button to continue."));
18. d.add(b);
19. d.setSize(300,300);
20. d.setVisible(true);
21. }
22. public static void main(String args[])
23. {
24. new DialogExample();
25. }
26. }

Java AWT Toolkit Example

1. import java.awt.*;
2. public class ToolkitExample {
3. public static void main(String[] args) {
4. Toolkit t = Toolkit.getDefaultToolkit();
5. System.out.println("Screen resolution = " + t.getScreenResolution());
6. Dimension d = t.getScreenSize();
7. System.out.println("Screen width = " + d.width);
8. System.out.println("Screen height = " + d.height);
9. }
10. }

Java AWT Toolkit Example: beep()


1. import java.awt.event.*;
2. public class ToolkitExample {
3. public static void main(String[] args) {
4. Frame f=new Frame("ToolkitExample");
5. Button b=new Button("beep");
6. b.setBounds(50,100,60,30);
7. f.add(b);
8. f.setSize(300,300);
9. f.setLayout(null);
10. f.setVisible(true);
11. b.addActionListener(new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. Toolkit.getDefaultToolkit().beep();
14. }
15. });
16. }
17. }

Java AWT Toolkit Example: Change TitleBar Icon


1. import java.awt.*;
2. class ToolkitExample {
3. ToolkitExample(){
4. Frame f=new Frame();
5. Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
6. f.setIconImage(icon);
7. f.setLayout(null);
8. f.setSize(400,400);
9. f.setVisible(true);
10. }
11. public static void main(String args[]){
12. new ToolkitExample();
13. }
14. }

Java ActionListener Example: On Button click


1. import java.awt.*;
2. import java.awt.event.*;
3. //1st step
4. public class ActionListenerExample implements ActionListener{
5. public static void main(String[] args) {
6. Frame f=new Frame("ActionListener Example");
7. final TextField tf=new TextField();
8. tf.setBounds(50,50, 150,20);
9. Button b=new Button("Click Here");
10. b.setBounds(50,100,60,30);
11. //2nd step
12. b.addActionListener(this);
13. f.add(b);f.add(tf);
14. f.setSize(400,400);
15. f.setLayout(null);
16. f.setVisible(true);
17. }
18. //3rd step
19. public void actionPerformed(ActionEvent e){
20. tf.setText("Welcome to Javatpoint.");
21. }
22. }

1. import java.awt.*;
2. import java.awt.event.*;
3. public class ActionListenerExample {
4. public static void main(String[] args) {
5. Frame f=new Frame("ActionListener Example");
6. final TextField tf=new TextField();
7. tf.setBounds(50,50, 150,20);
8. Button b=new Button("Click Here");
9. b.setBounds(50,100,60,30);
10.
11. b.addActionListener(new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. tf.setText("Welcome to Javatpoint.");
14. }
15. });
16. f.add(b);f.add(tf);
17. f.setSize(400,400);
18. f.setLayout(null);
19. f.setVisible(true);
20. }
21. }

You might also like