3

私はSwingを初めて使用し、Eclipseで非常に基本的なイベント処理プログラムを作成していました。これが私が書いたコードです:

public class SwingDemo2 {

JLabel jl;

public SwingDemo2() {
    JFrame jfr = new JFrame("Swing Event Handling");
    jfr.setSize(250, 100);
    jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jl = new JLabel();
    jl.setVisible(false);

    JButton jb1 = new JButton("OK");
    jb1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jl.setText("You Pressed OK");
            jl.setVisible(true);
        }
    });

    JButton jb2 = new JButton("Reset");
    jb2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            jl.setText("You Pressed Reset");
            jl.setVisible(true);
        }
    });

    jfr.setLayout(new BorderLayout());
    jfr.add(jl, SwingConstants.NORTH);
    jfr.add(jb1, SwingConstants.EAST);
    jfr.add(jb2, SwingConstants.WEST);
    jfr.setVisible(true);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new SwingDemo2();
        }
    });
}

}

Eclipseは、デバッグパースペクティブを開くように促します。そこでエラーが表示されます。
Thread [AWT-EventQueue-0] (Suspended (exception IllegalArgumentException)) EventDispatchThread.run() line: not available [local variables unavailable]

FlowLayoutの代わりに使用してもエラーは発生しませんでしたBorderLayout

私はポータルでエラーに関する情報を見つけようとしていましたが、この同様の質問に出くわしました。答えは、問題を説明せずに一連の設定を変更することです(これも役に立ちませんでした)。繰り返さないようにエラーを説明してください。よろしくお願いします!

注:エラーメッセージを更新しました

4

2 に答える 2

4

使用してみてください

jfr.add(jl, BorderLayout.PAGE_START);
jfr.add(jb1, BorderLayout.LINE_START);
jfr.add(jb2, BorderLayout.LINE_END);

Javaドキュメントからの重要な注意:

Before JDK release 1.4, the preferred names for the various areas were different, 
ranging from points of the compass (for example, BorderLayout.NORTH for the 
top area) to wordier versions of the constants we use in our examples. 
The constants our examples use are preferred because they are standard and 
enable programs to adjust to languages that have different orientations.

BorderLayoutの詳細については、BorderLayoutの使用方法を参照してください。

なぜそのエラーが発生したのかを実際に知るために、コードにこれらの2行を記述しただけです。

System.out.println("SwingConstants.NORTH : " + SwingConstants.NORTH);
System.out.println("BorderLayout.PAGE_START : " + BorderLayout.PAGE_START);

以下のような出力が得られました:

SwingConstants.NORTH : 1
BorderLayout.PAGE_START : First

SwingContants上の値を使用する際の問題は何であるかを理解できる出力を見てくださいBorderLayout Constraints

于 2013-01-06T16:12:10.283 に答える
4
  • sのBorderLayout代わりに使用 、SwingConstantjfr.add(jl, BorderLayout.NORTH);

  • SwingConstantsのために実装されているのTextLayoutではなく、JComponents layout

于 2013-01-06T16:12:56.973 に答える