저는 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());
}
}
시도가'l.repaint를 사용하는()'l.setText'후()' – kaetzacoatl
나 뛰어 뭔가, 난 그냥이 테스트했습니다. BorderLayout에 대해 동일한 섹션에 두 개의 객체를 가질 수 없습니다. 내 테스트의 결과는 두 번째 객체에 의해 덮어 쓰여진 첫 번째 객체입니다. 다른 말로하면 ... b1과 l은 b2와 tf에 의해 겹쳐 쓰여지고 있기 때문에 결코 나타나지 않을 것입니다. – Tyler