目标是使用swing组件构建一个简单的Java Web浏览器,并将JEditorPane存储在一个类中,并将地址栏和待添加的JButton存储在另一个类中。我如何让他们彼此互动,就像他们在同一个班级时一样?
原始类:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JavaBrowser extends JFrame {
private JTextField addressBar;
private JEditorPane contentDisplay;
public JavaBrowser() {
super("Java Browser v1.0");
addressBar = new JTextField("Enter URL here, then press enter");
addressBar.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
loadContent(event.getActionCommand());
}
}
);
add(addressBar, BorderLayout.NORTH);
contentDisplay = new JEditorPane();
contentDisplay.setEditable(false);
contentDisplay.addHyperlinkListener(
new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent event) {
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED) {
loadContent(event.getURL().toString());
}
}
}
);
add(new JScrollPane(contentDisplay), BorderLayout.CENTER);
setSize(500, 300);
setVisible(true);
}
public void loadContent(String userInput) {
try
{
contentDisplay.setPage(userInput);
addressBar.setText(userInput);
}
catch (Exception e)
{
System.out.println("Invalid URL entered in the address bar!");
}
}
}
JEditorPane课程:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JavaBrowser extends JFrame {
private JEditorPane contentDisplay;
public JavaBrowser() {
super("Java Browser v1.0");
contentDisplay = new JEditorPane();
contentDisplay.setEditable(false);
contentDisplay.addHyperlinkListener(
new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent event) {
if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED) {
loadContent(event.getURL().toString());
}
}
}
);
add(new JScrollPane(contentDisplay), BorderLayout.CENTER);
setSize(500, 300);
setVisible(true);
}
public void loadContent(String userInput) {
try
{
contentDisplay.setPage(userInput);
ToolBar.getAddressBar().setText(userInput);
}
catch (Exception e)
{
System.out.println("Invalid URL entered in the address bar!");
}
}
}
ToolBar类:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
public class ToolBar {
private static JTextField addressBar;
public static JTextField getAddressBar() {
return addressBar;
}
public void setAddressBar(JTextField addressBar) {
this.addressBar = addressBar;
}
public ToolBar() {
JavaBrowser jb = new JavaBrowser();
addressBar = new JTextField("Enter URL here, then press enter");
addressBar.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
jb.loadContent(event.getActionCommand());
}
}
);
jb.add(addressBar, BorderLayout.NORTH);
}
}
答案 0 :(得分:1)
您的ToolBar
课程不是任何类型的Component
。它应该扩展JPanel
。您还应该将布局管理器添加到JPanels。事实上,您应该使用更多的JPanel而不是在JFrame中填充内容。
此外,您应该将代码分为3个类:一个用于浏览器,一个用于工具栏,另一个用于控制两者。在该控制类中,创建JavaBrowser
的实例和ToolBar
的实例。更新ToolBar类,以便它可以存储对JavaBrowser的引用,并展开ToolBar的构造函数以期望JavaBrowser的实例。将两个内容放在控制类的JPanel中,并将该JPanel放在JFrame中。
我的挥杆有点生疏,但这是一个努力:
public class MainApp extends JFrame{
public MainApp(){
JavaBrowser browser = new JavaBrowser();
ToolBar toolbar = new ToolBar(browser);
JPanel panel = new JPanel(new BorderLayout());
panel.add(browser, BorderLayout.CENTER);
panel.add(toolbar, BorderLayout.NORTH);
add(panel);
}
}
public class ToolBar extends JPanel{
public ToolBar(final JavaBrowser browser){
super(new BorderLayout());
JTextField addressBar = new JTextField("...");
addressBar.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){ browser.loadContent(event.getActionCommand()); }
});
add(addressBar, BorderLayout.CENTER);
}
}
public class JavaBrowser extends JPanel{
public JavaBrowser(){
super(new BorderLayout());
JEditorPane editor = new JEditorPane();
// set up all your stuff like properties and listeners to the editor
add(editor, BorderLayout.CENTER);
}
// other methods such as loadContent(...)
}
然后在某个类(通常在psvm中)的某个时刻,使用SwingUtilities.invokeLater(r)
在Swing UI线程上启动Swing代码,其中r是Runnable
,它创建一个{{1}的实例1}},设置其大小并调用Controller
。