mapmaker.java

来自「主要是对于JAVA的编程的基本语言 希望能够帮得上你。」· Java 代码 · 共 184 行

JAVA
184
字号
package swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class MapMaker extends JFrame implements ActionListener {

	/**
	 * 常量、变量定义部分
	 */
	
	static MapMaker mg;
	JPanel backPanel, centerPanel, bottomPanel;
	JButton btnOK,btnRandom,btnExit;	
	JScrollPane scrollPane;
	JTextArea txtMap;	
	JLabel lblRow, lblCol,lblMaxNumber;
	StringBuffer buffer;		
	JComboBox rowList, colList,maxList;	
	Random random;	

	int ROW, COL;
	int maxIndex;

	public static void main(String arg[]) {
		mg=new MapMaker("地图数组生成器V1.0_华卫_2008年4月11日");
	}
	
	public void setRow(int row){
		ROW=row;
	}
	
	public void setCol(int col){
		COL=col;
	}
	
	public void initialize(){
		/**
		 * 设计地图生成器界面 *
		 */
		if(ROW==0){
			ROW=20;
		}
		
		if(COL==0){
			COL=20;
		}	
		
		backPanel = new JPanel(new BorderLayout());
		centerPanel=new JPanel(new GridLayout(1,1));				
		bottomPanel = new JPanel();		
		rowList = new JComboBox();
		for(int i=3; i<=50;i++){
			rowList.addItem(String.valueOf(i));
		}
		colList = new JComboBox();
		for(int i=3; i<=50;i++){
			colList.addItem(String.valueOf(i));
		}
		maxList=new JComboBox();
		for(int i=1;i<=900;i++){
			maxList.addItem(String.valueOf(i));
		}
		rowList.setSelectedIndex(ROW-3);
		colList.setSelectedIndex(COL-3);		
		maxList.setSelectedIndex(0);
		btnOK=new JButton("确定");
		
		maxIndex=Integer.parseInt(maxList.getSelectedItem().toString());
		
		random=new Random();
		btnRandom = new JButton("生成地图数组");		
		btnExit=new JButton("退出");			
		lblRow=new JLabel("设置行数:");
		lblCol=new JLabel("设置列数:");	
		lblMaxNumber=new JLabel("设置最大数字:");
		txtMap=new JTextArea("");
		txtMap.setEditable(false);
		txtMap.setFont(new Font("Gorgia",Font.BOLD,14));
		scrollPane=new JScrollPane(txtMap,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		this.setContentPane(backPanel);

		backPanel.add(centerPanel, "Center");
		backPanel.add(bottomPanel, "South");		
		centerPanel.add(scrollPane);	
		bottomPanel.add(lblRow);
		bottomPanel.add(rowList);
		bottomPanel.add(lblCol);
		bottomPanel.add(colList);		
		bottomPanel.add(btnOK);
		bottomPanel.add(lblMaxNumber);
		bottomPanel.add(maxList);
		bottomPanel.add(btnRandom);
		bottomPanel.add(btnExit);			
		
		buffer=new StringBuffer();
		buffer.append("map["+ROW+"]["+COL+"]={\n");			
		for (int i = 0; i < ROW; i++) {
			buffer.append("{");
			for (int j = 0; j < COL; j++) {
				int k=Math.abs(random.nextInt())%maxIndex+1;							
				if (j < COL - 1) {						
					buffer.append(k + ", ");												
				} else if (i != ROW - 1) {						
					buffer.append(k + "}, ");							
				} else {
					buffer.append(k + "}");
				}					
			}
			buffer.append("\n");
		}
		buffer.append("}");
		txtMap.setText(buffer.toString());			
		
		// 设置窗口
		this.setSize(700, 520);		
		this.setLocationRelativeTo(null); // 让窗口在屏幕居中

		// 将窗口设置为可见的
		this.setVisible(true);

		// 注册监听器		
		this.addWindowListener(new WindowAdapter(){
			public void windowClosed(WindowEvent e) {			
				System.exit(0);
			}
		});
		btnOK.addActionListener(this);		
		btnRandom.addActionListener(this);	
		btnExit.addActionListener(this);		
	}

	public MapMaker(String name) {
		super(name);		
		initialize();				
	}

	public void actionPerformed(ActionEvent e) {
		buffer=new StringBuffer();
		if(e.getSource()==btnRandom){
			maxIndex=Integer.parseInt(maxList.getSelectedItem().toString());			
			buffer.append("map["+ROW+"]["+COL+"]={\n");			
			for (int i = 0; i < ROW; i++) {
				buffer.append("{");
				for (int j = 0; j < COL; j++) {
					int k=Math.abs(random.nextInt())%maxIndex+1;							
					if (j < COL - 1) {						
						buffer.append(k + ", ");												
					} else if (i != ROW - 1) {						
						buffer.append(k + "}, ");							
					} else {
						buffer.append(k + "}");
					}					
				}
				buffer.append("\n");
			}
			buffer.append("}");
			txtMap.setText(buffer.toString());			
		}else if(e.getSource()==btnOK){			
			mg.setRow(rowList.getSelectedIndex()+3);
			mg.setCol(colList.getSelectedIndex()+3);
			initialize();
		}else if(e.getSource()==btnExit){
			System.exit(0);				
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?