Joreto

FavouriteSite

Dec 29th, 2023
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | Writing | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.swing.*;
  5. import java.net.*;
  6.  
  7. public class LinkRotator extends JFrame
  8.         implements Runnable, ActionListener {
  9.  
  10.     String[] pageTitle = new String[6];
  11.     URI[] pageLink = new URI[6];
  12.     int current = 0;
  13.     Thread runner;
  14.     JLabel siteLabel = new JLabel();
  15.  
  16.     public LinkRotator() {
  17.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         setSize(450, 450);
  19.         FlowLayout flo = new FlowLayout();
  20.         setLayout(flo);
  21.         add(siteLabel);
  22.         pageTitle = new String[] {
  23.                 "Спортни новини",
  24.                 "Виж Клюките",
  25.                 "Слушай музика",
  26.                 "Учи програмиране",
  27.                 "Импортвай кода си ",
  28.                 "ЦСКА"
  29.         };
  30.         pageLink[0] = getURI("https://sportal.bg/");
  31.         pageLink[1] = getURI("https://www.facebook.com/");
  32.         pageLink[2] = getURI("https://www.youtube.com/");
  33.         pageLink[3] = getURI("https://softuni.bg/");
  34.         pageLink[4] = getURI("https://github.com/");
  35.         pageLink[5] = getURI("https://cska.bg/");
  36.         Button visitButton = new Button("Visit Site");
  37.         visitButton.addActionListener(this);
  38.         add(visitButton);
  39.         setVisible(true);
  40.         start();
  41.     }
  42.  
  43.     private URI getURI(String urlText) {
  44.         URI pageURI = null;
  45.         try {
  46.             pageURI = new URI(urlText);
  47.         } catch (URISyntaxException ex) {
  48.  
  49.         }
  50.         return pageURI;
  51.     }
  52.  
  53.     public void start() {
  54.         if (runner == null) {
  55.             runner = new Thread(this);
  56.             runner.start();
  57.         }
  58.     }
  59.  
  60.     public void run() {
  61.         Thread thisThread = Thread.currentThread();
  62.         while (runner == thisThread) {
  63.             current++;
  64.             if (current > 5) {
  65.                 current = 0;
  66.             }
  67.             siteLabel.setText(pageTitle[current]);
  68.             repaint();
  69.             try {
  70.                 Thread.sleep(3000);
  71.             } catch (InterruptedException exc) {
  72.                
  73.             }
  74.         }
  75.     }
  76.  
  77.     public void actionPerformed(ActionEvent event) {
  78.         Desktop desktop = Desktop.getDesktop();
  79.         if (pageLink[current] != null) {
  80.             try {
  81.                 desktop.browse(pageLink[current]);
  82.                 runner = null;
  83.                 System.exit(0);
  84.             } catch (IOException exc) {
  85.                
  86.             }
  87.         }
  88.     }
  89.  
  90.     public static void main(String[] arguments) {
  91.         new LinkRotator();
  92.     }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment