1 50 package sample.jms.topics.GuiChat; 51 import java.awt.*; 52 import java.awt.event.ActionEvent ; 53 import java.awt.event.ActionListener ; 54 import javax.swing.*; 55 import javax.swing.border.BevelBorder ; 56 57 58 62 63 64 public class GuiContiner implements ActionListener { 65 JFrame chantFrame; 67 Container chatPanel; 68 static JTextArea chatArea; 69 JTextField message; 70 JTextField name; 71 JLabel messageInstructions; 72 JLabel nameInstructions; 73 JLabel cahtTitle; 74 JButton submitName; 75 JButton submitMessage; 76 JScrollPane chatAreaPane; 77 JPanel upperPanel; 78 JPanel enterMessagePanel; 79 80 GuiChat chat=null; 81 String userName="anonymous"; 82 83 86 public GuiContiner(){ 87 chat = new GuiChat(this); 89 90 chantFrame = new JFrame("MantaRay Chat"); 92 JFrame.setDefaultLookAndFeelDecorated(true); 93 chantFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 94 95 messageInstructions = new JLabel("Enter Message:", SwingConstants.LEFT); 97 messageInstructions.setForeground(Color.blue); 98 nameInstructions = new JLabel(" Enter Name:", SwingConstants.CENTER); 99 nameInstructions.setForeground(Color.blue); 100 cahtTitle = new JLabel("Conversation"); 101 cahtTitle.setFont(new Font("font",1,16)); 102 cahtTitle.setForeground(Color.black); 103 104 submitMessage= new JButton("Send Message"); 106 submitName = new JButton("Change Name"); 107 submitName.addActionListener(this); 108 submitMessage.addActionListener(this); 109 110 message = new JTextField(30); 112 message.setToolTipText("Enter your message here"); 113 message.setHorizontalAlignment(SwingConstants.LEFT); 114 message.setMaximumSize(new Dimension(70,25)); 115 message.addActionListener(this); 117 118 name = new JTextField(10); 120 name.setToolTipText("Enter user name here"); 121 name.setHorizontalAlignment(SwingConstants.LEFT); 122 name.setMaximumSize(new Dimension(50,25)); 123 name.addActionListener(this); 124 125 chatArea = new JTextArea(8,30); 127 chatArea.setEditable(false); 128 chatArea.setFont(new Font("font",2,14)); 129 chatAreaPane = new JScrollPane(chatArea); 130 chatAreaPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED )); 131 132 upperPanel = new JPanel(new GridLayout(1,4)); 134 upperPanel.add(cahtTitle); 135 upperPanel.add(nameInstructions); 136 upperPanel.add(name); 137 upperPanel.add(submitName); 138 139 enterMessagePanel= new JPanel(new BorderLayout(1,1)); 141 enterMessagePanel.add(messageInstructions, BorderLayout.WEST); 142 messageInstructions.setLabelFor(message); 143 enterMessagePanel.add(message, BorderLayout.CENTER); 144 enterMessagePanel.add(submitMessage, BorderLayout.EAST); 145 146 chatPanel=chantFrame.getContentPane(); 148 chatPanel.add(upperPanel, BorderLayout.NORTH); 149 chatPanel.add(chatAreaPane, BorderLayout.CENTER ); 150 chatPanel.add(enterMessagePanel, BorderLayout.SOUTH ); 151 152 chantFrame.pack(); 154 chantFrame.setVisible(true); 155 } 157 160 public void actionPerformed(ActionEvent e) { 161 if (e.getSource() == message || e.getSource() == submitMessage){ 163 chat.chatter(userName,message.getText()); 164 message.setText(""); 165 } 166 167 if (e.getSource() == name||e.getSource()==submitName){ 169 if (!name.getText().equals("")){ 170 userName=name.getText(); 171 } 172 } 173 174 } 176 180 public void doChat(String msg) { 181 chatArea.append(msg + "\n"); 182 chatArea.setCaretPosition(chatArea.getDocument().getLength()); 183 } 185 186 public static void main(String argv[]) { 187 System.out.println("MANTARAY GUI CHAT IS STARTING"); 188 javax.swing.SwingUtilities.invokeLater(new Runnable () { 190 public void run() { 191 GuiContiner GuiDisplay = new GuiContiner(); 192 } 193 }); 194 } 196 } | Popular Tags |