Java 我的JscrollPane不是';t(垂直)滚动整个图像,
当我增加preferredSize时,会发现更多的BuffereImage(在JPanel上,在JFrame上) 图像的大小将根据程序正在读取的文件的大小而变化 我在那里设置滚动窗格Java 我的JscrollPane不是';t(垂直)滚动整个图像,,java,swing,jpanel,jscrollpane,preferredsize,Java,Swing,Jpanel,Jscrollpane,Preferredsize,当我增加preferredSize时,会发现更多的BuffereImage(在JPanel上,在JFrame上) 图像的大小将根据程序正在读取的文件的大小而变化 我在那里设置滚动窗格 public Frame(String title, int theWidth, int theHeight) throws FileNotFoundException { super(String.format("Title", title)); this.width = the
public Frame(String title, int theWidth, int theHeight) throws FileNotFoundException {
super(String.format("Title", title));
this.width = theWidth;
this.height = theHeight;
this.panel = new Panel(width, height);
this.scroller = new JScrollPane(this.panel);
this.panel.drawLinesAndTab();
this.scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.scroller.setPreferredSize(new Dimension(this.width, this.height));
this.getContentPane().add(this.scroller);
}
(别开枪)这是我的小组课
public class Panel extends JPanel {
private BufferedImage image;
private ReadTabFile tab;
private String title;
private String theNotes;
private String theFlag;
private int a, b, c, x, y, beginBarlineX, beginBarlineY, endBarX, endBarY;
private ArrayList list;
private Font letterFont;
private Font flagFont;
private int width;
private int height;
boolean newLine = false;
public Panel(int theWidth, int theHeight) throws FileNotFoundException {
this.width = theWidth;
this.height = theHeight;
this.tab = new ReadTabFile("tabSource.txt");
this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = this.image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.image.getWidth(), this.image.getHeight());
setBorder(BorderFactory.createLineBorder(Color.black));
this.setFocusable(true);
}
public void drawLinesAndTab() {
Graphics g = this.image.getGraphics();
g.setColor(Color.black);
this.list = tab.readTabCode();
this.a = 20;
this.b = 100;
this.c = 60;
this.x = 40;
this.y = 100;
this.beginBarlineX = 20;
this.beginBarlineY = 100;
this.endBarY = 980;
this.endBarY = 100;
this.title = tab.getTitle();
g.drawString(this.title, 40, 20);
for (int i = 0; i < this.list.size(); i++) {
Bar theBar = (Bar) this.list.get(i);
drawBarline(a, b, a, b + 125);
ArrayList<String> stuff = theBar.getLinesInBar();
for (int j = 0; j < stuff.size(); j++) {
String line = stuff.get(j);
theFlag = line.substring(0, 1);
theNotes = line.substring(1, line.length());
if (newLine = true){
//make some bar lines.
}
try {
int w = this.image.getWidth();
//int h = this.image.getHeight();
if (c <= (w - 40)) {
newLine = false;
String zero = theFlag;
drawFlag(zero, x + 5, y - 20);
String one = theNotes.substring(0, 1);
g.drawLine(a, b, c, b);
drawLetter(one, x, y);
String two = theNotes.substring(1, 2);
drawLetter(two, x, y += 25);
g.drawLine(a, b += 25, c, b);
String three = theNotes.substring(2, 3);
drawLetter(three, x, y += 25);
g.drawLine(a, b += 25, c, b);
String four = theNotes.substring(3, 4);
drawLetter(four, x, y += 25);
g.drawLine(a, b += 25, c, b);
String five = theNotes.substring(4, 5);
drawLetter(five, x, y += 25);
g.drawLine(a, b += 25, c, b);
String six = theNotes.substring(5, 6);
drawLetter(six, x, y += 25);
g.drawLine(a, b += 25, c, b);
this.repaint();
b -= 125;
y -= 125;
x += 40;
a += 40;
c += 40;
} else {
newLine = true;
a = 20;
x = 20;
b += 225;
c = 60;
y += 225;
beginBarlineX = 20;
beginBarlineY += 100;
endBarX += 100;
endBarY = 100;
this.repaint();
}
} catch (Exception ex) {
System.err.println(ex + " within Panel, drawtab for loop");
}
}
}
}
public void drawBarline(int xTop, int yTop, int xBot, int yBot) {
Graphics g = this.image.getGraphics();
g.setColor(Color.black);
g.drawLine(xTop, yTop, xBot, yBot);
}
public Point makeBarline(int xTop, int yTop, int xBot, int yBot) {
Graphics g = this.image.getGraphics();
g.setColor(Color.black);
g.drawLine(xTop, yTop, xBot, yBot);
return (new Point());
}
public Point drawLetter(String letter, int x, int y) throws FontFormatException, IOException {
Graphics g = this.image.getGraphics();
g.setColor(Color.black);
g.setFont(letterFont(letter).deriveFont(20.0f));
g.drawString(letter, x, y);
return (new Point());
}
public Point drawFlag(String letter, int x, int y) throws FontFormatException, IOException {
Graphics g = this.image.getGraphics();
g.setColor(Color.black);
g.setFont(flagFont(letter).deriveFont(30.0f));
g.drawString(letter, x, y);
return (new Point());
}
public Font letterFont(String fontString) throws FontFormatException, IOException {
Graphics g = this.image.getGraphics();
g.setColor(Color.black);
if (!Character.isDigit(fontString.charAt(0))) {
this.letterFont = Font.createFont(Font.TRUETYPE_FONT, new File("LeRoy.ttf"));
g.getFontMetrics(this.letterFont);
g.setFont(this.letterFont);
return this.letterFont;
} else {
return null;
}
}
public Font flagFont(String fontString) throws FontFormatException, IOException {
Graphics g = this.image.getGraphics();
g.setColor(Color.black);
if (!Character.isDigit(fontString.charAt(0))) {
this.flagFont = Font.createFont(Font.TRUETYPE_FONT, new File("LeroyLuteNotes1.ttf"));
g.getFontMetrics(this.flagFont);
g.setFont(this.flagFont);
return this.flagFont;
} else {
return null;
}
}
@Override
public Dimension getPreferredSize() {
return (new Dimension(this.image.getWidth(), this.image.getHeight()));
}
public BufferedImage getImage() {
return this.image;
}
@Override
public void paintComponent(Graphics graphics) {
Graphics g = graphics.create();
g.drawImage(this.image, 0, 0, null);
}
}
公共类面板扩展了JPanel{
私有缓冲图像;
私有ReadTabFile选项卡;
私有字符串标题;
私人字符串注释;
私有字符串偏移;
专用int a、b、c、x、y、beginBarlineX、beginBarlineY、endBarX、endBarY;
私有数组列表;
私人字体字体;
私有字体;
私有整数宽度;
私人内部高度;
布尔换行=假;
公共面板(int theWidth,int theHeight)引发FileNotFoundException{
this.width=宽度;
this.height=高度;
this.tab=new ReadTabFile(“tabSource.txt”);
this.image=新的BuffereImage(宽度、高度、BuffereImage.TYPE\u INT\u RGB);
Graphics g=this.image.createGraphics();
g、 setColor(Color.WHITE);
g、 fillRect(0,0,this.image.getWidth(),this.image.getHeight());
setboorder(BorderFactory.createLineBorder(Color.black));
此参数为.setFocusable(true);
}
public void drawLinesAndTab(){
Graphics g=this.image.getGraphics();
g、 设置颜色(颜色为黑色);
this.list=tab.readTabCode();
这个a=20;
这是b=100;
这个c=60;
这个.x=40;
这个y=100;
此.beginBarlineX=20;
此参数为0.beginBarlineY=100;
这个.endBarY=980;
这个.endBarY=100;
this.title=tab.getTitle();
g、 抽绳(此标题,40,20);
for(int i=0;iJScrollPane
根据组件的首选大小设置其内容视图,而不是组件的内容
如果希望滚动内容:image,请覆盖面板
类的getPreferreedSize(Dimension)
功能,并将图像大小作为组件的首选大小返回
我们应该始终在被重写的paintComponent(Graphics)
函数中调用super.paintComponent(g)
,这是您目前没有执行的操作
JScrollPane
根据组件的首选大小设置其内容视图,而不是组件的内容
如果希望滚动内容:image,请覆盖面板
类的getPreferreedSize(Dimension)
功能,并将图像大小作为组件的首选大小返回
我们应该始终在被重写的paintComponent(Graphics)
函数中调用super.paintComponent(g)
,这是您目前没有执行的操作
JScrollPane
根据组件的首选大小设置其内容视图,而不是组件的内容
如果希望滚动内容:image,请覆盖面板
类的getPreferreedSize(Dimension)
功能,并将图像大小作为组件的首选大小返回
我们应该始终在被重写的paintComponent(Graphics)
函数中调用super.paintComponent(g)
,这是您目前没有执行的操作
JScrollPane
根据组件的首选大小设置其内容视图,而不是组件的内容
如果希望滚动内容:image,请覆盖面板
类的getPreferreedSize(Dimension)
功能,并将图像大小作为组件的首选大小返回
我们应该始终在被重写的paintComponent(Graphics)
函数中调用super.paintComponent(g)
,这是您目前没有执行的操作
我想我已经在我的Panel类中重写了getPreferredSize(D),一个维度接受两个整数,对吗?我将其传递为this.image.getWidth()和this.image.getHeight()。我还在Frame类中设置了preferredSize。因此,我仍然不明白为什么它没有滚动到底。@juju,当您使用add(scrollPane)将其添加到框架内容窗格时,请尝试不将preferredSize设置为JScrollPane
它会将JScrollPane
添加到BorderLayout.CENTER
。只需在Frame()
构造函数的末尾调用pack()
。试试看,让我知道谢谢你的帮助,这个.pack()看起来与getPreferredSize()完全一样所以我认为这是另外一回事。现在某个地方抛出了一个空指针,所以测试时间到了。滚动条现在可以工作了,谢谢你的帮助。图像仍然只显示在面板的大小上。但是谢谢你的帮助:)我想我已经覆盖了getPreferredSize(D)在我的Panel类中,一个维度取两个整数?我将其作为this.image.getWidth()和this.image.getHeight()传递。我还在Frame类中设置了preferredSize。因此,我仍然不明白为什么它没有滚动到底。@juju,当您使用add(scrollPane)将其添加到框架内容窗格时,请尝试不将preferredSize设置为JScrollPane
这将把JScrollPane
添加到BorderLayout.CENTER
。而只需调用