import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Chat
{
public static void main(String[] args)
{
MyFrame aFrame = new MyFrame();
aFrame.setLayout( null );
aFrame.setBounds(100,100,485,600);
aFrame.setResizable(false);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.setVisible(true);
}
}
class MyFrame extends JFrame
{
JTextField nameField = new JTextField(); //输入姓名
JTextArea inputArea = new JTextArea(5,40); //输入留言
JScrollPane inputScrollPane = new JScrollPane(inputArea);
final JTextArea chatArea = new JTextArea("留言记录:");
JScrollPane chatScrollPane = new JScrollPane(chatArea);
JRadioButton redButton = new JRadioButton("RED",false);
JRadioButton greenButton = new JRadioButton("GREEN",false);
JRadioButton yellowButton = new JRadioButton("YELLOW",true);
JButton enterButton = new JButton("发表");
public MyFrame()
{
chatArea.setEditable(false);
chatArea.setLineWrap(true);
inputArea.setLineWrap(true);
chatArea.setLayout(null);
chatArea.setBackground(Color.YELLOW);
chatScrollPane.setBounds(0,0,480,400);
add(chatScrollPane);
setTitle("留言板");
setSize(480,600);
JPanel southPanel = new JPanel();
southPanel.setLayout( null );
southPanel.setBounds(0,400,500,130);
JLabel nameLabel = new JLabel("访客姓名");
nameLabel.setBounds(20,0,80,20);
southPanel.add(nameLabel);
nameField.setBounds(100,0,380,20);
southPanel.add(nameField);
JLabel inputLabel = new JLabel("访客留言");
inputLabel.setBounds(20,50,80,20);
southPanel.add(inputLabel);
inputScrollPane.setBounds(100,25,380,100);
southPanel.add(inputScrollPane);
add(southPanel);
enterButton.setLayout(null);
enterButton.setBounds(300,530,175,25);
add(enterButton);
ActionListener listener = new MyListener();
enterButton.addActionListener(listener);
ButtonGroup colorGroup = new ButtonGroup();
colorGroup.add(redButton);
colorGroup.add(greenButton);
colorGroup.add(yellowButton);
redButton.setLayout(null);
greenButton.setLayout(null);
yellowButton.setLayout(null);
redButton.setBounds(20,530,80,25);
greenButton.setBounds(110,530,80,25);
yellowButton.setBounds(200,530,80,25);
add(redButton);
add(greenButton);
add(yellowButton);
redButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent re)
{
chatArea.setBackground(Color.RED);
}
});
greenButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ge)
{
chatArea.setBackground(Color.GREEN);
}
});
yellowButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ye)
{
chatArea.setBackground(Color.YELLOW);
}
});
}
private class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String strA = nameField.getText();
String strB = inputArea.getText();
chatArea.append('\n' + "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" + '\n' + strA +" : " + '\n' + strB);
nameField.setText("");
inputArea.setText("");
}
}
}