Create a choice box which lies below the send/quit buttons. Have it include a number of option to allow the user to choose a username to be used when chatting. Then modify the ActionListener for the send button. Have it prepend the the selected username to the message prior to adding it to the output textarea. Create a menubar. Add a File menu and a Help menu. Add a menu item called "quit" to the File menu. When that menu item is selected the program should be terminated. Experiment with changing the color/font of the various components within the chat room frame. Optional: Add a menu item called "about" to the Help menu. When that menu item is selected a modal Dialog should be displayed which tells the user about the chat program.
时间: 2024-02-14 14:05:35 浏览: 121
Thank you for your question. It seems like you are requesting me to create a choice box, modify the ActionListener for the send button, create a menubar with menu items for quitting and about, and experiment with changing the color/font of the components in the chat room frame.
Here is the code to create a choice box and modify the ActionListener for the send button:
```java
// Create an array of usernames
String[] usernames = {"User1", "User2", "User3"};
// Create a choice box with the usernames
Choice usernameChoice = new Choice();
for (String username : usernames) {
usernameChoice.add(username);
}
// Add the choice box to the frame
frame.add(usernameChoice, BorderLayout.NORTH);
// Modify the ActionListener for the send button
sendButton.addActionListener(e -> {
// Prepend the selected username to the message
String username = usernameChoice.getSelectedItem();
String message = username + ": " + inputTextField.getText() + "\n";
outputTextArea.append(message);
inputTextField.setText("");
});
```
Here is the code to create a menubar with menu items for quitting and about:
```java
// Create the menubar
JMenuBar menuBar = new JMenuBar();
// Create the File menu
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
// Create the Quit menu item
JMenuItem quitMenuItem = new JMenuItem("Quit");
quitMenuItem.addActionListener(e -> System.exit(0));
fileMenu.add(quitMenuItem);
// Create the Help menu
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
// Create the About menu item
JMenuItem aboutMenuItem = new JMenuItem("About");
aboutMenuItem.addActionListener(e -> {
// Display a modal dialog with information about the chat program
JOptionPane.showMessageDialog(frame, "This is a simple chat program.");
});
helpMenu.add(aboutMenuItem);
// Add the menubar to the frame
frame.setJMenuBar(menuBar);
```
Finally, here is an example of how to change the font and color of the components in the chat room frame:
```java
// Change the font and color of the output text area
outputTextArea.setFont(new Font("Arial", Font.BOLD, 16));
outputTextArea.setForeground(Color.BLUE);
// Change the font and color of the input text field
inputTextField.setFont(new Font("Arial", Font.PLAIN, 14));
inputTextField.setForeground(Color.RED);
// Change the background color of the frame
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
```
阅读全文