public class Test5 extends JFrame { JLabel mylabel; JButton mybutton;
public Test5() { super(); setTitle("Hello"); setSize(300,150); setLayout(null);
mylabel = new JLabel("Hello World."); mylabel.setBounds(50,50,200,30); this.add(mylabel);
mybutton = new JButton("OK"); mybutton.setBounds(100,100,100,25); this.add(mybutton);
mybutton.addActionListener(new ClickAction()); }
public static void main (String args []) { new Test5().setVisible(true); } class ClickAction implements ActionListener { public void actionPerformed(ActionEvent ev){ mylabel.setText("You are clicked!"); } } }
ここで「JLabel mylabel;」と「mylabel = new JLabel("Hello World.");」が別々に書かれているのは なぜなのでしょうか。
「JLabel mylabel;」を残したまま、 「mylabel = new Label("Hello World.");」の所を 「JLabel mylabel = new JLabel("Hello World.");」と 書くとエラーになるのはなぜなのかも教えてください。