2016-06-07 2 views
0

저는 Java를 처음 사용하며 텍스트 필드와 라벨을 변경하는 데 두 개의 버튼을 사용하려고합니다. 문제는 ClearButton 클래스의 l이고 CopyButton 클래스의 tf는 참조 할 수 없다고 생각합니다. 어떻게해야합니까?라벨 또는 텍스트 필드가 변경되도록하기위한 스윙 버튼

public class SwingEx1 
{ 
    JFrame f; 
    JPanel p; 
    JLabel l; 
    JTextField tf; 
    JButton b1,b2; 

    public SwingEx1() 
    { 
     f = new JFrame("Swing Example"); 
     p = new JPanel(); 
     l = new JLabel("Initial Label"); 
     tf = new JTextField("Enter Text"); 
     b1 = new JButton("Clear"); 
     b2 = new JButton("Copy"); 
    } 

    public void LaunchFrame() 
    { 
     p.add(b1,BorderLayout.SOUTH); 
     p.add(b2,BorderLayout.SOUTH); 
     p.add(l,BorderLayout.CENTER); 
     p.add(tf,BorderLayout.CENTER); 
     f.getContentPane().add(p); 
     b1.addActionListener(new ClearButton()); 
     b2.addActionListener(new CopyButton()); 
     f.pack(); 
     f.setVisible(true); 
    } 

    public static void main(String args[]) 
    { 
     SwingEx1 swObj1 = new SwingEx1(); 
     swObj1.LaunchFrame(); 
    } 
} 

class ClearButton implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
    tf.setText(""); 
    } 
} 

class CopyButton implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     l.setText(tf.getText()); 
    } 
} 
+0

시도가'l.repaint를 사용하는()'l.setText'후()' – kaetzacoatl

+1

나 뛰어 뭔가, 난 그냥이 테스트했습니다. BorderLayout에 대해 동일한 섹션에 두 개의 객체를 가질 수 없습니다. 내 테스트의 결과는 두 번째 객체에 의해 덮어 쓰여진 첫 번째 객체입니다. 다른 말로하면 ... b1과 l은 b2와 tf에 의해 겹쳐 쓰여지고 있기 때문에 결코 나타나지 않을 것입니다. – Tyler

답변

1

범위 문제입니다. SwingEx1 클래스 내부에서 tf를 선언했기 때문에 외부 클래스는 존재하지 않는다는 것을 알 수 있습니다! 단추 클래스를 SwingEx1 클래스 내부로 이동하면 변수 이름을 확인할 수있게됩니다. 클래스 구조는 다음과 같을 것입니다 :

class SwingEx1 
{ 
    ... 
    class ClearButton implements ActionListener 
    { 
     ... 
    } 
    class CopyButton implements ActionListener 
    { 
     ... 
    } 
} 
1

단순히 클래스가 ActionListener를 구현하도록 할 수없는 이유가 있습니까? 그렇게하면 범위 지정 문제에 대해 걱정할 필요가 없습니다. 변수에 btnClear, btnCopy와 같은 의미있는 이름을 부여해야합니다.

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class SwingEx1 implements ActionListener { 

    JFrame f; 
    JPanel p; 
    JLabel l; 
    JTextField tf; 
    JButton b1, b2; 

    public SwingEx1() { 
     f = new JFrame("Swing Example"); 
     p = new JPanel(); 
     l = new JLabel("Initial Label"); 
     tf = new JTextField("Enter Text"); 
     b1 = new JButton("Clear"); 
     b2 = new JButton("Copy"); 
    } 

    public void LaunchFrame() { 
     p.add(b1, BorderLayout.SOUTH); 
     p.add(b2, BorderLayout.SOUTH); 
     p.add(l, BorderLayout.CENTER); 
     p.add(tf, BorderLayout.CENTER); 
     f.getContentPane().add(p); 
     b1.addActionListener(this); 
     b2.addActionListener(this); 
     f.pack(); 
     f.setVisible(true); 
    } 

    public static void main(String args[]) { 
     SwingEx1 swObj1 = new SwingEx1(); 
     swObj1.LaunchFrame(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == b1) { 
      tf.setText(""); 
     } else if (e.getSource() == b2) { 
      l.setText(tf.getText()); 
     } 
    } 
}