Hi all.
pls help me.I repair a program for my need.At that time Text are not appear.Original progarm is here.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MarqueeFrame extends JFrame implements ActionListener {
private Timer timer;
private MarqueePanel marqueePanel;
// Timer Event Handler
public void actionPerformed(ActionEvent e) {
// move the Label in the MarqueePanel
marqueePanel.moveLabel();
}
public MarqueeFrame() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(d.width,d.height);
setTitle("Marquee");
setResizable(false);
timer = new Timer(150, this);
marqueePanel = new MarqueePanel(timer);
add(marqueePanel);
timer.start();
}
public static void main(String[] args) {
new MarqueeFrame().setVisible(true);
}
}
class MarqueePanel extends JPanel {
private int xLoc, yLoc;
private Dimension dimension;
private JLabel googleLabel;
private JLabel yahooLabel;
private Timer timer;
public MarqueePanel(final Timer timer) {
this.timer = timer;
setLayout(null);
dimension = Toolkit.getDefaultToolkit().getScreenSize();
xLoc = dimension.width;
yLoc = 0;
googleLabel = new JLabel("Google");
googleLabel.setForeground(Color.BLUE);
yahooLabel = new JLabel("Yahoo");
yahooLabel.setForeground(Color.RED);
googleLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
try {
Runtime.getRuntime().exec("cmd.exe /c start http://www.google.com");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
public void mouseEntered(MouseEvent e) {
timer.stop();
}
public void mouseExited(MouseEvent e) {
timer.start();
}
});
yahooLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
try {
Runtime.getRuntime().exec("cmd.exe /c start http://www.yahoo.com");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
public void mouseEntered(MouseEvent e) {
timer.stop();
}
public void mouseExited(MouseEvent e) {
timer.start();
}
});
add(googleLabel);
add(yahooLabel);
moveLabel();
}
public void moveLabel() {
Rectangle r = new Rectangle();
r.x = xLoc;
r.y = yLoc;
Dimension size = googleLabel.getPreferredSize();
r.width = size.width;
r.height = size.height;
googleLabel.setBounds(r);
// Append the Width of First Label to avoid overlapping
r.x += r.width + 5;
size = yahooLabel.getPreferredSize();
r.width = size.width;
r.height = size.height;
yahooLabel.setBounds(r);
xLoc -= 5;
if (xLoc < 0 )
xLoc = dimension.width;
}
}
I repaired this as looping style.At that time.marquee text is not appear.Pls help me.Where I wrong?Thank in advance.Here is repaired program
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MarqueeFrame extends JFrame implements ActionListener {
private Timer timer;
private MarqueePanel marqueePanel;
// Timer Event Handler
public void actionPerformed(ActionEvent e) {
// move the Label in the MarqueePanel
marqueePanel.moveLabel();
}
public MarqueeFrame() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(d.width,d.height);
setTitle("Marquee");
setResizable(true);
timer = new Timer(150, this);
marqueePanel = new MarqueePanel(timer);
add(marqueePanel);
timer.start();
}
public static void main(String[] args) {
new MarqueeFrame().setVisible(true);
}
}
class MarqueePanel extends JPanel {
private int xLoc, yLoc;
private Dimension dimension;
private JLabel googleLabel;
private JLabel yahooLabel;
private Timer timer;
private JLabel stringlbl;
String stringlabel[][];
public MarqueePanel(final Timer timer) {
this.timer = timer;
setLayout(null);
dimension = Toolkit.getDefaultToolkit().getScreenSize();
xLoc = dimension.width;
yLoc = 0;
stringlabel =new String[2][2];
stringlabel[0][0] = "Google";
stringlabel[0][1] = "http://www.google.com";
stringlabel[1][0] = "Yahoo";
stringlabel[1][1] = "http://www.yahoo.com";
for(int i=0;i<stringlabel.length;i++)
{
stringlbl = new JLabel(stringlabel[i][0]);
stringlbl.setForeground(Color.BLUE);
final String link = stringlabel[i][1];
stringlbl.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
try {
Runtime.getRuntime().exec("cmd.exe /c start "+link);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
public void mouseEntered(MouseEvent e) {
timer.stop();
}
public void mouseExited(MouseEvent e) {
timer.start();
}
});
add(stringlbl);
moveLabel();
}
/*googleLabel = new JLabel("Google");
googleLabel.setForeground(Color.BLUE);
yahooLabel = new JLabel("Yahoo");
yahooLabel.setForeground(Color.RED);
googleLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
try {
Runtime.getRuntime().exec("cmd.exe /c start http://www.google.com");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
public void mouseEntered(MouseEvent e) {
timer.stop();
}
public void mouseExited(MouseEvent e) {
timer.start();
}
});
yahooLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 0) {
try {
Runtime.getRuntime().exec("cmd.exe /c start http://www.yahoo.com");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
public void mouseEntered(MouseEvent e) {
timer.stop();
}
public void mouseExited(MouseEvent e) {
timer.start();
}
});
add(googleLabel);
add(yahooLabel);
moveLabel();*/
}
public void moveLabel() {
Rectangle r = new Rectangle();
r.x = xLoc;
r.y = yLoc;
Dimension size;// = googleLabel.getPreferredSize();
for(int i=0;i<stringlabel.length;i++)
{
JLabel label = new JLabel(stringlabel[i][0]);
size = label.getPreferredSize();
r.width = size.width;
r.height = size.height;
label.setBounds(r);
r.x += r.width+5;
}
/*r.width = size.width;
r.height = size.height;
googleLabel.setBounds(r);
// Append the Width of First Label to avoid overlapping
r.x += r.width + 5;
size = yahooLabel.getPreferredSize();
r.width = size.width;
r.height = size.height;
yahooLabel.setBounds(r);*/
xLoc -= 5;
if (xLoc < 0 )
xLoc = dimension.width;
}
}