使用NetBeans在Java中旋转Jlabel

时间:2017-05-14 19:50:33

标签: java swing rotation jlabel

我一直在尝试将JLabel时钟旋转180度,因此它是颠倒的,我已经完成了一些搜索并尝试使用Graphics2D实现它,但是我找不到一种方法让它在没有乱糟糟的东西的情况下工作。任何帮助表示赞赏。

我最大的麻烦似乎是因为我使用NetBeans自动创建了jLabel,当我尝试添加我从其他示例中看到的Graphics2D代码行时,整个块停止工作。

  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                   
}

1 个答案:

答案 0 :(得分:0)

  

我一直试图将JLabel时钟旋转180度,使其颠倒

为什么呢?谁想尝试颠倒阅读文字?

  

尝试使用Graphics2D实现它

在哪儿?我没有看到任何使用Graphics的代码?

Swing并非真正用于旋转组件。

你可以看看:

  1. Text Icon - 它会生成一个看起来像文字的图标。

  2. Rotated Icon - 允许您将图标旋转180度。

  3. 但是这两个图标在一起,你有一个实现,它基本上允许你通过旋转图标来旋转文本。