【发布时间】:2014-02-13 08:26:56
【问题描述】:
我正在开发一个带有标签的简单网络浏览器。我在标签窗格下方添加文本字段时遇到了很多麻烦,就像它在 chrome 中一样。继承人我到目前为止: 公共类浏览器 { 私有 JFrame 框架; 私人 JPanel 面板顶部; 私人 JEditorPane 编辑器; 私人 JScrollPane 滚动; 私有 JTextField 字段; 私有 JButton 按钮; 私人 JButton 住宅; 私人网址网址; 私有 JMenu 文件 = new JMenu("文件"); 私人 JMenuItem 出口 = 新 JMenuItem("Exit"); 私人 JMenuBar menuBar = new JMenuBar(); 私人 ImageIcon nt = new ImageIcon("./Icons/newTab.jpg"); 私有 ImageIcon cl = new ImageIcon("./Icons/close.png"); 私有 JButton newTab = new JButton(cl); 私人 JTabbedPane tabbedPane = new JTabbedPane(); 私人 int tabCounter = 0; 私人维度暗淡=新维度(nt.getIconWidth()+2, nt.getIconHeight()+2); 公共浏览器() { 初始化组件();
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);
}
}
谢谢!
【问题讨论】:
-
请发布一个包含所有字段和初始化的可编译示例。
-
请注意,您的问题几乎肯定出在布局管理器而不是单个组件上,所以我编辑了您的标签
-
我已经添加了初始化以及使用的其他方法
标签: java swing layout-manager jtabbedpane