Swing组件都采用MVC设计模式

Swing组件都采用MVC(Model-View-Controller,既模型-视图-控制器)设计模式,从而可以实现GUI组件的显示逻辑和数据逻辑的分离,允许程序员自定义Render来改变GUI组件的显示外观,提供更多的灵活性。

  1 package Com.SwingTest;
  2 
  3 
  4 
  5 import javax.swing.*;
  6 import java.awt.*;
  7 import java.awt.event.ActionEvent;
  8 import java.awt.event.ActionListener;
  9 import java.awt.event.KeyEvent;
 10 import java.net.*;
 11  
 12 public class SwingText_10 extends JFrame {
 13     public SwingText_10() {
 14         setTitle("游戏宝典");
 15         setBounds(650, 350, 665, 445);
 16         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 17         Container container = getContentPane();
 18         // setLayout(null);
 19         setLayout(new BorderLayout());
 20         JPanel p1 = new JPanel();
 21         JPanel p2 = new JPanel();
 22         JLabel bxx = new JLabel("borter right@");
 23         JButton bx1 = new JButton("出装");
 24         JButton bx2 = new JButton("铭文");
 25         JButton bx3 = new JButton("打法");
 26         p2.add(bx1);
 27         p2.add(bx2);
 28         p2.add(bx3);
 29         p1.add(bxx);
 30         this.add(BorderLayout.SOUTH, p1);
 31         bxx.setBackground(Color.white);
 32         Icon i = new ImageIcon("2.jpg");
 33         JLabel l = new JLabel();
 34         l.setIcon(i);
 35         container.add(l);
 36         JMenuBar m = new JMenuBar();
 37         setJMenuBar(m);
 38         // 英雄类型
 39         JMenu m1 = new JMenu("刺客(A)");
 40         JMenu m2 = new JMenu("战士(B)");
 41         JMenu m3 = new JMenu("射手(C)");
 42         JMenu m4 = new JMenu("法师(D)");
 43         JMenu m5 = new JMenu("辅助(E)");
 44         JMenu m6 = new JMenu("坦克(F)");
 45         // 英雄单位
 46         JMenu n1 = new JMenu("那可露露(O)");
 47         JMenu n2 = new JMenu("李白(P)");
 48         JMenu n3 = new JMenu("橘右京(Q)");
 49         JMenu n4 = new JMenu("关羽(O)");
 50         JMenu n5 = new JMenu("赵玉(P)");
 51         JMenu n6 = new JMenu("刘备(Q)");
 52         // 关羽攻略
 53         JMenuItem n31 = new JMenuItem("出装(U)");
 54         JMenuItem n32 = new JMenuItem("铭文(V)");
 55         JMenuItem n33 = new JMenuItem("打法(W)");
 56         // 李白攻略
 57         JMenuItem n21 = new JMenuItem("人生(U)");
 58         JMenuItem n22 = new JMenuItem("技能(V)");
 59         JMenuItem n23 = new JMenuItem("打法(W)");
 60         JMenuItem n24 = new JMenuItem("出装(X)");
 61         JMenuItem n25 = new JMenuItem("铭文(Y)");
 62         JMenuItem n26 = new JMenuItem("选手(Z)");
 63  
 64         // 绑定快捷键
 65         n21.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, ActionEvent.CTRL_MASK));
 66         n22.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
 67         n23.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
 68         n24.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
 69         n25.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
 70         n26.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
 71  
 72         // 英雄类型
 73         m.add(m1);
 74         m.add(m2);
 75         m.add(m3);
 76         m.add(m4);
 77         m.add(m5);
 78         m.add(m6);
 79         // 刺客英雄
 80         m1.add(n1);
 81         m1.add(n2);
 82         m1.add(n3);
 83         // 战士英雄
 84         m2.add(n4);
 85         m2.add(n5);
 86         m2.add(n6);
 87         // 关羽攻略
 88         n4.add(n31);
 89         n4.add(n32);
 90         n4.add(n33);
 91         // 李白攻略
 92         n2.add(n21);
 93         n2.add(n22);
 94         n2.add(n23);
 95         n2.add(n24);
 96         n2.add(n25);
 97         n2.add(n26);
 98         // --菜单栏->战士->关羽->出装
 99         n31.addActionListener(new ActionListener() {
100             @Override
101             public void actionPerformed(ActionEvent arg0) {
102                 // TODO Auto-generated method stub 铭文
103                 Icon i1 = new ImageIcon("src/关羽经典.png");
104                 JLabel l1 = new JLabel();
105                 l1.setIcon(i1);
106                 Icon i2 = new ImageIcon("src/关羽出装.png");
107                 JLabel l2 = new JLabel();
108                 l2.setIcon(i2);
109                 JDialog dialog = new JDialog();// 创建当前窗体的对话框
110                 dialog.setLayout(new GridLayout(2, 1, 5, 5));
111                 dialog.add(l1);
112                 dialog.add(l2);
113                 dialog.setModal(true);// 设置对话框为模态
114                 dialog.setSize(354, 200);// 设置对话框大小
115                 dialog.setLocationByPlatform(true);// 由系统平台布置窗体位置
116                 dialog.setTitle("关羽出装推荐");// 对话框标题
117                 dialog.setVisible(true);// 显示对话框
118             }
119         });
120         // --菜单栏->战士->关羽->铭文
121         n32.addActionListener(new ActionListener() {
122             @Override
123             public void actionPerformed(ActionEvent arg0) {
124                 // TODO Auto-generated method stub 铭文
125                 Icon i1 = new ImageIcon("src/na.jpg");
126                 JLabel l1 = new JLabel();
127                 l1.setIcon(i1);
128                 Icon i2 = new ImageIcon("src/122.png");
129                 JLabel l2 = new JLabel();
130                 l2.setIcon(i2);
131                 JDialog dialog = new JDialog();// 创建当前窗体的对话框
132                 dialog.setLayout(new GridLayout(2, 1, 5, 5));
133                 dialog.add(l1);
134                 dialog.add(l2);
135                 // dialog.setBackground(Color.gray);
136                 dialog.setModal(true);// 设置对话框为模态
137                 dialog.setSize(500, 400);// 设置对话框大小
138                 dialog.setLocationByPlatform(true);// 由系统平台布置窗体位置
139                 dialog.setTitle("铭文推荐");// 对话框标题
140                 dialog.setVisible(true);// 显示对话框
141             }
142         });
143         // --菜单栏->战士->关羽->打法
144         n33.addActionListener(new ActionListener() {
145             @Override
146             public void actionPerformed(ActionEvent arg0) {
147                 // TODO Auto-generated method stub 铭文
148                 Icon i1 = new ImageIcon("2.jpg");
149                 JLabel l1 = new JLabel();
150                 l1.setIcon(i1);
151                 Icon i2 = new ImageIcon("2.jpg");
152                 JLabel l2 = new JLabel();
153                 l2.setIcon(i2);
154                 JDialog dialog = new JDialog();// 创建当前窗体的对话框
155                 dialog.setLayout(new GridLayout(2, 1, 10, 10));
156                 dialog.add(l1);
157                 dialog.add(l2);
158                 dialog.setModal(true);// 设置对话框为模态
159                 dialog.setSize(500, 400);// 设置对话框大小
160                 dialog.setLocationByPlatform(true);// 由系统平台布置窗体位置
161                 dialog.setTitle("铭文推荐");// 对话框标题
162                 dialog.setVisible(true);// 显示对话框
163             }
164         });
165  
166         setVisible(true);
167     }
168  
169     public static void main(String[] args) {
170         new SwingText_10();
171     }
172 }

 

转载于:https://www.cnblogs.com/borter/p/9397536.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值