JFrameのボタンクリックイベントでJEditorPane内で表示されるhtmlページを変更する方法はありますか、ごめんなさい、私はJavaの初心者ですので、基本的な説明を本当に感謝します。こちらのページ1コンテンツが書かれている場合)ボタンクリック時のJEditorPane内のhtmlページの変更
package test1;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class check1 extends JFrame implements ActionListener{
JEditorPane jep;
JScrollPane scroll;
JPanel p,p1;
JButton b,b1,b2,b3;
Dimension d;
String url;
public check1() {
d = new Dimension(500, 1500);
b = new JButton("Click ME");
b.addActionListener(this);
b1 = new JButton("Click ME");
b2 = new JButton("Click ME");
//b2.setBounds(0, 100, 70, 40);
b3 = new JButton("Click ME");
//b3.setBounds(0, 150, 70, 40);
p = new JPanel();
p.setLayout(new FlowLayout());
p.add(b);
p1 = new JPanel();
p1.setLayout(new GridLayout(4, 2, 1, 1));
p1.add(b1,0,0);
p1.add(b2,0,1);
p1.add(b3,2,0);
p1.setVisible(false);
try {
jep = new JEditorPane("file:///C:/Users/Chinmay/workspace/project1/src/page1.html");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jep.setEditable(true);
scroll = new JScrollPane(jep);
setLayout(new BorderLayout());
getContentPane().add(p,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.WEST);
getContentPane().add(scroll,BorderLayout.CENTER);
setSize(1000, 800);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String args[])
{
new check1().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b))
{
p1.setVisible(true);
}
else if(e.getSource().equals(b1))
{
url ="file:///C:/Users/Chinmay/workspace/project1/src/page.html";
try {
jep.setPage(url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource().equals(b2))
{
url ="file:///C:/Users/Chinmay/workspace/project1/src/page1.html";
try {
jep.setPage(url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource().equals(b3))
{
url ="file:///C:/Users/Chinmay/workspace/project1/src/page2.html";
try {
jep.setPage(url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
ファイルオブジェクトを作成してjep.setPage(new File(url).toURI()。toURL())として渡しても機能しませんでした。ページが変更されない、私はrepaint()のようなJEditorPaneの内容をリフレッシュするためにsometingを行う必要がありますか?アプレットで使用するメソッド@AndrewThompson – charle819
アプレットまたはアプリケーションの 'JEditorPane'に対して' repaint() 'を呼び出す必要はありません。 –
すぐに役立つように、[MCVE]または[短く、自己完結型の正しい例](http://www.sscce.org/)を投稿してください。この場合、URLに実際のWebページを使用する必要があります。他の誰もあなたのマシンに存在する場所にこれらのファイルを持っていないからです。 –