问题描述
这是我当前的代码:
包日历;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 java.text.SimpleDateFormat;导入 java.util.Date;导入 javax.swing.JFrame;导入 javax.swing.JLabel;导入 javax.swing.JPanel;导入 javax.swing.Timer;公共类时钟扩展 JLabel {私有字符串模式;私人定时器定时器;公共时钟(字符串模式){this.pattern = 模式;创建定时器();计时器.start();}公共时钟(){模式=hh:mm:ss a";创建定时器();计时器.start();}私人无效createTimer(){计时器 = 新计时器(0,新 ActionListener(){@覆盖公共无效actionPerformed(ActionEvent arg0){setText(new SimpleDateFormat(pattern).format(new Date()));}});}公共静态无效主要(字符串[]参数){JFrame 框架 = 新的 JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel contentPane = new JPanel();frame.setContentPane(contentPane);时钟 digitalClock = new Clock();contentPane.add(digitalClock);frame.setVisible(true);}}
我终其一生都无法弄清楚如何旋转它.我查看了其他几个论坛,但没有其他人与此问题有关.
据我所知,没有办法旋转 JPanel 或 JFrame,所以我不走运吗?
这就是现在的样子:
,我建议,如果你有兴趣,你可以拿一份副本并将其存放在安全的地方.
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);
}
}
}
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
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.
这篇关于有什么办法可以旋转这个 90 度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!