0

RefreshablePanel拡張するクラスを作成しましたJPanel

public class RefreshablePanel extends JPanel {

static String description="";
static int x=10;
static int y=11;
    
protected void paintComponent(Graphics g){
       super.paintComponent(g);
    for (String line : description.split("\n"))
        g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
void updateDescription(String dataToAppend){
        description = description.concat("\n").concat(dataToAppend);
        System.out.println("The description is "+description);
   }   
}

そして、このようにGUI_classに追加しています

JScrollPane scrollPane_2 = new JScrollPane();
scrollPane_2.setBounds(32, 603, 889, 90);
frmToolToMigrate.getContentPane().add(scrollPane_2);

descriptionPanel = new RefreshablePanel();
scrollPane_2.setViewportView(descriptionPanel);
descriptionPanel.setBackground(Color.WHITE);
descriptionPanel.setLayout(null);
    

RefreshablePanel のインスタンスを作成しているクラスにスクロールバーを追加しましたが、スクロールバーが表示されません。追加してみました

@Override
public Dimension getPreferredSize() {
    return new Dimension(400, 1010);
}

リフレッシュ可能なパネルに移動しますが、文字列はこのように消えます ここに画像の説明を入力

下にスクロールしても何も表示されない

4

1 に答える 1

3
  1. nullレイアウトを使用しているため、何も機能しません。Swing は、レイアウト マネージャーを利用するように設計されています。

  2. 識別可能なサイズがありません。RefreshablePanelつまり、スクロール ペインに追加すると、スクロール ペインは単純にサイズが0x0. できればメソッドRefreshablePanelを介して、何らかのサイズのヒントをスクロールペインに戻す必要がありますgetPreferredSize

  3. JLabel(HTMLでラップされたテキストで)または編集不可JTextAreaを使用して、同じ結果を得ることができます

更新しました

コードを簡単に確認してください。xとのy値を宣言しており、メソッドで値をstaticインクリメントしていますypaintComponent

static int x=10;
static int y=11;

protected void paintComponent(Graphics g){
   super.paintComponent(g);
    for (String line : description.split("\n"))
        g.drawString(line, x, y += g.getFontMetrics().getHeight());
}

これは 2 つのことを意味します。

  1. 複数のインスタンスがある場合、RefreshablePanelそれらはすべて同じx/y値を共有し、それらを更新します
  2. yは常に新しい位置に更新されているため、パネルが 2 回ペイントされた場合、2 回目のペイントではy、最初の呼び出しが終了したときの最後の位置から位置が開始されます。

ペイント プロセスを制御できないことを忘れないでください。ペイント サイクルは、システムが必要と判断したときにいつでも実行できます...

x/値をメソッドyのローカル変数にしpaintComponentます...

更新しました

スクロール ペインは、使用可能なスペースが許せば、コンポーネントの推奨サイズに一致させようとします。これは、ウィンドウのサイズを変更するまでスクロールバーが表示されない可能性があることを意味する場合があります...しかし、nullレイアウトを使用しているため、うまくいきません...

スクロール ペインのサイズに影響を与えるには、Scrollable代わりにインターフェイスを使用できます...

ここに画像の説明を入力

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Scrollable01 {

    public static void main(String[] args) {
        new Scrollable01();
    }

    public Scrollable01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                RefreshablePanel pane = new RefreshablePanel();
                pane.updateDescription("1. You're using null layouts, so nothing is going to work the way it should. Swing is designed at the core to utilise layout managers.");
                pane.updateDescription("2. You're RefreshablePanel has no discernible size, meaning that when you add to the scroll pane, the scroll pane is likely to simply think it's size should 0x0. RefreshablePanel needs to provide some kind of size hint back to the scrollpane, preferably via the getPreferredSize method");
                pane.updateDescription("3. You could use a JLabel (with text wrapped in html) or a non-editable JTextArea to achieve the same results");

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(pane));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class RefreshablePanel extends JPanel implements Scrollable {

        public String description = "";

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 1010);
        }

        @Override
        protected void paintComponent(Graphics g) {
            int x = 10;
            int y = 11;
            super.paintComponent(g);
            for (String line : description.split("\n")) {
                g.drawString(line, x, y += g.getFontMetrics().getHeight());
            }
        }

        void updateDescription(String dataToAppend) {
            description = description.concat("\n").concat(dataToAppend);
            System.out.println("The description is " + description);
            repaint();
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(400, 400);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 64;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 64;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }
    }
}
于 2013-10-03T04:29:53.513 に答える