java浏览器的开发html显示异常,一定要内嵌浏览器吗

Python010

java浏览器的开发html显示异常,一定要内嵌浏览器吗,第1张

java浏览器开发,其实很简单, 使用JavaFX的WebView组件即可.(支持html5)也可以在swing里使用JFXPanel然后添加WebView

效果图如下

参考代码如下(java8+win10 x64 编写,测试)

import javafx.application.Application

import javafx.concurrent.Worker.State

import javafx.geometry.Insets

import javafx.scene.Scene

import javafx.scene.control.*

import javafx.scene.layout.*

import javafx.scene.web.*

import javafx.stage.Stage

public class WebViewTest extends Application {

public static void main(String[] args) {

launch(args)

}

@Override

public void start(Stage primaryStage) throws Exception {

//顶部

HBox hbox=new HBox(8)

Label labeAddr=new Label("地址")

TextField tfAddr = new TextField()

tfAddr.setMinWidth(500)

tfAddr.setText("http://www.baidu.com")

Button btnGo = new Button("Go转到")

Button btnGoBack=new Button("<-")

Button btnH=new Button("->")

TextField tfSerch = new TextField()

Button btnSerch= new Button("捜")

hbox.getChildren().addAll(labeAddr,tfAddr,btnGoBack,btnH,btnGo,tfSerch,btnSerch)

hbox.setPadding(new Insets(5))

//中间

WebView wv = new WebView()

WebEngine we = wv.getEngine()

we.getLoadWorker().stateProperty().addListener((ob, ov, nv) ->{

if(nv==State.SCHEDULED) {

tfAddr.setText(we.getLocation())//

}

})

//底部

Label labelBottom = new Label()

labelBottom.textProperty().bind(we.getLoadWorker().messageProperty())//底下的信息显示

BorderPane root = new BorderPane()

root.setTop(hbox)

root.setCenter(wv)

root.setBottom(labelBottom)

Scene scene = new Scene(root, 900, 600)

primaryStage.setTitle("JavaFX 雪飞潇潇 web浏览器")

primaryStage.setScene(scene)

primaryStage.show()

btnGo.setOnAction(e ->{

we.load(tfAddr.getText().trim())

})

btnGoBack.setOnAction(e->{

we.executeScript("history.back()")//执行JavaScript,后退

})

btnH.setOnAction(e->{

we.executeScript("history.forward()")//执行JavaScript,前进

})

//搜索

btnSerch.setOnAction(e->{

we.load("https://www.baidu.com/s?wd="+tfSerch.getText())

})

}

}

import javax.swing.*

import java.awt.*

import java.awt.event.*

import java.net.*

import java.io.*

import javax.swing.event.*

@SuppressWarnings("serial")

class Win3 extends JFrame implements ActionListener,Runnable

{

JButton button

URL url

JTextField text

JEditorPane editPane

byte b[]=new byte[118]

Thread thread

public Win3()

{

text=new JTextField(20)

editPane=new JEditorPane()

editPane.setEditable(false)

button=new JButton("确定")

button.addActionListener(this)

thread=new Thread(this)

JPanel p=new JPanel()

p.add(new JLabel("输入网址:"))

p.add(text)

p.add(button)

Container con=getContentPane()

con.add(new JScrollPane(editPane),BorderLayout.CENTER)

con.add(p,BorderLayout.NORTH)

setBounds(60,60,400,300)

setVisible(true)

validate()

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

editPane.addHyperlinkListener(new HyperlinkListener()

{

public void hyperlinkUpdate(HyperlinkEvent e)

{

if(e.getEventType()==

HyperlinkEvent.EventType.ACTIVATED)

{

try{

editPane.setPage(e.getURL())

}

catch(IOException e1)

{

editPane.setText(""+e1)

}

}

}

}

)

}

public void actionPerformed(ActionEvent e)

{

if(!(thread.isAlive()))

thread=new Thread(this)

try{

thread.start()

}

catch(Exception ee)

{

text.setText("我正在读取"+url)

}

}

public void run()

{

try {

int n=-1

editPane.setText(null)

url=new URL(text.getText().trim())

editPane.setPage(url)

}

catch(MalformedURLException e1)

{

text.setText(""+e1)

return

}

catch(IOException e1)

{

text.setText(""+e1)

return

}

}

}

public class Example3

{

public static void main(String args[])

{

new Win3()

}

}

function cache_none($interval = 60) \x0d\x0a{ \x0d\x0a // 向后兼容HTTP/1.0 \x0d\x0a header("Expires: 0") \x0d\x0a header("Pragma: no-cache") \x0d\x0a // 支持HTTP/1.1 \x0d\x0a header("Cache-Control: no-cache,no-store,max-age=0,s-maxage=0,must-revalidate") \x0d\x0a} \x0d\x0a当调用session_start()时,PHP会自动发送一个no-cache类的头来阻止缓存数据,\x0d\x0a要注意的是:\x0d\x0a通过POST方法发送的请求不能以如上所述的方式缓存。