有问题的代码:
textArea.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
posX = e.getX();
posY = e.getY();
}
});
textArea.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
setLocation(e.getXOnScreen() - posX, e.getYOnScreen() - posY);
}
});
背景:
我有一个 JFrame,在那个 JFrame 中有一个 JScrollPane,在 JScrollPane 中有一个名为“textArea”的 JTextArea。这个 JTextArea 占据了整个 JFrame 并且 JFrame 是未修饰的。所以给出一些观点,这通常是 JFrame 的样子......
当鼠标在 JTextArea 内单击并移动时,整个窗口被拖动。一切都设置为无法专注于这项工作,这意味着它是一个覆盖。
问题:
上面列出的代码运行良好,世界和平。但是一旦有足够的文本出现垂直滚动条(由于换行,没有水平滚动条),拖动窗口就会成为问题。当您单击并开始移动时,JFrame 会立即在屏幕上移动得更高。JTextArea 中的行,当您尝试移动它时,它会向上移动。我认为 get*OnScreen() 方法是有问题的,因为它都与 JTextArea 相关。
有问题的班级:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main extends JFrame {
private JTextArea textArea;
private JScrollPane textAreaScroll;
private int posX = 0;
private int posY = 0;
public Main() {
initComponents();
initListeners();
for(int i = 0; i < 20; i++){
addLine(i+" Hello");
}
}
public void addLine(String line){
textArea.append("\n> "+line);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
private void initListeners(){
textArea.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
posX = e.getX();
posY = e.getY();
}
});
textArea.addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
setLocation(e.getXOnScreen() - posX, e.getYOnScreen() - posY);
}
});
}
private void initComponents() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {}
textAreaScroll = new JScrollPane();
textArea = new JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
setAlwaysOnTop(true);
setAutoRequestFocus(false);
setBackground(new Color(130,210,255,130));
setFocusCycleRoot(false);
setFocusable(false);
setFocusableWindowState(false);
setName("main");
setOpacity(0.4f);
setResizable(false);
textAreaScroll.setBorder(null);
textAreaScroll.setFocusable(false);
textAreaScroll.setRequestFocusEnabled(false);
textArea.setEditable(false);
textArea.setBackground(new Color(0, 0, 0));
textArea.setColumns(20);
textArea.setFont(new Font("Consolas", 0, 14));
textArea.setForeground(new Color(255, 255, 255));
textArea.setLineWrap(true);
textArea.setRows(5);
textArea.setText("> Hello world!\n> another line!");
textArea.setBorder(null);
textArea.setFocusable(false);
textArea.setRequestFocusEnabled(false);
textAreaScroll.setViewportView(textArea);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textAreaScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(textAreaScroll, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
}