Java Reference
In-Depth Information
Display 18.21
Using
drawString
(part 1 of 2)
1
import
javax.swing.JFrame;
2
import
javax.swing.JPanel;
3
import
javax.swing.JButton;
4
import
java.awt.event.ActionListener;
5
import
java.awt.event.ActionEvent;
6
import
java.awt.BorderLayout;
7
import
java.awt.Graphics;
8
import
java.awt.Color;
9
import
java.awt.Font;
10
public class
DrawStringDemo
extends
JFrame
11
implements
ActionListener
12 {
13
public static final int
WIDTH = 350;
14
public static final int
HEIGHT = 200;
15
public static final int
X_START = 20;
16
public static final int
Y_START = 100;
17
public static final int
POINT_SIZE = 24;
18
private
String theText = "Push the button.";
19
private
Color penColor = Color.BLACK;
20
private
Font fontObject =
21
new
Font("SansSerif", Font.PLAIN, POINT_SIZE);
22
public static void
main(String[] args)
23 {
24 DrawStringDemo gui =
new
DrawStringDemo();
25 gui.setVisible(
true
);
26 }
27
public
DrawStringDemo()
28 {
29 setSize(WIDTH, HEIGHT);
30 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31 setTitle("drawString Demonstration");
32 getContentPane().setBackground(Color.WHITE);
33 setLayout(
new
BorderLayout());
34 JPanel buttonPanel =
new
JPanel();
35 buttonPanel.setBackground(Color.GRAY);
36 buttonPanel.setLayout(
new
BorderLayout());
37 JButton theButton =
new
JButton("The Button");
38 theButton.addActionListener(
this
);
39 buttonPanel.add(theButton, BorderLayout.CENTER);
40 add(buttonPanel, BorderLayout.SOUTH);
41 }