chatstartexitpanel.java

来自「软件工程实践课程的答案哦」· Java 代码 · 共 151 行

JAVA
151
字号
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * this calss functions as a chat room, all players can chat with each other*/

public class ChatStartExitPanel extends JPanel implements ActionListener
{	
	GameFace gameFace;
	JTextField contentToSent;
	JTextArea chatContent;
	Color color = new Color(7,122,94);
	//String clientName;
	JLabel name;
	JLabel score ;
	
	ChatStartExitPanel(GameFace gameFace)
	{
		this.gameFace = gameFace;
		
		this.setBackground(color);
		//////////////up//////////////////
		//////////title///////////
		JPanel up = new JPanel();
		
		JButton send = new JButton("Send");
		send.addActionListener(this);
		JPanel sendContent =new JPanel();
		
		up.setLayout(new BorderLayout());
		ImageIcon titleImage= new ImageIcon("pics\\title.gif");
		JLabel title = new JLabel(titleImage);
		up.setBackground(GameFace.bgColor);
		////////////////textArea////////////
		chatContent = new JTextArea("Columns Server Edition!");
		chatContent.setFont(new Font("ScanSerif",Font.PLAIN,16));
		chatContent.setBackground(new Color(209,244,252));
			chatContent.setEditable(false);
		contentToSent = new JTextField(10);
			contentToSent.addActionListener(this);
		JScrollPane scrollPanel = new JScrollPane(chatContent);
		chatContent.setLineWrap(true);
		scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		//scrollPanel.setAutoscrolls(false);
		
		JPanel text = new JPanel(new BorderLayout());
		ImageIcon lineImage= new ImageIcon("pics\\left.gif");
	
		JLabel leftLine = new JLabel(lineImage);
		JLabel rightLine = new JLabel(lineImage);
	
		text.add(scrollPanel,BorderLayout.CENTER);
		text.add(rightLine,BorderLayout.WEST);
		text.add(leftLine,BorderLayout.EAST);
		
		sendContent.add(contentToSent);
		sendContent.setBackground(color);
		sendContent.add(send);
		
		up.add(title,BorderLayout.NORTH);
		up.add(text,BorderLayout.CENTER);
		up.add(sendContent,BorderLayout.SOUTH);
	
		
		
		//////////////down////////////////////
		JButton start = new JButton("Start");
			start.addActionListener(gameFace);
		JButton exit = new JButton("Exit");
			exit.addActionListener(gameFace);
		JPanel down = new JPanel();
		
	
		down.setLayout(new BoxLayout(down,BoxLayout.X_AXIS));
		
		down.add(Box.createHorizontalStrut(10));
		down.add(start);
		down.add(Box.createHorizontalStrut(30));
		down.add(exit);
		down.setBackground(color);
		///////////////bottom////////////////////
		JPanel bottom = new JPanel();
		ImageIcon bottomImage = new ImageIcon("pics\\bottom.gif");
		JLabel lbottom = new JLabel(bottomImage);
		bottom.add(lbottom);
		bottom.setBackground(color);
		///////////////main////////////
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		this.add(up);
		this.add(Box.createVerticalStrut(30));
		JPanel pmessage = new JPanel(new GridLayout(2,1,0,18));
		name = new JLabel();
		score = new JLabel();
//		this.setSelfName(clientName);
		this.setSelftScore(0);
		
		name.setForeground(Color.white);
		pmessage.add(name);
		score.setForeground(Color.white);
		pmessage.add(score);
		pmessage.setBackground(color);
		
		this.add(pmessage);
		this.add(Box.createVerticalStrut(10));
		this.add(down);
		this.add(Box.createVerticalStrut(45));
		//this.add(bottom);
		
		
		
	}
	
	public void actionPerformed(ActionEvent e)
	{
		String command = e.getActionCommand();
		 if(command=="Send"||e.getSource()==contentToSent)
		{
			
			String content = contentToSent.getText();
			
			contentToSent.setText("");
			
			chatContent.append("\n"+gameFace.selfPanel.selfInfo.getName()+" says: "+content);
			Communication.sendChatContent(content);
		}
		
	
	}
	
	public void setSelftScore(int iscore)
	{
		score.setText("          "+"Score: "+iscore);
	}
	
	public void setSelfName(String pname)
	{
		name.setText("         "+"Welcome  "+pname);
	
	}
	/**
	 * receive the chat content from the server
	 * @param name the sender of the message
	 * @param the content what the sender says*/
	public void receiveChatContent(String name,String content)
	{
	
		chatContent.append("\n"+name+"says: "+content);
	}
}

⌨️ 快捷键说明

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