谢谢大家啊!想实现的效果是这样的:import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class RollWord extends JFrame { private static final long serialVersionUID = -2397593626990759111L; private JPanel pane = null; private MoveLabel label = null; private JScrollPane jScrollPane1; public RollWord(){ super("Test"); pane = new JPanel(); this.getContentPane().add(pane);//Container javax.swing.JFrame.getContentPane() {
label = new MoveLabel(""); //label.setLineWrap(true); label.setText("带有滚动效sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss果的标签"); //label.getText(); pane.add(label); label.setPreferredSize(new java.awt.Dimension(287, 159)); } { jScrollPane1 = new JScrollPane(); pane.add(jScrollPane1); } this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//void javax.swing.JFrame.setDefaultCloseOperation(int operation) this.setSize(300, 200); this.setVisible(true); } public static void main(String args[]) { new RollWord(); } class MoveLabel extends JTextArea implements Runnable { private static final long serialVersionUID = 1891684760189602720L; private String text = null; private Thread thread = null; private int x = 0; private int w = 0, h = 0; public MoveLabel(String text) { super(text); this.text = text; thread = new Thread(this); thread.start(); } public String getText() { return text; } public void setText(String text) { super.setText(text); this.text = text; } protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(this.getBackground()); g.fillRect(0, 0, w = this.getWidth(), h = this.getHeight()); g.setColor(this.getForeground()); g.setFont(this.getFont());
g.drawString(text, 0, x); } public void run() { while (true) { x -= 5; if (x < -h) { x = h; } this.repaint(); try { Thread.sleep(50); } catch (InterruptedException e){ e.printStackTrace(); } } } }}
问题解决了,谢谢各位!import com.cloudgarden.layout.AnchorConstraint; import com.cloudgarden.layout.AnchorLayout; import java.awt.GridLayout; import javax.swing.JPanel; import javax.swing.JTextArea;import javax.swing.WindowConstants; import javax.swing.SwingUtilities; /** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class NewJFrame extends javax.swing.JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel jPanel1; private JTextArea jTextArea1; int x=508; int y=334; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { NewJFrame inst = new NewJFrame(); inst.setLocationRelativeTo(null); inst.setVisible(true); } }); }
public NewJFrame() { super(); initGUI(0,0,500,500); initGUI(500,500,800,800); pack(); this.setSize(x, y); }
JScrollPane里面放JTextArea或者EditorPane之类的,然后操作JScrollPane,不知道是不是你要的效果
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RollWord extends JFrame { private static final long serialVersionUID = -2397593626990759111L; private JPanel pane = null; private MoveLabel label = null;
private JScrollPane jScrollPane1; public RollWord(){
super("Test");
pane = new JPanel();
this.getContentPane().add(pane);//Container javax.swing.JFrame.getContentPane()
{
label = new MoveLabel("");
//label.setLineWrap(true);
label.setText("带有滚动效sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss果的标签");
//label.getText();
pane.add(label);
label.setPreferredSize(new java.awt.Dimension(287, 159));
}
{
jScrollPane1 = new JScrollPane();
pane.add(jScrollPane1);
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//void javax.swing.JFrame.setDefaultCloseOperation(int operation)
this.setSize(300, 200);
this.setVisible(true);
} public static void main(String args[]) {
new RollWord();
}
class MoveLabel extends JTextArea implements Runnable { private static final long serialVersionUID = 1891684760189602720L; private String text = null; private Thread thread = null; private int x = 0; private int w = 0, h = 0; public MoveLabel(String text) {
super(text);
this.text = text;
thread = new Thread(this);
thread.start();
} public String getText() {
return text;
} public void setText(String text) {
super.setText(text);
this.text = text;
} protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(this.getBackground());
g.fillRect(0, 0, w = this.getWidth(), h = this.getHeight());
g.setColor(this.getForeground());
g.setFont(this.getFont());
g.drawString(text, 0, x);
} public void run() {
while (true) {
x -= 5;
if (x < -h) {
x = h;
}
this.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}}
不过现在遇到个问题,就是显示内容过长时不会自动转行,真的不知道怎么解决,希望大家帮忙下,上面代码是借鉴高手的!
<MARQUEE onmouseover="this.stop()" onmouseout="this.start()" width='1180'>
<a href="#" target="_blank">CCCCCCC.doc</a>
<a href="#" target="_blank">aaa.doc</a>
<a href="#" target="_blank">vvv.doc</a>
</MARQUEE>
</div>
import com.cloudgarden.layout.AnchorLayout;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JTextArea;import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewJFrame extends javax.swing.JFrame { /**
*
*/
private static final long serialVersionUID = 1L;
private JPanel jPanel1;
private JTextArea jTextArea1;
int x=508;
int y=334; /**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public NewJFrame() {
super();
initGUI(0,0,500,500);
initGUI(500,500,800,800);
pack();
this.setSize(x, y);
}
private void initGUI(int bgx,int bgy,int endx,int endy) {
try {
AnchorLayout thisLayout = new AnchorLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
getContentPane().add(getJPanel(), new AnchorConstraint(bgy, endx, endy, bgx, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
}
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
public JPanel getJPanel(){
JPanel panel=new JPanel();
GridLayout jPanel1Layout = new GridLayout(1, 1);
jPanel1Layout.setColumns(1);
jPanel1Layout.setHgap(5);
jPanel1Layout.setVgap(5);
panel.setLayout(jPanel1Layout);
{
jTextArea1 = new JTextArea();
panel.add(jTextArea1);
jTextArea1.setText("jTextAsssssssssssssssssssssssssssssssssssssssssssssssssssssrea1");
jTextArea1.setLineWrap(true);
}
return panel;
}}