public class HeadlinesGUIApp extends JFrame
class HeadlinePanel extends JPanel
1: //HeadlinesGUIApp.java
2: //Based on a LeMay example.
3: //Shows 4 headlines scrolling from bottom to top.
5: import java.awt.Graphics;
6: import java.awt.Graphics2D;
7: import java.awt.Color;
8: import java.awt.Font;
9: import java.awt.GridLayout;
10: import javax.swing.JFrame;
11: import javax.swing.JPanel;
13: public class HeadlinesGUIApp extends JFrame
14: {
15: HeadlinePanel news = new HeadlinePanel();
17: public HeadlinesGUIApp()
18: {
19: super("Headlines");
20: setSize(420, 100);
21: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22: JPanel pane = new JPanel();
23: pane.setLayout(new GridLayout(1, 1, 15, 15));
24: pane.add(news);
25: setContentPane(pane);
26: show();
27: news.scroll();
28: }
30: public static void main(String[] arguments)
31: {
32: HeadlinesGUIApp head = new HeadlinesGUIApp();
33: }
34: }
37: class HeadlinePanel extends JPanel
38: {
39: String[] headlines =
40: {
41: "Grandmother of Eight Makes Hole in One",
42: "Police Begin Campaign to Run Down Jaywalkers",
43: "Dr. Ruth to Talk About Sex with Newspaper Editors",
44: "Enraged Cow Injures Farmer with Axe"
45: };
46: int y = 76;
48: void scroll()
49: {
50: while (true)
51: {
52: y = y - 1;
53: if (y < -75) y = 76;
54: repaint();
55: try
56: {
57: Thread.sleep(250);
58: }
59: catch (InterruptedException e)
60: {
61: }
62: }
63: }
64:
65: public void paintComponent(Graphics comp)
66: {
67: Graphics2D comp2D = (Graphics2D)comp;
68: Font fontType = new Font("monospaced", Font.BOLD, 14);
69: comp2D.setFont(fontType);
70: comp2D.setColor(Color.cyan);
71: //or ... comp2D.setColor(getBackground());
72: comp2D.fillRect(0, 0, getSize().width, getSize().height);
73: comp2D.setColor(Color.black);
74: for (int i=0; i<headlines.length; i++)
75: comp2D.drawString(headlines[i], 5, y + (20 * i));
76: }
78: }