我正在尝试将滚动条添加到jtextarea,它在加载时右旋。但是,滚动窗格不能按预期工作。需要将一个滚动窗格添加到右旋转的jtextarea中。对此有任何建议是值得欢迎的。 原始代码段已附加
import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
public class InhibitScreenRightRotated extends JPanel {
public InhibitScreenRightRotated() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
InhibitScreenRightRotated inhibitScreenRightRotated = new InhibitScreenRightRotated();
JFrame f = new JFrame();
f.setSize(450, 450);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(inhibitScreenRightRotated);
f.setVisible(true);
}
public String inhibitList() {
String str = null;
for (int i = 0; i < 100; i++) {
str = str + "Room door is open. Close door to continue." + i + "\n";
}
return str;
}
public void jbInit() throws Exception {
this.setBounds(0, 0, 400, 375);
this.setLayout(null);
this.setBorder(new BevelBorder(0, new Color(255, 255, 255, 255), new Color(182, 182, 182, 255),
new Color(62, 62, 255, 255), new Color(89, 89, 89, 255)));
this.setBackground(new Color(47, 61, 110, 255));
rotatingLabel.setBounds(new Rectangle(334, 2, 40, 399));
rotatingLabel.setForeground(new Color(255, 255, 255, 255));
rotatingLabel.setText("System Inhibits");
rotatingLabel.setFont(new Font("Dialog", 1, 14));
rotatingLabel.setName("INHIBITS_LIST_HEADER");
jButton1.setBackground(new Color(141, 152, 190, 255));
// jButton1.setBorder(new CuiBorder());
jButton1.setFocusPainted(false);
jButton1.setText("Close");
jButton1.setName("INHIBITS_LIST_CLOSE");
jTextArea1.getCaret().deinstall(jTextArea1);
jTextArea1.setEditable(false);
jTextArea1.setBounds(new Rectangle(9, 42, 382, 275));
jTextArea1.setWrapStyleWord(true);
jTextArea1.setLineWrap(true);
jTextArea1.setBackground(new Color(186, 193, 218, 255));
jTextArea1.setText(inhibitList());
jTextArea1.setName("INHIBITS_LIST_TEXTAREA");
// setDocument... needs to be done
jButton1.setBounds(new Rectangle(15, ((jTextArea1.getWidth() + 9) - 83), 30, 83));
this.add(rotatingLabel);
this.add(jTextArea1);
this.add(jButton1);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(340, 10);
g2d.rotate(Math.toRadians(90));
jTextArea1.setVisible(false);
jTextArea1.update(g2d);
g2d.dispose();
}
JTextArea jTextArea1 = new JTextArea();
JScrollPane scrollPane = new JScrollPane();
public static final Font FONT_MSEC_LABEL_VALUE = new Font("Arial", Font.BOLD, 16);
RotatingLabel rotatingLabel = new RotatingLabel(Math.toRadians(90), FONT_MSEC_LABEL_VALUE, "System Inhibits", 0,
-170);
IUIRotatedButton jButton1 = new IUIRotatedButton();
public class IUIRotatedButton extends JButton {
private String str = "";
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int iX = getWidth() / 2;
int iY = getHeight() / 2;
g2.rotate(+Math.PI / 2, iX, iY);
g2.drawString(str, iX - 15, iY + 5);
}
public void setText(String str) {
this.str = str;
repaint();
}
}
public class RotatingLabel extends JLabel {
private Font font = null;
private String sText = null;
private double dThetaInRadians = 0;
private int iXExtra = 15;
private int iYExtra = 25;
private Icon icon = null;
public RotatingLabel(double dThetaInRadians, Font font, String sText, int iXExtra, int iYExtra) {
super();
this.font = font;
this.sText = sText;
this.dThetaInRadians = dThetaInRadians;
this.iXExtra = iXExtra;
this.iYExtra = iYExtra;
this.setHorizontalAlignment(SwingConstants.CENTER);
this.setVerticalAlignment(SwingConstants.CENTER);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
int iX = getWidth() / 2 + iXExtra;
int iY = getHeight() / 2 + iYExtra;
g2.rotate(dThetaInRadians, iX, iY);
if (sText != null)
g2.drawString(sText, iX, iY);
if (icon != null) {
g2.drawImage(((ImageIcon) icon).getImage(), iX, iY, this);
}
}
public void setText(String sText) {
// uncomment it for testing only.
// super.setText(sText);
this.sText = sText;
repaint();
}
}
}
在上面的代码块中,我为JTextArea添加了JScrollPane,但是滚动窗格没有按预期工作
import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
public class InhibitScreenRightRotated extends JPanel {
public InhibitScreenRightRotated() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
InhibitScreenRightRotated inhibitScreenRightRotated = new InhibitScreenRightRotated();
JFrame f = new JFrame();
f.setSize(450, 450);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(inhibitScreenRightRotated);
f.setVisible(true);
}
public String inhibitList() {
String str = null;
for (int i = 0; i < 100; i++) {
str = str + "Room door is open. Close door to continue." + i + "\n";
}
return str;
}
public void jbInit() throws Exception {
this.setBounds(0, 0, 400, 375);
this.setLayout(null);
this.setBorder(new BevelBorder(0, new Color(255, 255, 255, 255), new Color(182, 182, 182, 255),
new Color(62, 62, 255, 255), new Color(89, 89, 89, 255)));
this.setBackground(new Color(47, 61, 110, 255));
rotatingLabel.setBounds(new Rectangle(334, 2, 40, 399));
rotatingLabel.setForeground(new Color(255, 255, 255, 255));
rotatingLabel.setText("System Inhibits");
rotatingLabel.setFont(new Font("Dialog", 1, 14));
rotatingLabel.setName("INHIBITS_LIST_HEADER");
jButton1.setBackground(new Color(141, 152, 190, 255));
// jButton1.setBorder(new CuiBorder());
jButton1.setFocusPainted(false);
jButton1.setText("Close");
jButton1.setName("INHIBITS_LIST_CLOSE");
jTextArea1.getCaret().deinstall(jTextArea1);
jTextArea1.setEditable(false);
jTextArea1.setBounds(new Rectangle(9, 42, 382, 275));
jTextArea1.setWrapStyleWord(true);
jTextArea1.setLineWrap(true);
jTextArea1.setBackground(new Color(186, 193, 218, 255));
jTextArea1.setText(inhibitList());
jTextArea1.setName("INHIBITS_LIST_TEXTAREA");
// setDocument... needs to be done
scrollPane.setViewportView(jTextArea1);
scrollPane.setBounds(new Rectangle(69, 10, 275, 382));
scrollPane.setBackground(Color.white);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jButton1.setBounds(new Rectangle(15, ((jTextArea1.getWidth() + 9) - 83), 30, 83));
this.add(rotatingLabel);
this.add(scrollPane);
this.add(jButton1);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(340, 10);
g2d.rotate(Math.toRadians(90));
jTextArea1.setVisible(false);
jTextArea1.update(g2d);
g2d.dispose();
}
JTextArea jTextArea1 = new JTextArea();
JScrollPane scrollPane = new JScrollPane();
public static final Font FONT_MSEC_LABEL_VALUE = new Font("Arial", Font.BOLD, 16);
RotatingLabel rotatingLabel = new RotatingLabel(Math.toRadians(90), FONT_MSEC_LABEL_VALUE, "System Inhibits", 0,
-170);
IUIRotatedButton jButton1 = new IUIRotatedButton();
public class IUIRotatedButton extends JButton {
private String str = "";
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int iX = getWidth() / 2;
int iY = getHeight() / 2;
g2.rotate(+Math.PI / 2, iX, iY);
g2.drawString(str, iX - 15, iY + 5);
}
public void setText(String str) {
this.str = str;
repaint();
}
}
public class RotatingLabel extends JLabel {
private Font font = null;
private String sText = null;
private double dThetaInRadians = 0;
private int iXExtra = 15;
private int iYExtra = 25;
private Icon icon = null;
public RotatingLabel(double dThetaInRadians, Font font, String sText, int iXExtra, int iYExtra) {
super();
this.font = font;
this.sText = sText;
this.dThetaInRadians = dThetaInRadians;
this.iXExtra = iXExtra;
this.iYExtra = iYExtra;
this.setHorizontalAlignment(SwingConstants.CENTER);
this.setVerticalAlignment(SwingConstants.CENTER);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
int iX = getWidth() / 2 + iXExtra;
int iY = getHeight() / 2 + iYExtra;
g2.rotate(dThetaInRadians, iX, iY);
if (sText != null)
g2.drawString(sText, iX, iY);
if (icon != null) {
g2.drawImage(((ImageIcon) icon).getImage(), iX, iY, this);
}
}
public void setText(String sText) {
// uncomment it for testing only.
// super.setText(sText);
this.sText = sText;
repaint();
}
}
}