2017-11-17 9 views
0

私は、更新されるイメージを表示するJScrollPaneを含むGUIをコーディングしています。イメージは、JLabelのImageIconにあります。イメージサイズは、ImageIcon.getIconWith()およびgetIconHeight()を使用して取得されます。 JLabelの推奨サイズはこれらの寸法で更新されます。更新されたイメージを含むJScrollPaneのサイズが正しくありません

初めてアプリケーションを起動するとき、JScrollPaneとそのスクロールバーは、(スクロールを使用して)イメージ全体を表示するための適切なディメンションを持っています。しかし、イメージが更新されると、JScrollPaneとスクロールバーは、イメージが前のイメージのサイズを持つとみなします。 JScrollPaneを正しく更新するにはどうすればよいですか?

ここに私のGUIのキュレーション版があります。 Visualizer.javaはGUI VisualizerGUI.javaを使用します。 「実行」ボタンが押されると、ImageDrawer.drawImage()(実際のアプリケーションの動作をシミュレートする)を使用して新しい画像がランダムに生成され、関数VisualizerGUI.setTransitionsImage(String imgPath)を使用してJScrollPaneのコンテンツが更新されます。

Visualizer.java:

import java.util.*; 
import java.io.*; 

import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class Visualizer implements ActionListener { 

    private VisualizerGUI gui = null; 

    public Visualizer() { 
    gui = VisualizerGUI.createAndStart(this); 
    } 

    public static void main(String[] args) { 
    Visualizer viz = new Visualizer(); 
    } 

    public void actionPerformed(ActionEvent e) { 
    if (e.getActionCommand().equals("Run command")) { 
     run(); 
    } 
    } 

    public void run() { 
    updateGUIwithSolution(); 
    } 

    public void updateGUIwithSolution() { 
    gui.initGUIupdate(); 
    try { 
     ImageDrawer.drawImage(); 
     gui.setTransitionsImage("image.png"); 
    } catch (Exception e) { 
     System.out.println("Error while generating image"); 
     e.printStackTrace(); 
    } 
    gui.finalizeGUIupdate(); 
    } 
} 

VisualizerGUI.java:

import java.util.*; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

import java.awt.*; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import java.lang.reflect.InvocationTargetException; 

public final class VisualizerGUI { 

    private JFrame frame; 
    private JButton runButton; 
    private JButton nextButton; 
    private JScrollPane transitionsDisplay; 
    private JTabbedPane executionsDisplay; 
    private JTabbedPane tracesDisplay; 
    private JTextArea textInfoArea; 

    public VisualizerGUI() {} 

    private void initGUI(ActionListener actionsHandler) { 
     //Create and set up the window. 
     frame = new JFrame("Visualizer"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel controlPanel = new JPanel(new FlowLayout()); 
    runButton = new JButton("Run"); 
    runButton.addActionListener(actionsHandler); 
    runButton.setActionCommand("Run command"); 
    controlPanel.add(runButton); 
    nextButton = new JButton("Next"); 
    nextButton.addActionListener(actionsHandler); 
    nextButton.setActionCommand("Find next solution"); 
    controlPanel.add(nextButton); 

    transitionsDisplay = new JScrollPane(); 
    executionsDisplay = new JTabbedPane(); 
    tracesDisplay = new JTabbedPane(); 

    JSplitPane ETspliter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, executionsDisplay, tracesDisplay); 
    JSplitPane graphsSpliter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, transitionsDisplay, ETspliter); 

    textInfoArea = new JTextArea(); 
    textInfoArea.setLineWrap(true); 
    textInfoArea.setWrapStyleWord(true); 
    textInfoArea.setEditable(false); 
    JScrollPane textInfoAreaSP = new JScrollPane(textInfoArea); 

