1
これは私のコードであり、実行すると空白のウィンドウが表示されます。私はmain.setContentPane()
を行うにしようとしたときに最初に私はこのエラーを得た:スイングコンポーネントが表示されない
"Cannot make a static reference to the non-static field panel"
だから私は、コンストラクタでそれを置くが、何も表示されないでしょう。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame{
private JPanel panel;
private JButton performance;
private JButton concordance;
private JButton discordance;
private JButton resultat;
public void MainFrame() {
panel = new JPanel(new GridLayout(4, 1, 10, 10));
performance = new JButton("performance");
concordance = new JButton("concordance");
discordance = new JButton("discordance");
resultat = new JButton("resultat");
performance.setSize(50, 30);
concordance.setSize(50, 30);
discordance.setSize(50, 30);
resultat.setSize(50, 30);
panel.add(performance);
panel.add(concordance);
panel.add(discordance);
panel.add(resultat);
getContentPane().add(panel);
}
public static void main(String[] args) {
MainFrame main = new MainFrame();
main.setSize(300, 200);
main.setDefaultCloseOperation(EXIT_ON_CLOSE);
main.setVisible(true);
}
}
うわー、私は完全にそれを無効とすることを逃したが、私はそれがコンストラクタだと思ったし、それの上に目をやりました(テストされ、コンストラクタとして期待どおりに動作します) – phflack
心配なし!それは起こる。ときには外の目を見てみるのも良いでしょう:) – richjavadev