- Java code
import javax.swing.*;import java.awt.*;import javax.swing.event.*;import java.awt.event.*;public class Ziti extends JFrame implements ActionListener{ JPanel rongqi,mianban1,mianban2; JLabel biaoqian1,biaoqian2; JButton anniu1; public Ziti() { super("字体设置"); anniu1=new JButton("按钮"); biaoqian1=new JLabel("请选择字体"); biaoqian2=new JLabel("请选择大小"); rongqi= new JPanel(); rongqi.add(biaoqian1); rongqi.add(anniu1); rongqi.add(biaoqian2); this.setContentPane(rongqi); this.setLocationRelativeTo(null); setVisible(true); setSize(250,200); setDefaultCloseOperation(Ziti.EXIT_ON_CLOSE); } public static void main(String args[]){ new Ziti(); } public void actionPerformed(ActionEvent ae){ if (ae.getSource()==anniu1){ biaoqian1.setText("hello"); } }}
按按钮后liaoqian1的内容不变 为啥捏?
------解决方案--------------------
没有下面这个
- Java code
anniu1.addActionListener(this);