Hallo, Ich möchte ein Programm programmieren das wenn man auf VK_ENTER clickt das ein vorher eingegebener Satz gegoogelt wird allerdings klapt das nur bei einem einzelnen Wort!
Hier der Code:
und der Code der main
Hier der Code:
Code:
package Launch;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.*;
public class Launcher extends JFrame implements ActionListener,KeyListener {
public static JButton jb1;
public static JTextField jtf;
/**
*
*/
private static final long serialVersionUID = -6558618461179956458L;
public Launcher() {
setVisible(true);
setLayout(null);
setSize(500,600);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
addKeyListener(this);
// Design
// JLabel
JLabel jl1 = new JLabel();
jl1.setBounds(150, 0, 200, 200);
add(jl1);
jl1.setIcon(new ImageIcon(getClass().getResource("back1.png")));
//JTextField
jtf = new JTextField();
add(jtf);
jtf.setBackground(Color.WHITE);
jtf.setBounds(150, 150, 200, 50);
jtf.addKeyListener(this);
//JButton
jb1 = new JButton();
jb1.setBounds(150, 200, 200, 50);
jb1.addActionListener(this);
jb1.setIcon(new ImageIcon(getClass().getResource("search.png")));
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == jb1) {
String s = jtf.getText();
try {
new ProcessBuilder(new String[] { "cmd", "/c", "start",
"https://www.google.de/search?q="+ s + "&ie=&oe=" }).start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
String s = jtf.getText();
try {
new ProcessBuilder(new String[] { "cmd", "/c", "start",
"https://www.google.de/search?q="+ s + "&ie=&oe=" }).start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
Code:
package Launch;
public class Main {
public static void main(String[] args) {
Launcher l1 = new Launcher();
}
}