解決した質問: 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);
}
}
panel
JFrame の JPanel はどこにありますか。
私はこれに何時間も立ち往生しています。私は、ANYを機能させることができないようですActionListener
。問題の原因は何ですか?
すべてのヘルプは大歓迎です!