我试图做一个显示一些消息的栏,但我遇到了麻烦,请查看下面的图片,看看我到目前为止有什么:http: //i.stack.imgur.com/xSMtY.png
所以,在顶部栏中,我有一个自己制作的包含标签的面板,每条消息都是一个标签,它会在面板中滚动,问题是当消息离开屏幕时我希望她走到最后队列,但是由于我在我的 JPanel 中使用 BoxLayout 我不能这样做,在白栏中,我得到了相同的布局并且我遇到了同样的问题,我不知道如何在不中断链的情况下保持滚动...
(在 Java 中)
如果你们中的任何人可以帮助我,我会很高兴...提前感谢大家的时间:)
编辑:根据要求,我将在这里发布一些代码:
这是我的自定义面板代码:
package smstest;
import entidades.MensagemParaEcra;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import static java.lang.Thread.sleep;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
*
* @author NEMESIS
*/
public class MyPanel extends JPanel {
private JLabel label;
//private MyPanel panel;
private LinkedList<String> texto;
private LinkedList<MensagemParaEcra> msgs;
private LinkedList<JLabel> labels;
private int x = 0, tamanho;
public MyPanel() {
texto = new LinkedList<>();
msgs = new LinkedList<>();
labels = new LinkedList<>();
//config panel
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.setBackground(Color.black);
Dimension dim = new Dimension(1500, 30);
tamanho = 1500;
this.setPreferredSize(dim);
this.repaint();
this.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
tamanho = MyPanel.this.getWidth();
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
});
// start the scrolling of the labels
start();
}
public void addMensagem(MensagemParaEcra msg) {
labels.add(new JLabel(msg.getTexto() + " -- "));
refresh();
}
public void removerMsg(int id) {
int size = msgs.size();
for (int i = 0; i < size; i++) {
if (msgs.get(i).getId() == id) {
msgs.remove(i);
}
}
refresh();
}
private void refresh() {
int size = labels.size();
labels.get(0).setLocation(0, 0);
for (int i = 0; i < size; i++) {
labels.get(i).setForeground(Color.white);
labels.get(i).setFont(new Font("Tahoma", Font.BOLD, 40));
labels.get(i).setOpaque(true);
labels.get(i).setBackground(Color.red);
MyPanel.this.add(labels.get(i));
// since im using a box layout it's useless but otherwise this
// may add my labels one after the other
if (i > 0) {
int largura = labels.get(i - 1).getWidth();
labels.get(i).setLocation(x + largura, 0);
} else {
labels.get(i).setLocation(x, 0);
}
}
}
private void start() {
final Runnable running = new Runnable() {
@Override
public void run() {
MyPanel.this.repaint();
}
};
Thread t = new Thread() {
public void run() {
while (true) {
for (JLabel lb : labels) {
// make the labels get 3 pixels to the left
lb.setLocation(lb.getLocation().x -3, 0);
}
try {
SwingUtilities.invokeAndWait(running);
sleep(30);
} catch (InterruptedException ex) {
Logger.getLogger(MyPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(MyPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
t.start();
}
}
这是具有 2 个滚动文本栏的框架:
package smstest;
import entidades.DadosDaAplicacao;
import entidades.MensagemParaEcra;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import static java.lang.Thread.sleep;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
/**
*
* @author NEMESIS
*/
public final class MonitorDeMensagensFrame extends javax.swing.JFrame {
private static final MonitorDeMensagensFrame instance = new MonitorDeMensagensFrame();
private static Point point = new Point();
private int tamanho = 1500;
private JLabel label;
private JLabel label1;
/**
* Creates new form MonitorDeMensagensFrame
*/
public MonitorDeMensagensFrame() {
initComponents();
MyPanel topPanel = new MyPanel();
// its an undecorated frame so i have the listers to move it
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
point.x = e.getX();
point.y = e.getY();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Point p = getLocation();
setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
}
});
// setting the size of the frame
setSize(getRes().width, 73);
setLocation(0, 0);
PanelMsg.repaint();
// create the labels for the below bar
criarPub();
// add messages to the upper bar using a method in MyPanel
// this messages now are made up for tests
topPanel.addMensagem(new MensagemParaEcra(0, "msg 1"));
topPanel.addMensagem(new MensagemParaEcra(1, "msg 2"));
topPanel.addMensagem(new MensagemParaEcra(2, "msg 3"));
topPanel.addMensagem(new MensagemParaEcra(3, "msg 4"));
topPanel.addMensagem(new MensagemParaEcra(4, "msg 5"));
topPanel.addMensagem(new MensagemParaEcra(5, "msg 6"));
topPanel.addMensagem(new MensagemParaEcra(6, "msg 7"));
// start the rolling text on the below bar
startPub();
}
private Dimension getRes() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension scrnsize = toolkit.getScreenSize();
return scrnsize;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
PanelMsg = new javax.swing.JPanel();
PanelPublicidade = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
getContentPane().setLayout(new java.awt.GridLayout(2, 0));
PanelMsg.setBackground(new java.awt.Color(255, 0, 0));
PanelMsg.setMinimumSize(new java.awt.Dimension(173, 20));
PanelMsg.setLayout(new java.awt.BorderLayout());
getContentPane().add(PanelMsg);
PanelPublicidade.setBackground(new java.awt.Color(102, 255, 102));
PanelPublicidade.setMinimumSize(new java.awt.Dimension(403, 25));
javax.swing.GroupLayout PanelPublicidadeLayout = new javax.swing.GroupLayout(PanelPublicidade);
PanelPublicidade.setLayout(PanelPublicidadeLayout);
PanelPublicidadeLayout.setHorizontalGroup(
PanelPublicidadeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 806, Short.MAX_VALUE)
);
PanelPublicidadeLayout.setVerticalGroup(
PanelPublicidadeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 26, Short.MAX_VALUE)
);
getContentPane().add(PanelPublicidade);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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 ex) {
java.util.logging.Logger.getLogger(MonitorDeMensagensFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MonitorDeMensagensFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MonitorDeMensagensFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MonitorDeMensagensFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MonitorDeMensagensFrame().setVisible(true);
}
});
}
public static MonitorDeMensagensFrame getInstance() {
return instance;
}
public void criarPub() {
String msg = DadosDaAplicacao.getInstance().getMsgPublicidade();
PanelPublicidade.setLayout(new BoxLayout(PanelPublicidade, BoxLayout.X_AXIS));
PanelPublicidade.setBackground(Color.black);
Dimension dim = new Dimension(1500, 30);
PanelPublicidade.setPreferredSize(dim);
PanelPublicidade.repaint();
PanelPublicidade.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
tamanho = PanelMsg.getWidth();
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
});
label = new JLabel("");
label1 = new JLabel("");
label.setText(msg + " ");
label1.setText(msg + " ");
label.setForeground(Color.black);
label.setFont(new Font("Tahoma", Font.PLAIN, 40));
label.setOpaque(true);
label.setBackground(Color.white);
label1.setForeground(Color.black);
label1.setFont(new Font("Tahoma", Font.PLAIN, 40));
label1.setOpaque(true);
label1.setBackground(Color.white);
PanelPublicidade.add(label);
PanelPublicidade.add(label1);
label.setLocation(PanelPublicidade.getLocation().x, PanelPublicidade.getLocation().y);
label1.setLocation(label.getWidth(), label.getLocation().y);
PanelPublicidade.repaint();
}
public void startPub() {
final int tamanho = label.getWidth();
final Runnable running = new Runnable() {
@Override
public void run() {
PanelPublicidade.repaint();
}
};
Thread t = new Thread() {
@Override
public void run() {
while (true) {
label.setLocation(label.getLocation().x - 3,0);
label1.setLocation(label1.getLocation().x - 3, 0);
// if(label.getLocation().x + tamanho == 0){
// label.setLocation(label1.getLocation().x, 0);
// }
try {
SwingUtilities.invokeAndWait(running);
sleep(28);
} catch (InterruptedException ex) {
Logger.getLogger(MonitorDeMensagensFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(MonitorDeMensagensFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
t.start();
}
// Variables declaration - do not modify
private javax.swing.JPanel PanelMsg;
private javax.swing.JPanel PanelPublicidade;
// End of variables declaration
}
有代码...谢谢你的帮助:)