4

setRollover(true) を使用すると、Swing ツールバーのボタンは境界線なしでフラットになり、境界線はボタンをホバリングまたは押すときにのみ描画されます。ただし、ボタンが最初にパネルに追加され、次にパネルがツールバーに追加された場合、これは機能しません。それを達成する簡単な方法はありますか?

ボタンを JPanel に配置して、単一のコンポーネントとして機能させたいと考えています (最初/前/次/最後のページ ボタンを備えたページング コンポーネントを想像してください)。また、L&F に関係なく動作するようにしたい (JPanel がツールバーとボタンの間にない場合と同様)。

編集:

次の例で、ボタン 1 と 2 (直接追加) とボタン 3 と 4 (JPanel 経由で追加) を比較します。

import javax.swing.*;

public class ToolbarTest extends JFrame {
    ToolbarTest() {
        JToolBar toolbar = new JToolBar();
        toolbar.setRollover(true);

        JButton button = new JButton("One");
        button.setFocusable(false);
        toolbar.add(button);

        button = new JButton("Two");
        button.setFocusable(false);
        toolbar.add(button);

        JPanel panel = new JPanel();
        button = new JButton("Three");
        button.setFocusable(false);
        panel.add(button);

        button = new JButton("Four");
        button.setFocusable(false);
        panel.add(button);

        toolbar.add(panel);

        add(toolbar);
        pack();
    }

    public static void main(String[] args) throws Throwable {
        // optional: set look and feel (some lf might ignore the rollover property)
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {      // or "Windows", "Motif"
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }

        ToolbarTest frame = new ToolbarTest();
        frame.setVisible(true);
    }
}

スクリーンショットは次のとおりです。

Nimbus LF のツールバー:

Nimbus LF のツールバー

マウスを 2 番目のボタンの上に置いたときの同じツールバー (マウス カーソルは表示されません):

ホバリング・ニンバス

Windows LF の同じツールバー:

Windows LF の同じツールバー

3 つボタンと 4 つボタンを 1 つボタンと 2 つボタンと同じように機能させたいと思います。

4

2 に答える 2

1

JPanel の代わりに別の JToolbar を使用すると機能します。

しかし、複合コンポーネントをダイアログまたはツールバー以外の何かに含めたい場合、おそらく問題が発生するでしょう(または発生しない可能性があります)。(これは、ツールバー用と残り用の 2 種類のボタンを持つことに似ています)

これは、ツールバーを追加するように変更されたパネルを追加する質問のコードの一部です。

JToolBar component = new JToolBar();
component.setRollover(true);
component.setBorder(null);
component.setFloatable(false);

button = new JButton("Three");
button.setFocusable(false);
component.add(button);

button = new JButton("Four");
button.setFocusable(false);
component.add(button);

toolbar.add(component);
于 2012-03-14T20:04:02.393 に答える
1

1) JMenuBar をではなくコンテナとして設定することをお勧めしますJToolbar

欠点:

  • 動かしたり取り外したりすることはできません。Container

  • どこにでも配置できますが、コンテナー内にのみ配置できますJComponentLayoutManager


2)コード例からわかるように、ネストされた別のJToolBar場所に配置する方が良いでしょうJPanelJComponents


3) コード例ではJButton、4 回定義します。Java では、個別に定義する必要があります。Objects

于 2012-03-14T16:54:35.550 に答える