    JSplitPane topSpliter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, graphsSpliter, textInfoAreaSP); 

    transitionsDisplay.setPreferredSize(new Dimension(200,200)); 
    executionsDisplay.setPreferredSize(new Dimension(200,200)); 
    tracesDisplay.setPreferredSize(new Dimension(200,200)); 
    textInfoAreaSP.setPreferredSize(new Dimension(200,100)); 

    frame.getContentPane().setLayout(new BorderLayout()); 
     frame.getContentPane().add(controlPanel, BorderLayout.NORTH); 
     frame.getContentPane().add(topSpliter, BorderLayout.CENTER); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static VisualizerGUI createAndStart(ActionListener actionsHandler) { 
    VisualizerGUI gui = new VisualizerGUI(); 
    final Runnable guiRunner = 
     new Runnable() { 
     public void run() { 
      gui.initGUI(actionsHandler); 
      // gui.pack(); 
     } 
     }; 
    try { 
     javax.swing.SwingUtilities.invokeAndWait(guiRunner); 
    } catch (InterruptedException e) { 
     System.out.println(">>> WARNING <<< InterruptedException while creating the GUI"); 
    } catch (InvocationTargetException e) { 
     System.out.println(">>> WARNING <<< InvocationTargetException while creating the GUI"); 
    } 
    return gui; 
    } 

    public void clear() { 
    initGUIupdate(); 
    finalizeGUIupdate(); 
    } 

    public void initGUIupdate() { 
    // frame.setVisible(false); 
    transitionsDisplay.setViewportView(null); 
    executionsDisplay.removeAll(); 
    tracesDisplay.removeAll(); 
    textInfoArea.setText(null); 
    } 

    public void pack() { 
    frame.pack(); 
    } 

    public void finalizeGUIupdate() { 
    // frame.validate(); 
    // frame.repaint(); 
    // frame.setVisible(true); 
    } 

    public void setTransitionsImage(String imgPath) { 
    ImageIcon icon = new ImageIcon(imgPath); 
    icon.getImage().flush(); 
    int width = icon.getIconWidth(); 
    int height = icon.getIconHeight(); 
    JLabel label = new JLabel(); 
    label.setVerticalAlignment(SwingConstants.CENTER); 
    label.setHorizontalAlignment(SwingConstants.CENTER); 
    label.setIcon(icon); 
    label.setPreferredSize(new Dimension(width,height)); 
    //label.setPreferredSize(null); 
    transitionsDisplay.setViewportView(label); 
    label.revalidate(); 
    label.repaint(); 
    transitionsDisplay.getViewport().revalidate(); 
    transitionsDisplay.getViewport().repaint(); 
    transitionsDisplay.revalidate(); 
    // transitionsDisplay.validate(); 
    transitionsDisplay.repaint(); 
    frame.revalidate(); 
    // frame.validate(); 
    frame.repaint(); 
    } 

    public void setTransitionsImageInED(String imgPath) { 
    final Runnable guiRunner = 
     new Runnable() { 
     public void run() { setTransitionsImage(imgPath); } 
     }; 
    // javax.swing.SwingUtilities.invokeLater(guiRunner); 
    try { 
     javax.swing.SwingUtilities.invokeAndWait(guiRunner); 
    } catch (InterruptedException e) { 
     System.out.println(">>> WARNING <<< InterruptedException while creating the GUI"); 
    } catch (InvocationTargetException e) { 
     System.out.println(">>> WARNING <<< InvocationTargetException while creating the GUI"); 
    } 
    } 
} 

ImageDrawer.java:

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

public class ImageDrawer { 

    public static void drawImage() throws Exception { 
    try { 
     int width = 20 + (int)(Math.random() * 1000); 
     int height = 20 + (int)(Math.random() * 1000); 

     BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D ig2 = bi.createGraphics(); 
     ig2.setPaint(Color.blue); 
     ig2.fillRect(0, 0, width, height); 
     ig2.setPaint(Color.red); 
     ig2.fillRect(5, 5, width - 10, height - 10); 
     ig2.setPaint(Color.blue); 
     ig2.drawLine(0, 0, width, height); 
     ig2.drawLine(0, height, width, 0); 

     ImageIO.write(bi, "PNG", new File("image.png")); 

    } catch (IOException ie) { 
     ie.printStackTrace(); 
    } 
    } 
} 

私はこの問題を持っている理由誰かが説明できますか?ありがとう!

答えて

2
transitionsDisplay.setPreferredSize(new Dimension(200,200)); 
executionsDisplay.setPreferredSize(new Dimension(200,200)); 
tracesDisplay.setPreferredSize(new Dimension(200,200)); 
textInfoAreaSP.setPreferredSize(new Dimension(200,100)); 

setPreferredSize(...)は使用しないでください。各Swingコンポーネントは、独自のサイズを決定します。

ImageIcon.getIconWith()およびgetIconHeight()を使用してイメージサイズを取得します。 JLabelの推奨サイズはこれらの寸法で更新されます。

不要です。ここでも、JLabelはアイコンのサイズに基づいて独自のサイズを決定します。これは、画像/アイコンが変化すると動的に行われます。

ラベルの優先サイズがスクロールペインのサイズより大きい場合、スクロールペインのスクロールバーが表示されます。レイアウトマネージャーに仕事をさせてください。

+0

VisualizerGUI.javaの5 "setPreferredSize"行をコメントアウトし、.classファイルを削除して再コンパイルしました。私はまだ同じ問題があります。私が最初に "Run"ボタンを押すと(最初に画像がない)、画像は中央に配置され、ScrollPaneは正しい設定をします。しかし、 "Run"ボタンをもう一度押すと、画像の左上の位置とScrollPaneの設定が間違っています。それらは、表示されなければならない以前の画像であるかのようにセットアップされます。 – Gus

関連する問題