如果我们拖动滚动条超过某个点,显示的文本就会变得“清晰”。我手动将字符串放在远端,以查看它是否可以显示并且工作。为什么JPanel剪辑后的文本在某个点之后?
当我手动设置它时,它会从这些坐标中正确绘制(如示例中所示),但在使用滚动条更改x坐标时会剪辑)。
这是代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ScrollBarDemo2 extends JFrame {
private MessagePanel messagePanel = new MessagePanel();
private JScrollBar jscbHorizontal = new JScrollBar(JScrollBar.HORIZONTAL);
private JScrollBar jscbVertical = new JScrollBar(JScrollBar.VERTICAL);
private JTextField jtfMessage = new JTextField("Example String");
public ScrollBarDemo2() {
// Add components to the frame
add(messagePanel, BorderLayout.CENTER);
add(jscbHorizontal, BorderLayout.SOUTH);
add(jscbVertical, BorderLayout.EAST);
add(jtfMessage, BorderLayout.NORTH);
// Register listener to scroll bars
ScrollBarListener jscbListener = new ScrollBarListener();
jscbHorizontal.addAdjustmentListener(jscbListener);
jscbVertical.addAdjustmentListener(jscbListener);
// Register a listener in text field
jtfMessage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Set the text in messagePanel to the given text
messagePanel.setText(e.getActionCommand());
}
});
}
private class ScrollBarListener implements AdjustmentListener {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
// Determine the orientation of the event source
JScrollBar scrollBar = (JScrollBar)e.getAdjustable();
if (scrollBar.getOrientation() == JScrollBar.HORIZONTAL) {
// Obtain the horizontal space remaining
double spaceAvailable = (double)messagePanel.getHorizontalEmptySpace();
// Find how much to scale each value of the scroll bars (since we're using the default 100 total values)
double scaledValue = (scrollBar.getValue() * spaceAvailable/(double)scrollBar.getMaximum());
// Set new x coordinate
messagePanel.setX((int)scaledValue);
}
else if (scrollBar.getOrientation() == JScrollBar.VERTICAL) {
// Obtain the vertical space remaining
double spaceAvailable = (double)messagePanel.getVerticalEmptySpace();
// Find how much to scale each value of the scroll bars (since we're using the default 100 total values)
double scaledValue = (scrollBar.getValue()/(double)scrollBar.getMaximum()) * spaceAvailable;
// Set new x coordinate
messagePanel.setY((int)scaledValue);
}
}
}
/** main method **/
public static void main(String[] args) {
ScrollBarDemo2 frame = new ScrollBarDemo2();
frame.setSize(500, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MessagePanel extends JPanel {
private FontMetrics fm;
private String message = "";
private int messageX = -1;
private int messageY = -1;
public MessagePanel() {
this("Welcome to Java");
}
public MessagePanel(String message) {
this.message = message;
}
public void moveRight() {
messageX += getWidth()/50;
repaint();
}
public void moveLeft() {
messageX -= getWidth()/50;
repaint();
}
public void moveUp() {
messageY -= getHeight()/100;
repaint();
}
public void moveDown() {
messageY += getHeight()/100;
repaint();
}
public int getX() {
return messageX;
}
public int getY() {
return messageY;
}
public void setX(int x) {
messageX = x;
repaint();
}
public void setY(int y) {
messageY = y;
repaint();
}
public void setText(String newMessage) {
message = newMessage;
repaint();
}
public int getVerticalEmptySpace() {
return getHeight() - fm.getAscent();
}
public int getHorizontalEmptySpace() {
return getWidth() - fm.stringWidth(message);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (messageX < 0 && messageY < 0) { // Check to initialize centered position
fm = g.getFontMetrics();
messageX = getWidth() - fm.stringWidth(message); // Manually setting it to the very end coordinate
messageY = getHeight()/2 - fm.getAscent()/2;
}
g.drawString(message, messageX, messageY);
}
}
'非常感谢您的回答和可运行代码! ' - 我给你解决了你的问题。你所需要做的就是重命名方法,并且你可以运行。我们不是在这里为你写代码,而是给你提供解决问题所需的信息。 – camickr
我明白了,但我被告知要“接受”,不幸的是我们只能“接受”一个,即使你给出了一个完全简洁和简洁的答案,这些答案完全显示了我做错了什么以及我需要注意什么。我没有在寻找有人专门投入他们的专业知识和时间,所以我只是被吉尔伯特的答案所淹没,并且尽管评论指导说不要这样写了一个“感谢”。一如既往,我非常感谢您和社区在这里为我的问题提供了许多优秀的答案和代码审查。 – Legate
'我被告知要“接受”,不幸的是我们只能“接受”一个,即使你给出了一个非常简洁和简洁的答案,这些答案完全显示了我做错了什么以及我需要注意什么。“ - 好像它看起来似乎这是你应该接受的答案。这是第一个指出问题并提供解决方案的答案,但你仍然没有表示感谢。 “一如既往,我非常感谢你和社区 - 通常你也会花时间感谢所有提出建议的人。而且,在你发布的第一个问题上,你也没有感谢任何人。 – camickr