2

さて、オーバーレイ レイアウトを使用して、下の画像で白く表示されている Jpanel があります。これは、画像 ("No Image Available") と JButton ("Comment") を保持する ScrollPane を保持します。

ここに画像の説明を入力

このボタンを JPanel の右下隅に配置します。複数のレイアウトアプローチを試しましたが、うまくいかないようです。ボタンはせいぜい南東方向に 3/4 移動するだけで、その理由はわかりません。

どんな助けでも大歓迎です..

4

6 に答える 6

4

OverlayLayout;を使用して言及しました ボタンを実際に画像に重ねますか?

それが重要でない場合は、他の優れた提案のいずれかを使用してください。それらははるかに単純です。しかし、どうしてもボタンを画像に重ねて表示したい場合は、1 つの解決策があります。 a を使用しJLayeredPaneて 2 つJPanelの を重ね合わせて、ボタンと画像を配置します。残念ながら、 にJLayeredPaneはレイアウト マネージャーがないため、のサイズが変更さJPanelれるたびに のサイズを変更するには、コンポーネント リスナーを追加する必要JLayeredPaneがあります。

SSCCE は次のとおりです。

public class SSCCE {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout());

        final JLayeredPane layeredPane = new JLayeredPane();
        contentPane.add(layeredPane,BorderLayout.CENTER);

        final JPanel btnPane = new JPanel(new GridBagLayout());
        btnPane.setOpaque(false);

        JButton btn = new JButton("Comment");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.SOUTHEAST;
        btnPane.add(btn,gbc);

        final JPanel lblPane = new JPanel(new GridBagLayout());
        lblPane.setBackground(Color.CYAN);

        JLabel lbl = new JLabel("No Image Available");
        gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.CENTER;
        lblPane.add(lbl,gbc);

        layeredPane.add(btnPane,0);
        layeredPane.add(lblPane,1);
        layeredPane.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                lblPane.setBounds(0, 0, layeredPane.getWidth(), layeredPane.getHeight());
                btnPane.setBounds(0, 0, layeredPane.getWidth(), layeredPane.getHeight());
            }
        });


        frame.setSize(300,200);
        frame.setVisible(true);
    }

}
于 2012-05-24T17:30:01.973 に答える
3

さまざまなレイアウト マネージャーを使用することで、さまざまな解決策が考えられます。OverlayLayout はわかりませんが、WindowBuilder Pro (無料): https://developers.google.com/java-dev-tools/wbpro/が好きで、Swing の設計に役立ちます。

それを使用して、質問の SpringLayout 実装を作成しました (SpringLayout は、GUI ビルダーなしで処理するのが面倒なようです)。

JPanel panel = new JPanel();
SpringLayout sl_panel = new SpringLayout();
panel.setLayout(sl_panel);

JButton button = new JButton("Comments");
sl_panel.putConstraint(SpringLayout.SOUTH, button, 0, SpringLayout.SOUTH, panel);
sl_panel.putConstraint(SpringLayout.EAST, button, 0, SpringLayout.EAST, panel);
panel.add(button);

JScrollPane scrollPane = new JScrollPane();
sl_panel.putConstraint(SpringLayout.NORTH, scrollPane, 5, SpringLayout.NORTH, panel);
sl_panel.putConstraint(SpringLayout.WEST, scrollPane, 3, SpringLayout.WEST, panel);
sl_panel.putConstraint(SpringLayout.SOUTH, scrollPane, 3, SpringLayout.SOUTH, panel);
sl_panel.putConstraint(SpringLayout.EAST, scrollPane, 3, SpringLayout.EAST, panel);
panel.add(scrollPane);

JLabel lblNewLabel = new JLabel();
lblNewLabel.setIcon(new ImageIcon(foo.class.getResource("sSdA3.png")));
scrollPane.setViewportView(lblNewLabel);

実行中のコードの写真は次のとおりです。

ここに画像の説明を入力

ボタン(あなたの写真ではなく私のものです...)が下部のスクロールペインの上に浮かんでいるのがわかります。上記の余白を調整して、ボタンがスクロール バーの上に浮かないようにすることもできますが、これはボタンが z 軸上のどこにあるかを示すためのものです。

于 2012-05-24T16:54:17.110 に答える
3

最初のJPanelwith image をCENTERコンテンツ ペインの位置に追加し、JPanel のレイアウトを に設定してFlowLayout.RIGHT追加します。JButtonこれJPanelをコンテンツ ペインのPAGE_END位置に追加しBorderLayoutます。この例を見てください

import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ApplicationCloseExample
{   
    private Image image;
    private static final String HTML =
        "<html>" +
        "<style type'text/css'>" +
        "body, html { padding: 0px; margin: 0px; }" +
        "</style>" +
        "<body>" +
        "<img src='http://pscode.org/media/starzoom-thumb.gif'" +
        " width=320 height=240>" +
        "";

    private void displayGUI()
    {
        final JFrame frame = new JFrame("Application Close Example");

        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                int result = JOptionPane.showConfirmDialog(
                                frame, "Do you want to Exit ?"
                                , "Exit Confirmation : ", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)               
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                else if (result == JOptionPane.NO_OPTION)   
                    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            }
        });

        JPanel contentPane = new JPanel();
        contentPane.setOpaque(true);
        JLabel label = new JLabel(HTML);
        contentPane.add(label);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
        JButton button = new JButton("Comment");
        bottomPanel.add(button);

        frame.getContentPane().add(contentPane, BorderLayout.CENTER);
        frame.getContentPane().add(bottomPanel, BorderLayout.PAGE_END);

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

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ApplicationCloseExample().displayGUI();
            }
        });
    }
}

出力は次のとおりです。

右ボタン

于 2012-05-24T17:14:23.823 に答える
1

位置にボタンを追加してJPanelと言い、そのパネルをメイン コンテナー ( ) の位置に配置します。BorderLayoutEASTBorderLayoutSOUTH

したがって、次のようになります。

-------------
|           |
|           |
|   Image   |
|           |
|-----------|
|______Button
于 2012-05-24T16:38:18.223 に答える
1

GridBagLayoutデフォルトのレイアウトの代わりに使用することをお勧めします。GridBagLayout を使用すると、すべてを制御できます。
ここに役立つリンクがあります:GridBagLayoutの使用方法

于 2012-05-24T16:35:28.180 に答える
1

SpringLayoutを確認してください。コンポーネントの NORTH、WEST、EAST、または SOUTH から特定の距離に要素を配置できます。コードは次のようになります。

SpringLayout layout = new SpringLayout();
setLayout(layout);
...
add(_button);
...
layout.putConstraint(SpringLayout.EAST, _button, -20, SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.SOUTH, _button, -20, SpringLayout.SOUTH, this);
于 2012-05-24T17:02:28.870 に答える