2014-01-23 17 views
0

我正在研究一個帶有選項卡的簡單網絡瀏覽器。在tabbedpane下面添加文本字段時遇到了很多麻煩,就像在Chrome中一樣。繼承人我到目前爲止: 公共類瀏覽器 { 私人JFrame框架; private JPanel panel Top; 私有JEditorPane編輯器; 私人JScrollPane滾動; 私人JTextField字段; 私人JButton按鈕; 私人JButton家; 私人網址; private JMenu file = new JMenu(「File」); private JMenuItem exit = new JMenuItem(「Exit」); private JMenuBar menuBar = 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 Dimension dim = new Dimension(nt.getIconWidth()+ 2, nt.getIconHeight()+ 2); (); public Browser() { initComponents();我可以在JEditorPane上面獲得JTextField,但是在JTabbedPane下面?

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); 
} 
} 

謝謝!

+0

請上傳與各界和初始化一個編譯例子。 –

+0

請注意,您的問題幾乎肯定在於佈局管理器而不是單個組件,所以我編輯了標籤 – Pureferret

+0

我已經添加了初始化和其他使用的方法 – intrigatory57

回答

1

看看這個例子。我不想嘗試猜測哪個變量是哪個組件的,所以我列舉了一個類似的例子。你需要做的是把你想要的所有東西放在標籤裏面。所以基本上每個小組都會有自己的文本領域和編輯窗格

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(); 
      } 
     }); 
    } 
} 
+0

非常感謝!這實際上幫助我意識到代碼中的一個主要缺陷 – intrigatory57

相關問題