下記画像の、//**の部分ですがなぜ
setBounds(200, 200, 200, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
のような書き方ができるのでしょうか。
インスタンス化もしていませんし。クラス名もないですし。
Java
1package week9; 2 3import java.awt.event.ActionEvent; 4import java.awt.event.ActionListener; 5 6import javax.swing.JButton; 7import javax.swing.JFrame; 8import javax.swing.JLabel; 9import javax.swing.JPanel; 10 11public class DelegationTest extends JFrame implements ActionListener { 12 13 private static final long serialVersionUID = 1L; 14 15 JButton bt1; 16 JButton bt2; 17 JLabel label; 18 19 public DelegationTest() { 20 setBounds(200, 200, 200, 100);//** 21 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//** 22 label = new JLabel("クリックしてください", JLabel.CENTER); 23 add("North", label); 24 bt1 = new JButton("ボタン1"); 25 bt2 = new JButton("ボタン2"); 26 bt1.addActionListener(this); 27 bt2.addActionListener(this); 28 JPanel p = new JPanel(); 29 p.add(bt1); 30 p.add(bt2); 31 add("Center", p); 32 setVisible(true); 33 } 34 35 public static void main(String[] args) { 36 new DelegationTest(); 37 } 38 39 public void actionPerformed(ActionEvent e) { 40 if (e.getSource() == bt1) 41 label.setText("ボタン1"); 42 else if (e.getSource() == bt2) 43 label.setText("ボタン2"); 44 } 45}
回答3件
あなたの回答
tips
プレビュー