Hi all,
I have two programs.One is for scrolling text.Code is here
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MarqueeTest
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame implements ActionListener
{
private ActionListener listener;
private Timer t1;
public MyFrame()
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setSize(d.width,d.height);
setTitle("MARQUEE");
MyPanel panel = new MyPanel();
add(panel);
listener = this;
t1 = new Timer(50,listener);
t1.start();
}
public void actionPerformed(ActionEvent event)
{
repaint();
}
}
class MyPanel extends JPanel
{
private int x,y;
private Dimension d ;
public MyPanel()
{
d= Toolkit.getDefaultToolkit().getScreenSize();
x = d.width - 100;
y = 25;
}
public void paintComponent(Graphics g)
{
x -= 5;
if ( x < -250)
x = d.width;
Graphics2D g2 = (Graphics2D) g;
Font font = new Font("Comic Sans MS",Font.BOLD,25);
g2.setFont(font);
g2.setPaint(Color.BLUE);
g2.drawString("Go To Web Page?",x,y);
}
}
Another one is for opening web page when I click text.Code is here
import java.awt.Color;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URI;
import javax.swing.*;
public class Link {
public static void main(String[] args) throws URISyntaxException {
final URI uri = new URI("http://java.sun.com");
JFrame frame = new JFrame("Links");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 400);
Container container = frame.getContentPane();
container.setLayout(new GridBagLayout());
// JButton
JButton button = new JButton();
String html = "<HTML><marquee>Click the <FONT color=\"#000099\"><U>link</U></FONT> to go to the Java website.</marquee></HTML>";
System.out.println("String Text = " +html);
button.setText(html);
button.setHorizontalAlignment(SwingConstants.LEFT);
button.setBorderPainted(false);
button.setOpaque(false);
button.setBackground(Color.WHITE);
button.setToolTipText(uri.toString());
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open(uri);
}
});
container.add(button);
frame.setVisible(true);
}
private static void open(java.net.URI uri) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(uri);
} catch (IOException e) {
// TODO: error handling
}
} else {
// TODO: error handling
}
}
}
I want to open web page when I click scrolling text.At the MarqueeTest, use paintComponent(Graphics g){ g.drawString("Go To Web Page?",x,y) }.To get link at the scrolling text, I must place Component at the "Go To Web Page?" String.To insert Component ,how will I do?And what fun: can I use at paintComponent(Graphics g) to appear scrolled link text.Pls help me.I do need your help.Thank in advance.:?: