0

これが私が構築しているアプリケーションですhttps://github.com/chrisbramm/LastFM-History-Graph .

以下は、 のコントローラ クラスの一部ですsrc/lastfmhistoryclasses。がOutputGUIView作成されると、3 つのコンポーネント a が作成され、これがと別のJPanel graphPanelに追加されます。これらはすべて に追加されます。次に、以下のリスナーが の優先サイズを変更し、ボタンを初めて押すと、UI が更新されて、サイズが大きくなり、 のスクロールバーが変化したことを示します。JScrollPane graphScrollPanelJPanel autocompletePanelJFrame OutputGUIViewgraphPanelgraphPanelgraphScrollPanel

ただし、別のボタンを押すと、優先サイズが変更されますが、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は、すべてのgraphHeightが実際に大きいため、含まれているgraphPanelが実際に大きいことを認識しません。行うことは、高さが ~6000px の長方形を描画することです。この場合、setPreferredSize を使用する必要がありますか、または Graphics2D オブジェクトのサイズを設定する別の方法はありますか?

4

1 に答える 1

5

updateUIこれは UI ルック アンド フィール API に関連しており、再描画とはほとんど関係ありません。

試した/使用しているレイアウトマネージャーは何ですか?

への呼び出しはinvalidate()repaint()親コンテナーのレイアウト マネージャーに影響を与え、それに応じてコンポーネントを再レイアウトする必要がありますが、レイアウト マネージャーは最小/最大/優先サイズの情報のみをガイドとして使用することを覚えておく必要があります。

あなたの例からの私の最高のゲストは、あなたがどちらかを呼び出そうとするべきだということです

outputGUIView.graphPanel.invalidate();
outputGUIView.graphPanel.repaint();

および/または

outputGUIView.invalidate();
outputGUIView.repaint();

スクロール ペインで無効化を呼び出しても、スクロール ペインではなくビューポートがコンテンツのレイアウトを担当するため、おそらく多くのことは達成されません。

あなたが本当に必死なら、あなたは試すことができます

outputGUIView.graphScrollPanel.getViewport().invalidate();
outputGUIView.graphScrollPanel.getViewport().repaint();
于 2012-07-24T23:39:24.280 に答える