这是我正在构建的应用程序https://github.com/chrisbramm/LastFM-History-Graph。
下面是控制器类的一部分src/lastfmhistoryclasses
。创建OutputGUIView
时,它会创建三个组件, a JPanel graphPanel
, this 添加到 aJScrollPane graphScrollPanel
和 another JPanel autocompletePanel
。然后这些都添加到JFrame OutputGUIView
. 然后下面的侦听器会更改首选大小,graphPanel
并且当您第一次按下按钮时,UI 会更新以显示graphPanel
大小增加并且滚动条graphScrollPanel
发生了变化。
但是现在,如果您按下另一个按钮,首选尺寸会发生变化,但 UI 不会更新,如果您通过最大化它来更改窗口尺寸,它会这样做。
class Zoom2000 implements ActionListener{
public void actionPerformed(ActionEvent e){
outputGUIView.graphPanel.setPreferredSize(new Dimension(screenWidth, 2000));
outputGUIView.graphScrollPanel.updateUI();
}
}
class ZoomDefault implements ActionListener{
public void actionPerformed(ActionEvent e){
outputGUIView.graphPanel.setPreferredSize(new Dimension(screenWidth, screenHeight));
outputGUIView.graphScrollPanel.updateUI();
}
}
class Zoom6000 implements ActionListener{
public void actionPerformed(ActionEvent e){
outputGUIView.graphPanel.setPreferredSize(new Dimension(screenWidth, 6000));
outputGUIView.graphScrollPanel.updateUI();
}
}
我一直在尝试不同的东西,例如invalidate/validate/revalidate
(和重新绘制)各种组件,而graphPanel 只是坐在那里,只有在您更改窗口尺寸时才会重新绘制。
任何想法我做错了什么?
编辑更新:这是 GraphPanel 类:
package lastfmhistoryguis;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
import de.umass.lastfm.*;
import lastfmhistoryclasses.*;
SuppressWarnings("serial")
public class GraphPanel extends JPanel {
private LastFMHistory graphData;
private int graphHeight;
private int graphWidth;
private int zoom;
private final int PAD = 20;
public GraphPanel(LastFMHistory model, int zoom){
this.graphData = model;
if (zoom != 1){
this.zoom = zoom;
}else{
this.zoom = 1;
System.out.println("getHeight() returning:" + getHeight());
}
System.out.println("Width" + getWidth() + "Height" + getHeight());
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Drawing");
Graphics2D graph = (Graphics2D) g;
if (graphData == null) {
System.err.println("No data found");
} else {
System.out.println("paintComponent Width" + getWidth() + "Height" + getHeight());
graphWidth = getWidth() - 5 * PAD;
//graphHeight = getHeight() - 2 * PAD;
graphHeight = 6000 - 2* PAD;
System.out.println(graphWidth + ", " + graphHeight);
int x0 = graphWidth + PAD;
graph.draw(new Rectangle2D.Double(PAD, PAD, graphWidth, graphHeight));
double xInc = (double) (graphWidth) / (graphData.dayMax);
double secondHeight = (double) (graphHeight) / 86400;
for (int i = 0; i <= 86400; i++) {
if (i % 3600 == 0) {
graph.draw(new Line2D.Double(x0, (i * secondHeight) + PAD,
x0 + 10, (i * secondHeight) + PAD));
String hour = Integer.toString(i / 3600);
graph.drawString(hour, x0 + 15,
(int) ((i * secondHeight) + PAD));
}
}
for (Track t : graphData.history) {
if (t.getPlayedWhen() != null) {
Color color = t.getColour();
int duration = t.getDuration();
int day = Math.abs(t.getDay());
double dayOrigin = x0 - ((day + 1) * xInc);
double timeOrigin = t.getGraphHeight() * secondHeight + PAD;
double trackHeight = duration * secondHeight;
graph.setColor(color);
// System.out.println("PLOTTING TRACK, " + day + ", " +
// dayOrigin + ", " + t.getGraphHeight() + ", " + timeOrigin
// + ", " + trackHeight);
// graph.draw(new Rectangle2D.Double(dayOrigin, timeOrigin,
// xInc, trackHeight));
graph.fillRect((int) dayOrigin, (int) timeOrigin,
(int) xInc, (int) trackHeight);
}
}
for (int i = 0; i < graphData.dayMax;){
graph.draw(new Line2D.Double(x0 - i * xInc, PAD, x0 - i * xInc, graphHeight + PAD));
i = i + 7;
}
}
}
public void zoom(int zoom){
this.zoom = zoom;
repaint();
}
}
它创建一个 Graphics2D 对象,在该对象上绘制一个大的轮廓矩形,然后将每个轨道矩形绘制到此 Graphics2D 图形对象的某个位置。现在我已经按照这里的建议删除了 setPreferredSize 的使用,但是现在我遇到了一个问题,即当我手动将 GraphPanel 中的 graphHeight 设置为 6000 的高度时,graphScrollPanel 没有意识到它所包含的 graphPanel 实际上更大,因为所有 graphHeight 都是正在绘制一个大约 6000px 高的矩形。所以在这种情况下我应该使用 setPreferredSize 还是有另一种方法来设置 Graphics2D 对象的大小?