我想在 JPanel 上绘制字符串和矩形。
我将 JPanel 添加到 JScrollPane 但如果我绘制一些字符串,则不会出现滚动条。
例如,这里有一些代码。
package gui;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class MainView extends JFrame {
private SelfDrawnPanel sdp;
private JScrollPane jsp;
public MainView(){
super("TestProgram");
this.setSize(300, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.sdp = new SelfDrawnPanel();
this.jsp = new JScrollPane(this.sdp);
this.add(this.jsp);
this.setVisible(true);
}
class SelfDrawnPanel extends JPanel {
public void paint(Graphics g) {
for (int i = 0; i < 100; i++) {
g.setColor(Color.BLACK);
g.drawString("test", 10, (i*10)+10);
}
}
}
}