- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 JFrame 中滚动文本,并且文本本身使用 MarqueePanel 包含超链接。类(class)。然而,虽然滚动工作正常,但链接似乎都在固定位置。
我尝试使用 JEditorPane 和 JTextPane,虽然外观正确,但超链接似乎根本没有移动。这是可以完成的事情吗?
编辑:代码如下 - MarqueePanel 类链接位于上方。
public static void Main(String[] args)
{
JFrame w = new JFrame("跑马灯测试");
尺寸 screenSize = Toolkit.getDefaultToolkit().getScreenSize();
w.setSize(screenSize.width, (int) ((float) .04 * (float) screenSize.height));
MarqueePanel mp = new MarqueePanel(22,2);
JEditorPane jep = new JEditorPane("text/html", "
测试链接编号 1
测试链接号 2
");
jep.setOpaque(假);
jep.setEditable(假);
jep.addMouseListener(mp);
jep.addHyperlinkListener(new HyperlinkListener() {
公共(public)无效超链接更新(HyperlinkEvent e){
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if (Desktop.isDesktopSupported()) {
尝试 {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException e1) {
//TODO 自动生成的 catch block
e1.printStackTrace();
} catch (URISyntaxException e1) {
//TODO 自动生成的 catch block
e1.printStackTrace();
}
}
}
}
});
mp.add(jep);
w.add(mp);
w.pack();
w.setVisible(true);
}
最佳答案
Marquee Panel
并不是为处理事件而设计的,因为它只是在不同的位置呈现每个组件以提供滚动效果。
这是一个尝试转换 MouseEvent 并将事件重新分派(dispatch)回原始组件的版本:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* The MarqueePanelMouse is used to scroll components from the right edge of the
* panel to the left edge. Scrolling is continuous. To simulate the scrolling
* of text you can simply add a JLabel to the panel.
*
* Various properties control the scrolling of the components on the panel.
* Changes to the properties are dynamic and will take effect the next time
* the components are scrolled.
*/
public class MarqueePanelMouse extends JPanel
implements ActionListener, AncestorListener, WindowListener, MouseListener, MouseMotionListener
{
protected boolean paintChildren;
protected boolean scrollingPaused;
protected int scrollOffset;
protected int wrapOffset;
private int preferredWidth = -1;
private int scrollAmount;
private int scrollFrequency;
private boolean wrap = false;
private int wrapAmount = 50;
private boolean scrollWhenFocused = true;
private Timer timer = new Timer(1000, this);
/**
* Convenience constructor that sets both the scroll frequency and
* scroll amount to a value of 5.
*/
public MarqueePanelMouse()
{
this(5, 5);
}
/**
* Create an AnimatedIcon that will continuously cycle with the
* default (500ms).
*
* @param component the component the icon will be painted on
* @param icons the Icons to be painted as part of the animation
*/
public MarqueePanelMouse(int scrollFrequency, int scrollAmount)
{
setScrollFrequency( scrollFrequency );
setScrollAmount( scrollAmount );
setLayout( new BoxLayout(this, BoxLayout.X_AXIS) );
addAncestorListener( this );
// addMouseListener( this );
// addMouseMotionListener( this );
}
/*
* Translate the location of the children before they are painted so it
* appears they are scrolling left to right
*/
@Override
public void paintChildren(Graphics g)
{
// Need this so we don't see a flicker of the text before scrolling
if (! paintChildren) return;
// Normal painting as the components scroll right to left
Graphics2D g2d = (Graphics2D)g;
g2d.translate(-scrollOffset, 0);
super.paintChildren(g);
g2d.translate(scrollOffset, 0);
// Repaint the start of the components on the right edge of the panel once
// all the components are completely visible on the panel.
// (Its like the components are in two places at the same time)
if (isWrap())
{
wrapOffset = scrollOffset - super.getPreferredSize().width - wrapAmount;
g2d.translate(-wrapOffset, 0);
super.paintChildren(g);
g2d.translate(wrapOffset, 0);
}
}
/*
* The default preferred size will be half the size of the components added to
* the panel. This will allow room for components to be scrolled on and off
* the panel.
*
* The default width can be overriden by using the setPreferredWidth() method.
*/
@Override
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
d.width = (preferredWidth == -1) ? d.width / 2 : preferredWidth;
return d;
}
@Override
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public int getPreferredWidth()
{
return preferredWidth;
}
/**
* Specify the preferred width on the panel. A value of -1 will cause the
* default preferred with size calculation to be used.
*
* @param preferredWidth preferred width of the panel in pixels
*/
public void setPreferredWidth(int preferredWidth)
{
this.preferredWidth = preferredWidth;
revalidate();
}
/**
* Get the scroll amount.
*
* @return the scroll amount in pixels
*/
public int getScrollAmount()
{
return scrollAmount;
}
/**
* Specify the scroll amount. The number of pixels to scroll every time
* scrolling is done.
*
* @param scrollAmount scroll amount in pixels
*/
public void setScrollAmount(int scrollAmount)
{
this.scrollAmount = scrollAmount;
}
/**
* Get the scroll frequency.
*
* @return the scroll frequency
*/
public int getScrollFrequency()
{
return scrollFrequency;
}
/**
* Specify the scroll frequency. That is the number of times scrolling
* should be performed every second.
*
* @param scrollFrequency scroll frequency
*/
public void setScrollFrequency(int scrollFrequency)
{
this.scrollFrequency = scrollFrequency;
int delay = 1000 / scrollFrequency;
timer.setInitialDelay( delay );
timer.setDelay( delay );
}
/**
* Get the scroll only when visible property.
*
* @return the scroll only when visible value
*/
public boolean isScrollWhenFocused()
{
return scrollWhenFocused;
}
/**
* Specify the scrolling property for unfocused windows.
*
* @param scrollWhenVisible when true scrolling pauses when the window
* loses focus. Scrolling will continue when
* the window regains focus. When false
* scrolling is continuous unless the window
* is iconified.
*/
public void setScrollWhenFocused(boolean scrollWhenFocused)
{
this.scrollWhenFocused = scrollWhenFocused;
}
/**
* Get the wrap property.
*
* @return the wrap value
*/
public boolean isWrap()
{
return wrap;
}
/**
* Specify the wrapping property. Normal scrolling is such that all the text
* will scroll from left to right. When the last part of the text scrolls off
* the left edge scrolling will start again from the right edge. Therefore
* there is a time when the component is blank as nothing is displayed.
* Wrapping implies that as the end of the text scrolls off the left edge
* the beginning of the text will scroll in from the right edge. So the end
* and the start of the text is displayed at the same time.
*
* @param wrap when true the start of the text will scroll in from the right
* edge while the end of the text is still scrolling off the left
* edge. Otherwise the panel must be clear of text before
* will begin again from the right edge.
*/
public void setWrap(boolean wrap)
{
this.wrap = wrap;
}
/**
* Get the wrap amount.
*
* @return the wrap amount value
*/
public int getWrapAmount()
{
return wrapAmount;
}
/**
* Specify the wrapping amount. This specifies the space between the end of the
* text on the left edge and the start of the text from the right edge when
* wrapping is turned on.
*
* @param wrapAmount the amount in pixels
*/
public void setWrapAmount(int wrapAmount)
{
this.wrapAmount = wrapAmount;
}
/**
* Start scrolling the components on the panel. Components will start
* scrolling from the right edge towards the left edge.
*/
public void startScrolling()
{
paintChildren = true;
scrollOffset = - getSize().width;
timer.start();
}
/**
* Stop scrolling the components on the panel. The conponents will be
* cleared from the view of the panel
*/
public void stopScrolling()
{
timer.stop();
paintChildren = false;
repaint();
}
/**
* The components will stop scrolling but will remain visible
*/
public void pauseScrolling()
{
if (timer.isRunning())
{
timer.stop();
scrollingPaused = true;
}
}
/**
* The components will resume scrolling from where scrolling was stopped.
*/
public void resumeScrolling()
{
if (scrollingPaused)
{
timer.restart();
scrollingPaused = false;
}
}
@Override
public Component getComponentAt(int x, int y)
{
Point translated = getTranslatedPoint(x, y);
for (Component c: getComponents())
{
if (c.getBounds().contains(translated))
return c;
}
return null;
}
public Point getTranslatedPoint(int x, int y)
{
int translatedX = x + scrollOffset;
if (isWrap())
{
int preferredWidth = super.getPreferredSize().width;
preferredWidth += getWrapAmount();
translatedX = translatedX % preferredWidth;
}
return new Point(translatedX, y);
}
// Implement ActionListener
/**
* Adjust the offset of the components on the panel so it appears that
* they are scrolling from right to left.
*/
public void actionPerformed(ActionEvent ae)
{
scrollOffset = scrollOffset + scrollAmount;
int width = super.getPreferredSize().width;
if (scrollOffset > width)
{
scrollOffset = isWrap() ? wrapOffset + scrollAmount : - getSize().width;
}
repaint();
}
// Implement AncestorListener
/**
* Get notified when the panel is added to a Window so we can use a
* WindowListener to automatically start the scrolling of the components.
*/
public void ancestorAdded(AncestorEvent e)
{
SwingUtilities.windowForComponent( this ).addWindowListener( this );
}
public void ancestorMoved(AncestorEvent e) {}
public void ancestorRemoved(AncestorEvent e) {}
// Implement WindowListener
public void windowActivated(WindowEvent e)
{
if (isScrollWhenFocused())
resumeScrolling();
}
public void windowClosed(WindowEvent e)
{
stopScrolling();
}
public void windowClosing(WindowEvent e)
{
stopScrolling();
}
public void windowDeactivated(WindowEvent e)
{
if (isScrollWhenFocused())
pauseScrolling();
}
public void windowDeiconified(WindowEvent e)
{
resumeScrolling();
}
public void windowIconified(WindowEvent e)
{
pauseScrolling();
}
public void windowOpened(WindowEvent e)
{
startScrolling();
}
// Implement MouseMotionListener
public void mouseMoved(MouseEvent e)
{
redispatchMouseEvent(e);
}
public void mouseDragged(MouseEvent e)
{
redispatchMouseEvent(e);
}
public void mouseClicked(MouseEvent e)
{
redispatchMouseEvent(e);
System.out.println("clicked");
}
public void mouseEntered(MouseEvent e)
{
redispatchMouseEvent(e);
}
public void mouseExited(MouseEvent e)
{
redispatchMouseEvent(e);
}
public void mousePressed(MouseEvent e)
{
redispatchMouseEvent(e);
}
public void mouseReleased(MouseEvent e)
{
redispatchMouseEvent(e);
}
private void redispatchMouseEvent(MouseEvent e)
{
int eventID = e.getID();
Component component = getComponentAt( e.getX(), e.getY() );
if (component == null) return;
Point translatedPoint = getTranslatedPoint( e.getX(), e.getY() );
Point componentPoint = SwingUtilities.convertPoint(this, translatedPoint, component);
System.out.println(eventID + " : " + componentPoint);
MouseEvent me = new MouseEvent(
component,
e.getID(),
e.getWhen(),
e.getModifiers(),
componentPoint.x,
componentPoint.y,
e.getClickCount(),
e.isPopupTrigger()
);
component.dispatchEvent( me );
}
/*
@Override
public boolean isOptimizedDrawingEnabled()
{
return false;
}
*/
}
此版本尚未经过调试或全面测试,因此它可能适合您,也可能不适合您。
关于java - MarqueePanel 中的 JEditorPane/JTextPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782443/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!