2017-05-14 1 views
0

J'ai essayé de faire pivoter une JLabel Clock de 180 degrés de sorte qu'elle soit à l'envers, j'ai fait quelques recherches et essayé de l'implémenter en utilisant Graphics2D, mais je n'arrive pas à la trouver. travailler sans tout gâcher. Toute aide est appréciée. Mon plus gros problème semble être que depuis que j'ai créé mon jLabel automatiquement en utilisant NetBeans et quand j'essaie d'ajouter les lignes de code Graphics2D que j'ai vues d'autres exemples, le bloc entier cesse de fonctionner.Rotation de Jlabel en Java en utilisant NetBeans

package time; 

import java.awt.Color; 
import java.awt.Toolkit; 
import java.util.Calendar; 
import java.util.GregorianCalendar; 
import java.text.DecimalFormat; 
import javax.swing.JTextField; 

/** 
* 
* @author beaty 
*/ 
public class Interface extends javax.swing.JFrame { 

    /** 
    * Creates new form Interface 
    */ 
    public Interface() { 

     //Sets background as black and makes it auto fullscreen, windowed 
     getContentPane().setBackground(Color.black); 


     initComponents(); 

     //make fullscreen 
     Toolkit tk = Toolkit.getDefaultToolkit(); 
     int xsize = (int) tk.getScreenSize().getWidth(); 
     int ysize = (int) tk.getScreenSize().getHeight(); 
     this.setSize(xsize, ysize); 


     //Thread generates the calendar and autoupdates 
     new Thread() 
     { 
      public void run() 
      { 
       while(true) 
       { 
        Calendar cal = new GregorianCalendar(); 
        DecimalFormat fmt = new DecimalFormat("00"); 


        int hour = cal.get(Calendar.HOUR); 
        int minute = cal.get(Calendar.MINUTE); 
        int second = cal.get(Calendar.SECOND); 
        int AM_PM = cal.get(Calendar.AM_PM); 

        String day_night = ""; 
        if(AM_PM ==1) 
        { 
         day_night = "PM"; 
        } 
        else 
        { 
         day_night = "AM"; 
        } 

        if(hour == 0) 
        { 
         hour = 12; 
        } 
        else 
        { 

        } 
        String time = hour + ":" + fmt.format(minute) + " " + day_night; 
        /*  Swap this trime string to add seconds 
          *String time = hour + ":" + fmt.format(minute) + ":" + fmt.format(second) + " " + day_night; 

        */ 

        Clock.setText(time); 
       } 
      } 
     }.start(); 

    } 

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

     Clock = new javax.swing.JLabel(); 
     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setTitle("WHPL Clock"); 
     setAutoRequestFocus(false); 
     setBackground(new java.awt.Color(0, 0, 0)); 
     setUndecorated(true); 

     Clock.setBackground(new java.awt.Color(0, 0, 0)); 
     Clock.setFont(new java.awt.Font("DS-Digital", 0, 300)); // NOI18N 
     Clock.setForeground(new java.awt.Color(255, 255, 255)); 
     Clock.setText("7:45"); 
     Clock.setHorizontalAlignment(JTextField.CENTER); 

     jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/time/exit.png"))); // NOI18N 
     jButton1.setActionCommand("System.out(0);"); 
     jButton1.setBorder(new javax.swing.border.MatteBorder(new javax.swing.ImageIcon(getClass().getResource("/time/exit.png")))); // NOI18N 
     jButton1.setBorderPainted(false); 
     jButton1.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/time/exit_selected.png"))); // NOI18N 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(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() 
       .addGap(115, 115, 115) 
       .addComponent(Clock, javax.swing.GroupLayout.DEFAULT_SIZE, 1232, Short.MAX_VALUE)) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(63, 63, 63) 
       .addComponent(Clock, javax.swing.GroupLayout.PREFERRED_SIZE, 221, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 

     getAccessibleContext().setAccessibleDescription(""); 

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // Button action to exit application 
     System.exit(0); 
    }           

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

    // Variables declaration - do not modify      
    private javax.swing.JLabel Clock; 
    private javax.swing.JButton jButton1; 
    // End of variables declaration     
} 

Répondre

0

Je suis en train de tourner un JLabel horloge 180 degrés il est à l'envers

Pourquoi? Qui veut essayer de lire le texte à l'envers?

essayer de le mettre en œuvre à l'aide Graphics2D

Où? Je ne vois aucun code qui utilise des graphiques?

Swing n'est pas vraiment conçu pour faire tourner les composants.

Vous pouvez consulter:

  1. Text Icon - il fera une icône qui ressemble à du texte.

  2. Rotated Icon - vous permet de faire pivoter une icône de 180 degrés.

Mais les deux icônes ensemble et vous avez une implémentation qui vous permet essentiellement de faire pivoter le texte en faisant tourner une icône.