首页上一页 1 下一页尾页 1 条记录 1/1页
动作事件监听
发表在Java视频课程答疑
2018-10-23 悬赏:1 学分
是否精华
是
否
版块置顶:
是
否
代码和视频老师一样啊,为啥我这里异常了?
import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//动作事件监听import javax.swing.*;public class Demo130 extends JFrame { public Demo130(){ setBounds(150, 150, 600, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); JPanel centerP =new JPanel(); centerP.setLayout(new FlowLayout()); centerP.add(centerP,BorderLayout.CENTER); JPanel southP =new JPanel(); JLabel console =new JLabel("点击组件"); southP.add(console); centerP.add(southP,BorderLayout.SOUTH); JButton btn=new JButton("按钮"); centerP.add(btn); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.setText("按钮被点击"); } }); JTextField jt=new JTextField(10); centerP.add(jt); jt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); JTextField jtTmp=(JTextField)obj; System.out.println(jtTmp.getText()); console.setText("文本框中点击了回车"); } });; JCheckBox jc=new JCheckBox("多选框"); centerP.add(jc); jc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.setText("多选框被点击"); } }); JRadioButton jr=new JRadioButton("单选框"); centerP.add(jr); jr.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.setText("单选框被点击"); } }); String valuse[]={"选项1","选项2","选项3"}; JComboBox jb=new JComboBox(valuse); centerP.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.setText("下拉列表被选择"); } }); centerP.validate();//重新验证一下容器中的组件 } public static void main(String[] args) { new Demo130(); }}
import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//动作事件监听import javax.swing.*;public class Demo130 extends JFrame { public Demo130(){ setBounds(150, 150, 600, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); JPanel centerP =new JPanel(); centerP.setLayout(new FlowLayout()); centerP.add(centerP,BorderLayout.CENTER); JPanel southP =new JPanel(); JLabel console =new JLabel("点击组件"); southP.add(console); centerP.add(southP,BorderLayout.SOUTH); JButton btn=new JButton("按钮"); centerP.add(btn); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.setText("按钮被点击"); } }); JTextField jt=new JTextField(10); centerP.add(jt); jt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); JTextField jtTmp=(JTextField)obj; System.out.println(jtTmp.getText()); console.setText("文本框中点击了回车"); } });; JCheckBox jc=new JCheckBox("多选框"); centerP.add(jc); jc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.setText("多选框被点击"); } }); JRadioButton jr=new JRadioButton("单选框"); centerP.add(jr); jr.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.setText("单选框被点击"); } }); String valuse[]={"选项1","选项2","选项3"}; JComboBox jb=new JComboBox(valuse); centerP.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { console.setText("下拉列表被选择"); } }); centerP.validate();//重新验证一下容器中的组件 } public static void main(String[] args) { new Demo130(); }}