我愿意添加一个移动的文字和图片(从一边到另一边,然后从“窗口”出来重复循环)。
目前我正在使用这种结构在我的应用程序中显示菜单:
try
{
repaint = ImageIO.read(new File(ReturnPageName(0)));
}catch (IOException e) {
}
image = new ImageIcon(repaint.getScaledInstance(sizeX,sizeY, Image.SCALE_SMOOTH));
imageLabel.setIcon(image);
revalidate();
repaint();
在JLabel
中使用以下参数设置JFrame
:
imageLabel = new JLabel();
imageLabel.setIcon(image);
imageLabel.setVisible(true);
add(imageLabel);
setUndecorated(true);
setVisible(true);
setSize(sizeX, sizeY);
我想添加到显示的菜单图片移动文本/图片如上所述。我怎么能轻松做到?尝试使用新类扩展可运行但不成功。
编辑:
我做过类似的事情,
绘图菜单:
public void drawPageZero()
{
repaint.setData(repaint0.getData());
Graphics g = repaint.createGraphics();
g.setFont(font);
g.setColor(black);
FontMetrics fm = g.getFontMetrics();
if (xN < - fm.stringWidth(text))
{
xN = 2 * fm.stringWidth(text);
}
if (xN < 0 && xN >= -fm.stringWidth(text))
{
xN2 = xN + fm.stringWidth(text);
xN3 = xN + 2 *fm.stringWidth(text);
}
else if (xN <= 2*fm.stringWidth(text) && xN >= fm.stringWidth(text))
{
xN2 = xN - 2*fm.stringWidth(text);
xN3 = xN - fm.stringWidth(text);
}
else if (xN >= 0 && xN < fm.stringWidth(text))
{
xN2 = xN + fm.stringWidth(text);
xN3 = xN - fm.stringWidth(text);
}
g.drawString(text,xN,66);
g.drawString(text,xN2,66);
g.drawString(text,xN3,66);
image = new ImageIcon(repaint.getScaledInstance(sizeX,sizeY, Image.SCALE_SMOOTH));
imageLabel.setIcon(image);
revalidate();
repaint();
timer = new Timer();
timer.schedule(new infoBar(), 50);
}
计时器看起来像这样:
class infoBar extends TimerTask
{
@Override
public void run() {
if (page ==0) {
xN -= 1;
timer.cancel();
timer.purge();
drawPageZero();
}
else
{
timer.cancel();
timer.purge();
}
}
}
现在的问题是,在查看移动文本时会非常疼。这可能是由于刷新太慢,这可能是由repaint.setData(repaint0.getData());
操作引起的。有没有简单快捷的方法将图像从一个BufferedImage
粘贴到另一个{{1}}?在此之前我每个周期加载图片,但也很慢。
答案 0 :(得分:1)
使用javax.swing.Timer来安排定期事件,并从此计时器触发的线程进行动画更改。正如@ControlAltDel所指出的,JScrollPane可能是一个很好的类来查找实现移动文本。
答案 1 :(得分:0)
试试这个,创建一个新的Java类并粘贴这段代码:
import java.util.Timer;
import java.util.TimerTask;
public class TextoDesplazable extends javax.swing.JFrame {
Timer timer = new Timer();
int x, y, limite;
public TextoDesplazable() {
initComponents();
this.setLocationRelativeTo(null);
x = jLabel1.getX();
y = jLabel1.getY();
limite = -jLabel1.getWidth();
System.out.println("X: " + x);
timer.schedule(task, 0, 5);
}
TimerTask task = new TimerTask() {
@Override
public void run() {
if (x < limite) {
x = TextoDesplazable.this.getWidth();
}
jLabel1.setLocation(x--, y);
}
};
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Hello");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(308, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TextoDesplazable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TextoDesplazable().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}