다음은 내가 구축중인 애플리케이션입니다. https://github.com/chrisbramm/LastFM-History-Graph.Java updateUI가 단 한 번만 작동합니다.
다음은 컨트롤러 클래스의 일부입니다 (src/lastfmhistoryclasses
). OutputGUIView
을 만들면 JPanel graphPanel
의 세 가지 구성 요소가 만들어지고 JScrollPane graphScrollPanel
및 JPanel autocompletePanel
에 추가됩니다. 이들은 모두 JFrame OutputGUIView
에 추가됩니다. 아래의 청취자는 기본 크기 인 graphPanel
을 변경하고 graphPanel
의 크기가 증가하고 graphScrollPanel
의 스크롤 막대가 변경되었음을 나타 내기 위해 UI가 업데이트하는 버튼을 처음으로 누르면 변경됩니다.
그러나 다른 버튼을 누르면 기본 크기가 변경되지만 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();
}
}
나는 모든 동안 graphPanel 그냥 거기에 앉아뿐만 아니라 같은 다양한 구성 요소에 invalidate/validate/revalidate
으로 (그리고 다시 칠) 다른 일을 시도하고 창 크기를 변경하는 경우에만 페인트 다시했습니다.
내가 잘못하고있는 아이디어가 있습니까?
편집 UPDATE
: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 객체 각 트랙 사각형을 생성 한 다음이 어딘가로 그려 : 여기 는 GraphPanel 클래스이며 Graphics2D 그래프 오브젝트 이제는 setPreferredSize as recommended here의 사용을 제거했지만 지금은 수동으로 graphPanel에서 height를 6000으로 설정했을 때 graphScrollPanel이 그 graphPanel을 인식하지 못하는 것을 포함하여 모든 graphHeight가 실제로 더 커지는 문제가 있습니다. ~ 6000px 높이의 직사각형을 그립니다. 따라서이 경우 setPreferredSize를 사용해야합니까 아니면 Graphics2D 객체의 크기를 설정하는 다른 방법이 있습니까?
을 시도 할 수 정말 절망적 인 경우
나는 더 많은 정보가 타락하지 않을 비트.구성 요소 레이아웃 (무엇의 레이아웃인지)과 사용중인 레이아웃 관리자 (예 : – MadProgrammer
)는 prefSize 속성에 약간의 기대가있는 것처럼 들립니다. LayoutManager에서 사용할 크기 지정 힌트입니다. 자동으로 마술을 트리거하는 수단이 아닙니다. 즉, "zoom"과 같은 응용 프로그램 속성 :-) 또는 다른 말로하면 : 사용자 정의 패널을 구현하여 확대/축소 속성을 지원하고 뷰 계층 구조에 다시 레이아웃해야한다는 것을 알립니다. – kleopatra