Swing超基础学习总结——4、CardLayout与ActionListener
2016-08-28 16:01
429 查看
使用CardLayout制作切换卡
案例代码
CardLayout详细说明
切换卡主要方法
ActionListener详细说明
使用方法
案例代码统计点击次数
给某一容器设置卡片布局后,添加每一个组件都当做是一页,第一个组件即第一页,注意这里是一个组件(也可是一个容器)为一页
方法参数为执行切换卡的容器对象
①注册监听:
②绑定监听:
方法2:
①实现ActionListener,重写方法
②绑定监听
注:第一个案例中用了很多static,静态变量是很浪费内存的,但是由于main方法为静态所以全局变量只能用静态变量,因此大部分情况下还是比较适合用第二个案例中的方式。
关于swing的学习还剩下菜单和列表,加油!!
案例代码
CardLayout详细说明
切换卡主要方法
ActionListener详细说明
使用方法
案例代码统计点击次数
使用CardLayout制作切换卡
案例代码:
public class CardLayoutAndListenerTest { /** * @param args */ static JButton preButton; static JButton nextButton; static Panel controlPanel; static Panel cardPanel; static CardLayout c; public static void main(String[] args) { controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); cardPanel = new Panel(); cardPanel.setLayout(new FlowLayout()); c = new CardLayout(); JFrame frame = new JFrame(); frame.setSize(300, 200); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); cardPanel.setLayout(c); for (int i = 0; i < 5; i++) { // 按顺序添加页,第一个添加在最上面,依次类推 cardPanel.add(new JButton("按钮" + i)); } preButton = new JButton("前一张"); nextButton = new JButton("后一张"); //绑定监听 preButton.addActionListener(actionListener); nextButton.addActionListener(actionListener); controlPanel.add(preButton); controlPanel.add(nextButton); frame.add(cardPanel, BorderLayout.CENTER); frame.add(controlPanel, BorderLayout.SOUTH); } //注册监听 static ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //对应某个按钮的变量名 if (e.getSource() == preButton) { // 打开前一页 c.previous(cardPanel); } if (e.getSource() == nextButton) { // 打开后一页 c.next(cardPanel); } } }; }
CardLayout详细说明
卡片布局,切换卡片,即局部刷新给某一容器设置卡片布局后,添加每一个组件都当做是一页,第一个组件即第一页,注意这里是一个组件(也可是一个容器)为一页
“切换卡”主要方法
使用卡片布局的对象执行next方法(后一页)或previous方法(前一页)方法参数为执行切换卡的容器对象
ActionListener详细说明
使用方法
方法1:①注册监听:
ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ...... } };
②绑定监听:
组件对象.addActionListener(actionListener);
方法2:
①实现ActionListener,重写方法
@Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == button) { …… } }
②绑定监听
组件对象.addActionListener(this);
案例代码(统计点击次数):
上述使用方法1已经在CardLayout的代码中展示了,接下的案例是用使用方法2来现实的。public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ActionFrame actionFrame = new ActionFrame("ActionListener实例"); } } class ActionFrame extends JFrame implements ActionListener { JButton button; int i = 0; JLabel jLabel; public ActionFrame(String title) { // TODO Auto-generated constructor stub setSize(300, 200); setTitle(title); setLayout(new BorderLayout()); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setVisible(true); jLabel = new JLabel("点击次数:0", JLabel.CENTER); // 设置JLable中的文字居中 // jLabel.setHorizontalAlignment(JLabel.CENTER); button = new JButton("点击"); button.addActionListener(this); Container container = getContentPane(); container.add(jLabel, BorderLayout.CENTER); container.add(button, BorderLayout.SOUTH); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == button) { i++; jLabel.setText("点击次数:" + i); } } }
注:第一个案例中用了很多static,静态变量是很浪费内存的,但是由于main方法为静态所以全局变量只能用静态变量,因此大部分情况下还是比较适合用第二个案例中的方式。
关于swing的学习还剩下菜单和列表,加油!!
相关文章推荐
- Bmob数据库查询结果为null
- 表单的简单使用
- [Lintcode] Combination Sum II
- MVC NPOI Linq导出Excel通用类
- 数据库原理-数据库系统的结构
- Loadrunner:集合点(Rendezvous)
- 36个Android开发常用代码片段
- JS一维数组、多维数组和对象的混合使用
- 浅谈Java中的深拷贝和浅拷贝
- 1028. List Sorting (25)
- 回溯法案列总结
- ATSHA204A I2C Linux驱动
- J2SE第三章面向对象设计思想
- 第四章 Hibernate入门
- 正则表达式^\w+$
- eclipse 有的地方打不上断点,设置的断点有斜杠
- 在Lua 5.2中保护全局环境
- JSP学习笔记---page指令
- leetcode :Binary Search:Pow(x, n)(050)
- JS判断是否在微信浏览器打开