知道如何更改在 JFrame 中的按钮单击事件上在 JEditorPane 中查看的 html 页面,抱歉,我是 java 新手,因此我们将非常感谢基本的解释。(需要更改此处写入第 1 页内容的内容)
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();
}
}
}
}
最佳答案
当事情不起作用时,要做的第一件事是将代码简化为显示错误的最简单的代码。这段代码中有很多错误,我在简化过程中简单地删除了它。但从这个工作代码开始,一点一点地改变它,直到它再次中断,然后准备错误的 MCVE 并将其发布在这里。
在进入工作示例之前的其他提示。
- 不要有四个名称相同的按钮!调试时会很困惑,最终用户也会感到困惑。如果您无法为它们想出更有用的名称,请使用相同或相似的文本作为属性的名称。
- “单击按钮时不可见/可见”部分与问题无关,因此为了简化代码,请删除该部分并让包含 3 个按钮的面板从一开始就可见。
- 但话虽如此,只需要 2 个按钮就可以看到它工作或失败。
- 不要在布局约束中使用魔数(Magic Number)!我怀疑这些按钮最终出现在您希望它们出现的位置。
- 应用程序资源在部署时将成为嵌入式资源,因此明智的做法是立即开始访问它们,就好像它们是嵌入式资源一样。安embedded-resource必须通过 URL 而不是文件访问。请参阅info. page for embedded resource了解如何构成 URL。
话虽如此,剩下的问题总结为:
url = "file:///C:/Users/Chinmay/workspace/project1/src/page1.html";
该属性不是URL
,但是 String
。调用属性url
不会改变这一点。那么当 jep.setPage(url);
被调用时,该方法将假定 String
代表 File
路径并相应地处理它(或相应地失败)。由于该字符串不代表有效的文件路径,因此它将失败。
这是一个仅使用 2 个按钮和实际 URL 的工作代码。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.net.URL;
public class check1 extends JFrame implements ActionListener {
JEditorPane jep;
JScrollPane scroll;
JPanel p, p1;
JButton b1, b2;
String url;
public check1() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
b1 = new JButton("Button 1");
b1.addActionListener(this);
b2 = new JButton("Button 2");
b2.addActionListener(this);
p = new JPanel();
p.setLayout(new FlowLayout());
p1 = new JPanel();
p1.setLayout(new GridLayout(4, 2, 1, 1));
p1.add(b1);
p1.add(b2);
try {
jep = new JEditorPane(
new URL("http://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html#constructor.summary"));
} catch (IOException e) {
e.printStackTrace();
}
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(b1)) {
url = "http://docs.oracle.com/javase/8/docs/api/javax/swing/JButton.html#constructor.summary";
} else if (e.getSource().equals(b2)) {
url = "http://docs.oracle.com/javase/8/docs/api/javax/swing/JApplet.html#constructor.summary";
}
try {
jep.setPage(new URL(url));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
关于java - 单击按钮时更改 JEditorPane 内的 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40530450/