I've recently completed a project in Java,and showed it to my advisor. Forget about the detail about the project,my final output of the program is to simply output two webpages(one is translated by Google,the other is the Chinses webpage that I retrieved).
The two statements that I wrote are:
Process p=Runtime.getRuntime().exec("cmd /c start http://news.baidu.com/ns?word="+key1+"+"+keyy+"&bt=0&et=0&si=&rn=20&tn=newsA&ie=gb2312&ct=1&cl=2&f=12");
Process r=Runtime.getRuntime().exec("cmd /c start http://translate.google.cn/translate?u=http%3A%2F%2F"+u.substring(7)+"&sl=en&tl=zh-CN&hl=zh-CN&ie=UTF-8");
They will pop up two IE windows to show the webpage.
My advisor is satisfied with my result,but he is not happy with the output format.He would like to see these two webpages shown in a GUI window instead of IE windows.(Preferably with some panel seperating the two pages in this GUI).
I am just suddenly stuck on this point,how would I put those two webpages into a GUI frame in Java (in two seperate text boxes or sth. similar).
I am using Eclipse IDE.
开发者_运维知识库Please kindly help ,either with ideas or code,thanks.
You would need a web browser component for the rendering of your HTML.
See the thread here for a list of possible browser component.
You can see nice screenshots on the The DJ Project web page as well.
It depends on what GUI toolkit you are using:
- Swing - see ccheneson answer
- SWT/JFrame - it has it's own browser component
I have used QT Webkit to do something similar to this before and it actually worked out extremely well.
http://qt.nokia.com/doc/qtjambi-4.4/html/com/trolltech/qt/qtjambi-index.html
If you want to access an HTTPS site using this method however it's a little more difficult to get it completely working.
JavaFX is the best option.It can work together with Swing.
package com.test.swing.pro;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
public class SwingJFxBrower {
private static WebEngine engine = null;
static JTextField urlText = null;
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
JFrame frame = new JFrame("Swing and JavaFX : Browser");
JButton back = new JButton("<<<<");
JButton go = new JButton("Go");
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
goBack();
}
});
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadUrlCustom();
}
});
back.setPreferredSize(new Dimension(100, 25));
go.setPreferredSize(new Dimension(100, 25));
frame.add(back, BorderLayout.NORTH);
JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
urlText = new JTextField(100);
urlText.setPreferredSize(new Dimension(100, 25));
java.awt.Font font = new java.awt.Font("Courier", java.awt.Font.BOLD, 12);
urlText.setFont(font);
urlLabel.setPreferredSize(new Dimension(100, 25));
frame.add(urlLabel, BorderLayout.WEST);
frame.add(urlText, BorderLayout.CENTER);
frame.add(go, BorderLayout.EAST);
final JFXPanel jfxPanel = new JFXPanel();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
jfxPanel.setPreferredSize(new Dimension(screenSize.width - 150, screenSize.height - 200));
frame.add(jfxPanel, BorderLayout.SOUTH);
frame.setLayout(new FlowLayout());
Platform.runLater(() -> {
WebView webView = new WebView();
jfxPanel.setScene(new Scene(webView));
engine = webView.getEngine();
loadUrl();
});
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(screenSize.width - 100, screenSize.height - 150);
frame.setResizable(false);
centreWindow(frame);
}
private static void loadUrl() {
engine.load("https://duckduckgo.com/");
}
private static void loadUrlCustom() {
String url = (urlText.getText());
boolean validUrl = isValidURL(url);
if (validUrl) {
urlText.setBackground(java.awt.Color.WHITE);
urlText.setForeground(java.awt.Color.BLACK);
Platform.runLater(new Runnable() {
@Override
public void run() {
engine.load(url);
}
});
} else {
urlText.setBackground(java.awt.Color.RED);
urlText.setForeground(java.awt.Color.WHITE);
}
}
public static void goBack() {
try {
final WebHistory history = engine.getHistory();
Platform.runLater(new Runnable() {
public void run() {
try {
history.go(-1);
} catch (Exception ignore) {
}
}
});
} catch (Exception ignore) {
}
}
public static boolean isValidURL(String url) {
URL u = null;
try {
u = new URL(url);
} catch (MalformedURLException e) {
return false;
}
try {
u.toURI();
} catch (URISyntaxException e) {
return false;
}
return true;
}
public String goForward() {
final WebHistory history = engine.getHistory();
ObservableList<WebHistory.Entry> entryList = history.getEntries();
int currentIndex = history.getCurrentIndex();
Platform.runLater(new Runnable() {
public void run() {
history.go(1);
}
});
return entryList.get(currentIndex < entryList.size() - 1 ? currentIndex + 1 : currentIndex).getUrl();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGUI();
}
});
}
public static void centreWindow(Window frame) {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
}
}
精彩评论