본문 바로가기
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();
         }
      });
   }
}

 

'2023 > PROJECT' 카테고리의 다른 글

[JAVA]SWING  (0) 2023.06.23
[JAVA] 캘린더 UI (클래스 정리)  (0) 2023.06.22
[JAVA] 캘린더 UI  (0) 2023.06.20
[JAVA] 오라클 일정 추가 캘린더  (0) 2023.06.20
[JAVA] 둥근 검색창  (0) 2023.06.19