Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

java - Is there any way I can rotate this 90 degrees?

This is my current code:

package Calendar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Clock extends JLabel {
    private String pattern;
    private Timer timer;

public Clock(String pattern){
    this.pattern = pattern;
    createTimer();
    timer.start();
}

public Clock(){
    pattern = "hh:mm:ss a";
    createTimer();
    timer.start();
}

private void createTimer(){
    timer = new Timer(0, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            setText(new SimpleDateFormat(pattern).format(new Date()));
        }
    });
}

public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel contentPane = new JPanel();
    frame.setContentPane(contentPane);
    Clock digitalClock = new Clock();
    contentPane.add(digitalClock);
    frame.setVisible(true);
}
}

and I cannot for the life of me figure out how to get this rotated. I looked on several other forums but there was no body else with this issue.

As far as I can tell there is no way to rotate either a JPanel or JFrame so am I out of luck?

This is what it looks like right now:

Digital Clock

And I would like to get it so the text is rotated 90 degrees. basically vertical.

EDIT: SOLUTION:

    package Calendar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.Timer;
import java.awt.*;
import java.awt.geom.*;

public class Clock extends JLabel {

    private String pattern;
    private Timer timer;

    public Clock ()
    {
        super();
        pattern = "hh:mm:ss a";
        createTimer();
        timer.start();
    }

    public Clock(String pattern)
    {
        super (pattern);
        this.pattern = pattern;
        createTimer();
        timer.start();
    }

    private void createTimer(){
        timer = new Timer(0, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                setText(new SimpleDateFormat(pattern).format(new Date()));
            }
        });
    }

    public void paint (Graphics g)
    {
        if (g instanceof Graphics2D) {
            Graphics2D g2 = (Graphics2D) g;
            AffineTransform flipTrans = new AffineTransform();
            double widthD = (double) getWidth();
            flipTrans.setToRotation(-Math.PI / 2.0, getWidth() / 2.0, getHeight() / 2.0);
            g2.setTransform(flipTrans);
            super.paint(g);
        } else {
            super.paint(g);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is based on this example and makes use of JXLayer to physically rotate the label component the way you want it.

It also saves you from having to do any custom painting of your own.

I've used a similar technique to rotate JTabbedPane tabs so they run vertically down the side of the JTabbedPane

Clock

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jdesktop.jxlayer.JXLayer;
import org.pbjar.jxlayer.demo.TransformUtils;
import org.pbjar.jxlayer.plaf.ext.transform.DefaultTransformModel;

public class RotateClock {

    public static void main(String[] args) {
        new RotateClock();
    }

    public RotateClock() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ExamplePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ExamplePane extends JPanel {

        private JSlider slider;
        private Clock clock;
        private DefaultTransformModel transformModel;

        public ExamplePane() {

            setLayout(new BorderLayout());

            slider = new JSlider(0, 360);
            slider.setValue(0);
            slider.setSnapToTicks(true);
            slider.setPaintTicks(true);
            slider.setMajorTickSpacing(10);
            slider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {

                    transformModel.setRotation(Math.toRadians(slider.getValue()));

                }
            });

            clock = new Clock();

            transformModel = new DefaultTransformModel();
            transformModel.setRotation(Math.toRadians(0));
            transformModel.setScaleToPreferredSize(true);
            JXLayer<JComponent> rotatePane = TransformUtils.createTransformJXLayer(clock, transformModel);

            add(slider, BorderLayout.NORTH);
            add(rotatePane);

        }
    }

    public class Clock extends JLabel {

        private String pattern;
        private Timer timer;

        public Clock(String pattern) {
            this.pattern = pattern;
            setText(new SimpleDateFormat(pattern).format(new Date()));
            createTimer();
            timer.start();
        }

        public Clock() {
            this("hh:mm:ss a");
        }

        private void createTimer() {
            timer = new Timer(0, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    setText(new SimpleDateFormat(pattern).format(new Date()));
                }
            });
            timer.setInitialDelay(0);
        }
    }

}

This deals with, amongst other things, oddities with mouse events and the preferred size of the component.

Unfortunately, the code required to perform the transformation (Piet's examples) is no longer available on the net

I've put all the source code of JXLayer (version 3) and Piet's examples into a single zip and I would suggest, if you are interested, you grab a copy and store it some where safe.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...