fir
commit
b648749ded
|
@ -0,0 +1,3 @@
|
||||||
|
out/*
|
||||||
|
.idea/*
|
||||||
|
note.iml
|
Binary file not shown.
After Width: | Height: | Size: 158 KiB |
|
@ -0,0 +1,85 @@
|
||||||
|
package top.dreamcenter.note;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.text.BadLocationException;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.KeyAdapter;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JFrame jf = new JFrame("日记本");
|
||||||
|
|
||||||
|
jf.setSize(800, 1020);
|
||||||
|
jf.setResizable(false);
|
||||||
|
jf.setLocationRelativeTo(null);
|
||||||
|
jf.getContentPane().setLayout(null);
|
||||||
|
jf.setBackground(Color.WHITE);
|
||||||
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
// 内容面板穿透
|
||||||
|
JPanel imPanel=(JPanel) jf.getContentPane();
|
||||||
|
imPanel.setOpaque(false);
|
||||||
|
|
||||||
|
// 设置背景图片
|
||||||
|
URL bkPath = Main.class.getClassLoader().getResource("note.png");
|
||||||
|
assert bkPath != null;
|
||||||
|
ImageIcon backgroundImage = new ImageIcon(bkPath);
|
||||||
|
Image image = backgroundImage.getImage();
|
||||||
|
Image newimg = image.getScaledInstance(jf.getWidth() - 50, jf.getHeight(), java.awt.Image.SCALE_SMOOTH);
|
||||||
|
JLabel backgroundLabel = new JLabel(new ImageIcon(newimg));
|
||||||
|
backgroundLabel.setBounds(0, 0, jf.getWidth() , jf.getHeight());
|
||||||
|
|
||||||
|
|
||||||
|
// 文本行
|
||||||
|
JTextArea textArea = new JTextArea();
|
||||||
|
textArea.setFont(new Font(null, Font.PLAIN, 36));
|
||||||
|
textArea.setOpaque(false); // 透明背景
|
||||||
|
textArea.setLineWrap(true); // 自动换行
|
||||||
|
textArea.setTabSize(2);
|
||||||
|
textArea.setBounds(20, 90, jf.getWidth() - 40, jf.getHeight() - 140);
|
||||||
|
|
||||||
|
int maxRows = 19;
|
||||||
|
textArea.addKeyListener(new KeyAdapter() {
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
try {
|
||||||
|
int len = textArea.getText().length();
|
||||||
|
Rectangle rec = textArea.modelToView(len);
|
||||||
|
// 最大行数
|
||||||
|
int line = rec.y / rec.height + 1;
|
||||||
|
|
||||||
|
// 超出部分逻辑判断
|
||||||
|
if (line > maxRows) {
|
||||||
|
int i = len - 1;
|
||||||
|
for (; i >= 0; i--) {
|
||||||
|
Rectangle rec0 = textArea.modelToView(i);
|
||||||
|
|
||||||
|
int tmpLine = rec0.y / rec0.height + 1;
|
||||||
|
if (tmpLine <= maxRows){
|
||||||
|
i++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("超出行数字符串内容:" + textArea.getText(i, len - i));
|
||||||
|
textArea.setText(textArea.getText(i, len - i));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (BadLocationException badLocationException) {
|
||||||
|
badLocationException.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// 添加组件
|
||||||
|
jf.getLayeredPane().add(backgroundLabel, Integer.valueOf(Integer.MIN_VALUE));
|
||||||
|
jf.getContentPane().add(textArea);
|
||||||
|
|
||||||
|
jf.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue