gpt4 book ai didi

java - 有什么办法可以旋转90度吗?

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:59 26 4
gpt4 key购买 nike

这是我当前的代码:

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);
}
}

我一生都无法弄清楚如何旋转它。我查看了其他几个论坛,但没有其他人讨论这个问题。

据我所知,没有办法旋转 JPanel 或 JFrame,所以我运气不好吗?

这就是现在的样子:

Digital Clock

我希望文本旋转 90 度。基本上是垂直的。

编辑:解决方案:

    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 example并利用 JXLayer按照您想要的方式物理旋转标签组件。

它还使您不必自己进行任何自定义绘画。

我使用了类似的技术来旋转 JTabbedPane 选项卡,以便它们沿着 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);
}
}

}

这涉及到鼠标事件和组件的首选大小等问题。

不幸的是,执行转换所需的代码(Piet 的示例)在网上不再可用

我已将 JXLayer(版本 3)和 Piet 示例的所有源代码放入一个 zip 中。我建议,如果您有兴趣,请获取一份副本并将其存储在安全的地方。

关于java - 有什么办法可以旋转90度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22976226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com