📄 mypanel.java
字号:
/**
* Email: taorundong@126.com
*
* @author taorundong
* @version 1.00 07/02/04
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyPanel extends JPanel implements ActionListener,Runnable{
private
JTextField message = null;
JTextArea text = null;
JComboBox comboBox = null;
JScrollPane scrollPane = null;
String firstString[] = {"Chose one person to chat!"};
JTextArea combo = null;
InputThread inputToTextArea = null;
MyPanel(){
super();
this.setLayout(new BorderLayout());
comboBox = new JComboBox(firstString);
comboBox.addActionListener(this);
combo = new JTextArea();
text = new JTextArea();
message = new JTextField();
message.addActionListener(this);
showMessage();
showComboBox();
showTextArea();
showLabel();
validate();
new Thread(this).start();
}
//put the input and output thread into a single thread
public void run(){
inputToTextArea = new InputThread(text,message); //new InputThread
}
public void showMessage(){
JLabel left = new JLabel("Welcome to the chating world!");
JPanel temp = new JPanel(new GridLayout(1,2));
temp.add(left);
message.setToolTipText("Input the message here");
temp.add(message);
temp.validate();
this.add(temp,"South");
this.validate();
}
public JTextArea getTextArea(){
return text;
}
public void showComboBox(){
combo.setBackground(Color.pink);
combo.setEditable(false);
JPanel temp = new JPanel(new BorderLayout());
JLabel down = new JLabel();
down.setIcon(new ImageIcon("picture\\3.jpg"));
temp.add(comboBox,"North");
temp.add(combo,"Center");
temp.add(down,"South");
temp.validate();
this.add(temp,"West");
this.validate();
}
public String getMessageContent(){
return message.getText();
}
public void setMessageContent(String s){
text.append("\n"+s);
}
public void showTextArea(){
text.setBackground(Color.GREEN);
text.setWrapStyleWord(false);
text.setLineWrap(true);
text.setAlignmentX(JTextArea.LEFT_ALIGNMENT);
text.setEditable(false);
text.validate();
scrollPane = new JScrollPane(text);
this.add(scrollPane,"Center");
this.validate();
}
public void showLabel(){
JPanel slip = new JPanel();
slip.setLayout(new GridLayout(2,1));
slip.setBackground(Color.yellow);
JLabel down = new JLabel();
JLabel top = new JLabel();
down.setIcon(new ImageIcon("picture\\1.jpg"));
top.setIcon(new ImageIcon("picture\\2.jpg"));
slip.add(top);
slip.add(down);
slip.validate();
this.add(slip,"East");
this.validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==comboBox){
new MusicThread("music\\press.wav");
Object temp = comboBox.getSelectedItem();
combo.setText((String)temp);
}
if(e.getSource()==message){ //here will send the message
new MusicThread("music\\send.wav");
try{
inputToTextArea.getOutput().writeUTF(message.getText());
}
catch(Exception ee){
ee.printStackTrace();
}
text.append(message.getText()+"\n");
message.setText("");//new output thread
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -