📄 testcontrolframe.java
字号:
package com.xmu.typot.test;import javax.swing.WindowConstants;import com.xmu.typot.domain.MonopolyGame;import com.xmu.typot.domain.Player;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;/** * 测试类 * @author typot * 2007.12.26 */public class TestControlFrame extends javax.swing.JFrame implements TestListener{ /** * Auto-generated main method to display this JFrame */ private MonopolyGame control; private JPanel PlayerPanel; private JLabel jLabel4; private JLabel jLabel5; private JButton changeButton; private JTextField diceText; private JButton button4; private JTextField textField4; private JLabel name4; private JButton button3; private JTextField textField3; private JLabel name2; private JLabel image4; private JLabel jLabel2; private JLabel image1; private JLabel image3; private JLabel name3; private JButton button2; private JTextField jTextField2; private JLabel image2; private JButton button1; private JTextField jTextField1; private JLabel name1; private JLabel jLabel3; private JLabel jLabel1; private JPanel dicePanel; private JLabel[] images = new JLabel[4]; private JTextField[] moneys = new JTextField[4]; private JLabel[] names = new JLabel[4]; public static void main(String[] args) { TestControlFrame inst = new TestControlFrame(); inst.setVisible(true); } public TestControlFrame() { super(); initGUI(); } private void initGUI() { try { this.setTitle("Monopoly test"); BorderLayout thisLayout = new BorderLayout(); getContentPane().setLayout(thisLayout); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { PlayerPanel = new JPanel(); getContentPane().add(PlayerPanel, BorderLayout.NORTH); GridBagLayout PlayerPanelLayout = new GridBagLayout(); PlayerPanelLayout.rowWeights = new double[] { 0.1, 0.1, 0.1, 0.1, 0.1 }; PlayerPanelLayout.rowHeights = new int[] { 7, 20, 7, 7, 7 }; PlayerPanelLayout.columnWeights = new double[] { 0.1, 0.1, 0.1, 0.1 }; PlayerPanelLayout.columnWidths = new int[] { 7, 7, 7, 7 }; PlayerPanel.setLayout(PlayerPanelLayout); PlayerPanel.setPreferredSize(new java.awt.Dimension(304, 269)); { jLabel1 = new JLabel(); PlayerPanel.add(jLabel1, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jLabel1.setText("\u5934\u50cf"); } { jLabel2 = new JLabel(); PlayerPanel.add(jLabel2, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jLabel2.setText("\u540d\u5b57"); } { jLabel3 = new JLabel(); PlayerPanel.add(jLabel3, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jLabel3.setText("\u91d1\u94b1"); } { image1 = new JLabel(); PlayerPanel.add(image1, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); // image1.setIcon(new ImageIcon(getClass().getClassLoader() // .getResource("image/1player.gif"))); images[0] = image1; } { name1 = new JLabel(); PlayerPanel.add(name1, new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); // name1.setText("1p"); names[0] = name1; } { jTextField1 = new JTextField(); PlayerPanel.add(jTextField1, new GridBagConstraints( 2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); moneys[0] = jTextField1; } { button1 = new JButton(); PlayerPanel.add(button1, new GridBagConstraints( 3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); button1.setText("\u4fee\u6539"); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub changeMonye(0,moneys[0].getText()); } }); } { image2 = new JLabel(); PlayerPanel.add(image2, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); // image2.setIcon(new ImageIcon(getClass().getClassLoader() // .getResource("image/3player.gif"))); images[1] = image2; } { jTextField2 = new JTextField(); PlayerPanel.add(jTextField2, new GridBagConstraints( 2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); moneys[1] = jTextField2; } { button2 = new JButton(); PlayerPanel.add(button2, new GridBagConstraints( 3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); button2.setText("\u4fee\u6539"); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub changeMonye(1,moneys[1].getText()); } }); } { image3 = new JLabel(); PlayerPanel.add(image3, new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); // image3.setIcon(new ImageIcon(getClass().getClassLoader() // .getResource("image/10player.gif"))); images[2] = image3; } { name3 = new JLabel(); PlayerPanel.add(name3, new GridBagConstraints( 1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); // name3.setText("3p"); names[2] = name3; } { image4 = new JLabel(); PlayerPanel.add(image4, new GridBagConstraints( 0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); // image4.setIcon(new ImageIcon(getClass().getClassLoader() // .getResource("image/9player.gif"))); images[3] = image4; } { name2 = new JLabel(); PlayerPanel.add(name2, new GridBagConstraints( 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); // name2.setText("2p"); names[1] = name2; } { textField3 = new JTextField(); PlayerPanel.add(textField3, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); moneys[2] = textField3; } { button3 = new JButton(); PlayerPanel.add(button3, new GridBagConstraints( 3, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); button3.setText("\u4fee\u6539"); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub changeMonye(2,moneys[2].getText()); } }); } { name4 = new JLabel(); PlayerPanel.add(name4, new GridBagConstraints( 1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); // name4.setText("4p"); names[3] = name4; } { textField4 = new JTextField(); PlayerPanel.add(textField4, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); moneys[3] = textField4; } { button4 = new JButton(); PlayerPanel.add(button4, new GridBagConstraints(3, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); button4.setText("\u4fee\u6539"); button4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub changeMonye(3,moneys[3].getText()); } }); } { jLabel5 = new JLabel(); PlayerPanel.add(jLabel5, new GridBagConstraints( 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); } } { dicePanel = new JPanel(); getContentPane().add(dicePanel, BorderLayout.SOUTH); GridBagLayout dicePanelLayout = new GridBagLayout(); dicePanelLayout.rowWeights = new double[] { 0.1 }; dicePanelLayout.rowHeights = new int[] { 7 }; dicePanelLayout.columnWeights = new double[] {0.0, 0.0, 0.1}; dicePanelLayout.columnWidths = new int[] {119, 88, 7}; dicePanel.setLayout(dicePanelLayout); dicePanel.setPreferredSize(new java.awt.Dimension(304, 28)); { jLabel4 = new JLabel(); dicePanel.add(jLabel4, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); jLabel4.setText("\u9ab0\u5b50"); } { diceText = new JTextField(); dicePanel.add(diceText, new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); } { changeButton = new JButton(); dicePanel.add(changeButton, new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); changeButton.setText("\u4fee\u6539"); changeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub if(diceText.getText().equals("")) { JOptionPane.showMessageDialog(null,"你输入的值不能为空"); return; } int dice = Integer.parseInt(diceText.getText()); if(dice > 6 || dice < 1) { JOptionPane.showMessageDialog(null,"你输入的值有误"); return; } control.dice(dice); } }); } } pack(); this.setSize(312, 353); } catch (Exception e) { e.printStackTrace(); } } protected void changeMonye(int i, String text) { // TODO Auto-generated method stub if(i >= control.getPLAYER_TOTAL()) { JOptionPane.showMessageDialog(null, "没有此玩家"); return; } int money = Integer.parseInt(text); Player[] player = control.getPlayer(); player[i].setMoney(money); control.repaint(); } public TestControlFrame(MonopolyGame game) { super(); control = game; initGUI(); } public void NoticeChange() { // TODO Auto-generated method stub this.rePut(); } private void rePut() { // TODO Auto-generated method stub Player[] player = control.getPlayer(); if(player == null) return; for(int i = 0; i < 4; i ++) { images[i].setIcon(null); names[i].setText(""); moneys[i].setText(""); } for(int i = 0; i < player.length; i ++) { images[i].setIcon(new ImageIcon(getClass().getClassLoader() .getResource("image/"+player[i].getIndex()+"player.gif"))); names[i].setText(player[i].getName()); Integer money = player[i].getMoney(); moneys[i].setText(money.toString()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -