2023/PROJECT
[JAVA] 검색창
by Or0i쿠
2023. 6. 21.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class ChatWindow extends JFrame {
private JTextArea chatArea;
private JTextField inputField;
public ChatWindow() {
setTitle("Chat Window");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
chatArea = new JTextArea();
chatArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatArea);
add(scrollPane, BorderLayout.CENTER);
inputField = new JTextField();
inputField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
add(inputField, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void appendMessage(String message) {
chatArea.append(message + "\n");
}
private void sendMessage() {
String message = inputField.getText();
if (message == null || message.trim().isEmpty()) {
appendMessage("메세지를 입력해주세요.");
return;
}
appendMessage("나: " + message);
inputField.setText("");
// Perform Google search
String searchUrl = "https://www.google.com/search?q=" + URLEncoder.encode(message, StandardCharsets.UTF_8);
try {
Desktop.getDesktop().browse(new URI(searchUrl));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
// Handle any error that occurred during browsing
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ChatWindow();
}
});
}
}