0

いくつかのメッセージを表示するバーを作成しようとしていて、問題が発生しました。下の画像をチェックして、これまでの内容を確認してください: http://i.stack.imgur.com/xSMtY.png

トップバーには、ラベルを含む自分で作成したパネルがあります。各メッセージはラベルであり、パネル内を転がります。問題は、メッセージが画面から消えるときです。彼女に最後まで行ってもらいたいです。しかし、作成したJPanelでBoxLayoutを使用しているため、白いバーでは同じレイアウトが得られ、同じ問題が発生します。チェーンを壊さずにルーリングを続ける方法がわかりません...

(ジャワで)

あなたの誰かが私を助けることができれば、私はそれを嬉しく思います...あなたの時間を前もってありがとう:)

編集:要求に応じて、ここにいくつかのコードを投稿します:

これは私のカスタムパネルコードです:

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                   
}

コードがあります...助けてくれてありがとう:)

4

0 に答える 0