我正在开发一个带有选项卡的简单网络浏览器。我在选项卡 Pane 下方添加文本字段时遇到很多麻烦,就像在 Chrome 中一样。这是我到目前为止所拥有的: 公共(public)类浏览器 { 私有(private) JFrame 框架; 私有(private) JPanel 面板顶部; 私有(private) JEditorPane 编辑器; 私有(private) JScrollPane 滚动; 私有(private) JTextField 字段; 私有(private) JButton 按钮; 私有(private) JButton 主页; 私有(private) URL url; 私有(private) JMenu 文件 = new JMenu("文件"); 私有(private) JMenuItem exit = new JMenuItem("退出"); 私有(private) JMenuBar 菜单栏 = new JMenuBar(); private ImageIcon nt = new ImageIcon("./Icons/newTab.jpg"); private ImageIcon cl = new ImageIcon("./Icons/close.png"); 私有(private) JButton newTab = new JButton(cl); 私有(private) JTabbedPane tabbedPane = new JTabbedPane(); 私有(private) int tabCounter = 0; 私有(private)尺寸昏暗=新尺寸(nt.getIconWidth()+ 2, nt.getIconHeight()+2); 公共(public)浏览器() { initComponents();
frame = new JFrame();
frame.setTitle("TeslaNet Browser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setJMenuBar(menuBar);
menuBar.add(file);
file.add(exit);
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
panelTop = new JPanel();
frame.add(BorderLayout.NORTH, panelTop);
panelTop.add(field);
panelTop.add(button);
panelTop.add(home);
panelTop.add(newTab);
newTab.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
initComponents();
}
});
newTab.setToolTipText("New Tab");
newTab.setPreferredSize(dim);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
frame.add(topPanel);
topPanel.add(tabbedPane, BorderLayout.CENTER);
panelTop.add(scroll, BorderLayout.CENTER);
frame.setVisible(true);
}
private void initComponents()
{
try
{
url = new URL("http://www.reddit.com");
}
catch(MalformedURLException mal)
{
JOptionPane.showMessageDialog(null,mal);
}
try
{
editor = new JEditorPane(url);
editor.setEditable(false);
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(null,ioe);
}
scroll = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
field = new JTextField(14);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
field.setText(url.toString());
}
});
button = new JButton("Go");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
editor.setPage(field.getText());
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(null, ioe);
}
}
});
home = new JButton("Home");
home.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
url = new URL("http://www.thissongissick.com");
field.setText(url.toString());
}
catch(MalformedURLException mal)
{
JOptionPane.showMessageDialog(null,mal);
}
try
{
editor.setPage(field.getText());
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(null, ioe);
}
}
});
JPanel tab = new JPanel();
JButton closeButton = new JButton(nt);
closeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tabbedPane.remove(tabbedPane.getTabCount()-1);
}
});
closeButton.setPreferredSize(dim);
closeButton.setToolTipText("Close");
JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
System.out.print(tabbedPane.getSelectedIndex());
tab.setOpaque(false);
tab.add(tabLabel, BorderLayout.WEST);
tab.add(closeButton, BorderLayout.EAST);
tabbedPane.addTab(null, editor);
tabbedPane.setTabComponentAt(tabbedPane.getTabCount()-1, tab);
}
}
谢谢!
最佳答案
看看这个例子。我不想尝试猜测哪些变量适用于哪个组件,所以我想出了一个类似的例子。您需要做的就是将所需的所有内容放入选项卡的面板中内部。因此基本上每个面板都会有自己的文本字段和编辑器 Pane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Browser1 {
private JTabbedPane tabbedPane;
private final JButton add;
int i = 1;
public Browser1() {
tabbedPane = new JTabbedPane();
tabbedPane.add(new JScrollPane(createTabbedPanel()), "Tab " + i);
add = new JButton("Add Tab");
add.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
i++;
tabbedPane.add(new JScrollPane(createTabbedPanel()), "Tab " + i);
}
});
JFrame frame = new JFrame("Browser");
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
buttonPanel.add(add);
frame.add(buttonPanel, BorderLayout.PAGE_START);
frame.add(tabbedPane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createTabbedPanel() {
JPanel panel = new JPanel(new BorderLayout());
JTextField field = new JTextField(50);
JEditorPane pane = new JEditorPane();
pane.setPreferredSize(new Dimension(700, 500));
panel.add(field, BorderLayout.NORTH);
panel.add(pane, BorderLayout.CENTER);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new Browser1();
}
});
}
}
关于java - 我可以在 JEditorPane 上方但 JTabbedPane 下方获取 JTextField 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21297178/