📄 clientgui.java
字号:
package Client;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import ShareData.Message;
public class ClientGUI extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private IClient ic;
private JMenuBar menuBar=new JMenuBar();
private JMenu menuC=new JMenu("Chatroom");
private JMenu menuV=new JMenu("Setting");
private JMenu menuH=new JMenu("Help");
private JMenuItem menuIC=new JMenuItem("Connect",KeyEvent.VK_C);
// private JMenuItem menuIS=new JMenuItem("Send",KeyEvent.VK_S);
private JMenuItem menuIQ=new JMenuItem("Quit",KeyEvent.VK_Q);
private JMenuItem menuIN=new JMenuItem("Change Nickname",KeyEvent.VK_N);
private JMenuItem menuIA=new JMenuItem("About Us",KeyEvent.VK_A);
private JMenuItem menuIT=new JMenuItem("Tips",KeyEvent.VK_T);
private JTextArea recTxt=new JTextArea();
private JTextArea sendTxt=new JTextArea(3,3);
private JScrollPane jsp=new JScrollPane(sendTxt);
// private JLabel sendLbl=new JLabel("Send To");
private JLabel slbl=new JLabel("Sender: ");
private JLabel sender=new JLabel(" ");
private JLabel rlbl=new JLabel("Receiver:" );
private JLabel receiver=new JLabel(" ");
private JButton sendBtn=new JButton("Send");
private JButton cancelBtn=new JButton("Cancel");
private JPanel pane1=new JPanel();
private JPanel pane2=new JPanel();
private JList clientList=new JList();
private JScrollPane jspUser=new JScrollPane(clientList);
private JScrollPane recjsp=new JScrollPane(recTxt);
public void iniMenu(){
setJMenuBar(menuBar);
menuBar.add(menuC);
menuBar.add(menuV);
menuBar.add(menuH);
menuC.add(menuIC);
// menuC.add(menuIS);
menuC.add(menuIQ);
menuV.add(menuIN);
menuH.add(menuIT);
menuH.add(menuIA);
}
public void iniPane(){
pane1.add(slbl,FlowLayout.LEFT);
pane1.add(sender);
pane1.add(rlbl);
// pane1.add(sendLbl);
pane1.add(receiver);
pane1.setBackground(Color.YELLOW);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
GroupLayout layout=new GroupLayout(pane2);
pane2.setLayout(layout);
pane2.setBackground(Color.YELLOW);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.SequentialGroup hGroup=layout.createSequentialGroup();
hGroup.addGroup(layout.createParallelGroup().addComponent(pane1).addComponent(jsp));
hGroup.addGroup(layout.createParallelGroup().addComponent(sendBtn).addComponent(cancelBtn));
layout.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup=layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(pane1).addComponent(sendBtn));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(jsp).addComponent(cancelBtn));
layout.setVerticalGroup(vGroup);
add(pane2,BorderLayout.PAGE_END);
}
public void addEvent(){
menuIT.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "1.Chatroom->Connect\n2.Setting->Change Nickname");
}
});
menuIC.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
ic.connect();
sender.setText(" Anonymous ");
}
});
menuIQ.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ic.quit();
// System.exit(0);
}
});
menuIN.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name=JOptionPane.showInputDialog("Please give your new nickname:");
ic.setName(name);
sender.setText(name);
}
});
menuIA.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Java Chatroom V1.0");
}
});
sendBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String message=ic.getName()+" said to "+receiver.getText() +" : "+ sendTxt.getText();
// clientList.getSelectedValue().toString()
Message m=new Message("conversation",message);
System.out.println(message);
ic.sendMessage(m);
sendTxt.setText("");
}
});
clientList.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
receiver.setText(clientList.getSelectedValue().toString());
}
});
cancelBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
sendTxt.setText("");
}
});
}
public void updateShowMessage(String message){
recTxt.append(message+"\r\n");
}
public void updateClientList(Object[] l){
clientList.setListData(l);
}
public ClientGUI(IClient ic){
this.ic=ic;
this.setBackground(Color.YELLOW);
iniMenu();
iniPane();
add(recjsp);
add(jspUser,BorderLayout.LINE_END);
clientList.setFixedCellWidth(85);
addEvent();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Chatting_ClientGUI");
this.setSize(600, 500);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public ClientGUI(){
// ClientGUI(new Client());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -