Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class TypewriterJFrame extends JFrame implements ActionListener {
- /** Application: TypewriterJFrame.java by Chris Clarke, 07 September 2016.
- * Purpose: Example of text "typed" automatically in a JFrame TextArea.
- **/
- Panel userControls;
- TextArea ta1;
- Button btnSet;
- boolean paint=false;
- final String txt = "Entering the Dragon's lair,\nI watched as ..."; // or put own text here
- TypewriterJFrame() {
- setTitle("Typewriter by Chris Clarke");
- setLayout(new BorderLayout());
- userControls = new Panel();
- ta1=new TextArea("", 2, 35);
- userControls.add(ta1);
- btnSet=new Button("Press me!");
- btnSet.addActionListener(this);
- userControls.add(btnSet);
- add("North",userControls);
- }
- public static void main(String[] args) {
- TypewriterJFrame frame = new TypewriterJFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // Set to a reasonable size.
- frame.setSize(1024, 768);
- frame.setVisible(true);
- }
- public void paint(Graphics g) {
- if (!paint) {
- g.setColor(Color.WHITE);
- g.fillRect(0,0,1024,768);
- return;
- }
- String output="";
- for (int c=0; c<txt.length(); c++) {
- output += txt.charAt(c);
- ta1.setText(output);
- for (long num=0; num<50000000; num++); // try other values
- }
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnSet) {
- paint=true;
- repaint();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement