由于操作系统限制,我正在尝试旋转填充有文本标签的 GridLayout 以模拟纵向视图。它们所在的 JPanel 不是方形的,因此当旋转 90 度时,标签会根据 JPanel 的尺寸被切断。是否可以根据旋转调整布局大小以仍然适合 JPanel?对此进行的研究显示了许多旋转选项,但仅限于方形 JPanel。
为了进一步解释我的问题:当我旋转里面绘制的标签时,它们保持格式化为正常方向的 x,y,并且我希望它格式化布局以适合 90 度旋转的 x,y (所以基本上 y 和 x 被翻转)。目前我的网格的一部分在旋转后被切断。此外,最终显示应适合当前 JPnel 中填充的所有 13 x 24 个字母。
编辑:使用模糊的注释表明我需要在旋转后进行绘画,但这样做会裁剪网格并且不会填充回我喜欢的尺寸。
J面板代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Screen extends JPanel {
private JLabel[][] labels = new JLabel[13][24];
private GridLayout layout;
public Screen() {
//setLocation(315,35);
layout = new GridLayout(13, 24);
layout.preferredLayoutSize(this);
//setBounds(315, 65, 243, 350);
setBounds(315, 65, 243, 350);
setLayout(layout);
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 24; j++) {
labels[i][j] = new JLabel();
labels[i][j].setBackground(Color.BLACK);
add(labels[i][j]);
}
}
//testing new letter
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 24; j++) {
labels[i][j].setText("T");
labels[i][j].setForeground(Color.GREEN);
}
}
setBackground(Color.black);
setVisible(true);
repaint();
}
@Override
public void paintComponent(Graphics g) {
//Rotates screen graphics to correct orientation
Graphics2D g2d = (Graphics2D) g;
int w2 = getWidth() / 2;
int h2 = getHeight() / 2;
g2d.rotate(Math.PI / 2, w2, h2);
super.paintComponent(g);
setSize(243,350);
}
}
测试代码:
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
public class RotateTest {
public static class Frame extends JFrame {
public Frame() {
Screen screen = new Screen();
JLayeredPane pane = new JLayeredPane();
setUndecorated(false);
setSize(800, 480);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.add(screen, 0, 0);
pane.setVisible(true);
add(pane);
}
}
public static void main(String[] args) {
Frame frame = new Frame();
}
}