изменить цвет лабиринта с JSlider в java swing - PullRequest
0 голосов
/ 10 июля 2019

Как мне изменить цвет метки в java swing. Во время работы цвет ярлыка меняется между красным и синим цветом через каждые 200 миллисекунд. Когда я перемещаю ползунок, интервал изменения цвета метки обновляется и равен значению jslider. Но мой код не работает. Кто-нибудь, помогите мне, пожалуйста, спасибо. Вот мой код.

JFrame

public class AOutcome extends javax.swing.JFrame {

/**
 * Creates new form AOutcome
 */
public AOutcome() {
    initComponents();
   // jlbColor.setOpaque(true);
}

/**
 * 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() {

    jlbColor = new javax.swing.JLabel();
    jsdColor = new javax.swing.JSlider();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jlbColor.setFont(new java.awt.Font("Lucida Grande", 1, 28)); // NOI18N
    jlbColor.setForeground(new java.awt.Color(0, 0, 255));
    jlbColor.setText("Java is cool");

    jsdColor.setMajorTickSpacing(100);
    jsdColor.setMaximum(1000);
    jsdColor.setMinimum(200);
    jsdColor.setPaintLabels(true);
    jsdColor.setPaintTicks(true);
    jsdColor.addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(javax.swing.event.ChangeEvent evt) {
            jsdColorStateChanged(evt);
        }
    });

    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(21, Short.MAX_VALUE)
            .addComponent(jsdColor, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(19, 19, 19))
        .addGroup(layout.createSequentialGroup()
            .addGap(111, 111, 111)
            .addComponent(jlbColor, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(43, 43, 43)
            .addComponent(jlbColor)
            .addGap(18, 18, 18)
            .addComponent(jsdColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(24, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void jsdColorStateChanged(javax.swing.event.ChangeEvent evt) {                                      
    changecolor();
}                                     

/**
 * @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(AOutcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(AOutcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(AOutcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(AOutcome.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new AOutcome().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel jlbColor;
private javax.swing.JSlider jsdColor;
// End of variables declaration                   

private void changecolor() {
    int color;
    color = jsdColor.getValue();
    jlbColor.setBackground(new Color(color));
    jlbColor.repaint();
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...