0

解決した質問: windowbuilderのクイック テスト/プレビュー ボタンと Eclipse のコンパイル ボタンを混同しました:

ここに画像の説明を入力 (「偶然に」マウスをボタンの上に置いて、コンパイルされていないことを確認することによってのみ発見されました)


元の質問

非常に具体的な問題があります。

私は2つの異なるクラスを持っています:

  • クラス 1 は、JFrame 内に JPanel を実装します。

  • クラス 2 は JFrame のみを実装します。

クラス 2 では、次のコードは完全に機能します。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class frameTest extends JFrame {

private JPanel contentPane;
private JTextField txtGeefLiefde;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frameTest frame = new frameTest();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public frameTest() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnTest = new JButton("press");

    btnTest.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            testFunction();
        }
    });
    btnTest.setBounds(162, 188, 89, 23);
    contentPane.add(btnTest);

}

public void testFunction()
{
    JTextPane textPane = new JTextPane();
    textPane.setBounds(162, 231, 89, 20);
    textPane.setText(":)");
    contentPane.add(textPane);
}

}

ここで、クラス 1 にもまったく同じ機能を実装したいと考えています。

私は次のことを試しました:

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

    public class frontpanel extends JFrame {

    private JPanel panel;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frontpanel frame = new frontpanel();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public frontpanel() {

            JButton btnTest = new JButton("press");

            btnTest.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e) {

                        testFunction();

                   }
            });
            btnTest.setBounds(162, 188, 89, 23);
            panel.add(btnTest);
    }
        public void testFunction()
        { 
        JTextPane textPane = new JTextPane();
        textPane.setBounds(162, 231, 89, 20);
        textPane.setText(":)");
        panel.add(textPane);
        }
    }

panelJFrame の JPanel はどこにありますか。

私はこれに何時間も立ち往生しています。私は、ANYを機能させることができないようですActionListener。問題の原因は何ですか?

すべてのヘルプは大歓迎です!

4

2 に答える 2

2

あなたのコードの私のテストケースに合うように以下のわずかに変更されたコードを与え、panel初期化を追加してTestフレームに追加します。

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;

public class Test extends JFrame {

  private JPanel panel = new JPanel();

/**
 * Launch the application.
 */
   public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
  public void run() {
    try {
          Test frame = new Test();
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the frame.
   */
  public Test() {
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    JButton btnTest = new JButton("press");

    btnTest.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

        testFunction();
      }
    });

    btnTest.setBounds(162, 188, 89, 23);
    panel.add(btnTest);
    this.setSize(new Dimension(300, 300));
    this.add(panel);
  }

  public void testFunction()
  { 
    JTextPane textPane = new JTextPane();
    textPane.setBounds(162, 231, 89, 20);
    textPane.setText(":)");
    panel.add(textPane);
  }
}
于 2013-08-21T20:02:00.863 に答える
2

問題は、あなたが初期化したことがなくpanel、また、jframe

    public FrontPanel() {

                JButton btnTest = new JButton("press");

                btnTest.addActionListener(new ActionListener() {
                       public void actionPerformed(ActionEvent e) {

                            testFunction();

                       }
                });
               // btnTest.setBounds(162, 188, 89, 23);  you should avoid using this
                panel = new JPanel();
                panel.add(btnTest);
                this.add(panel);
                this.pack();                                                 
        }

}

ところで、それほど重要ではありませんが、

1) Java Code Conventionsに従います。クラス名は大文字で始まります。

JFrame2)動作をオーバーライドしていない場合は、継承に対してコンポジションを使用する代わりに拡張しないでくださいJFrame

例:

public class FrontPanel{

  private JFrame frame;
  private JPanel panel;

}

3)setBoundsこのジョブを に委任する代わりに使用しないでくださいLayoutManagerレイアウト マネージャーの使用

于 2013-08-21T19:38:40.680 に答える