2016-12-17 82 views
-1

我想創建一個對話框,其中包含兩個splitPanel和一個菜單欄。兩個splitPanel都包含jlist。爲什麼我在jmenubar旁邊有一個白色區域?

但是,當我運行我的代碼,我得到白色區域旁邊的菜單欄。在下面的圖片白色區域創建旁邊組織

enter image description here

這裏是我的部分代碼:

public class HistList extends JPanel { 
JTable table = new JTable(); 
JMenuItem remove = new JMenuItem("Remove"); 
JScrollPane scrollPane = new JScrollPane(); 
MyTableModel model = new MyTableModel(); 

HistList() { 
    try { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
     ex.printStackTrace(); 
    } 
    String[] title = {" ", "Time", "Name", "Location"}; 
    Object[][] data = { 
     {false, "01:22:16", "Google", "http://www.google.com"}, 
     {false, "01:22:16", "Yahoo - login", "https://login.yahoo.com/?.src=ym&.intl=us&.lang=en-US&.done=https%3a//mail.yahoo.com"}, 
     {false, "01:22:29", "Oracle | Integrated Cloud Applications and Platform Services", "https://www.oracle.com/index.html"} 
    }; 
    for (int i = 0; i < data.length; i++) { 
     model.addRow(data[i]); 
    } 
    this.table = new JTable(model); 
    this.table.setShowGrid(false); 
    this.scrollPane = new JScrollPane(this.table); 
    Dimension size = new Dimension(510, 380); 
    this.scrollPane.setPreferredSize(size); 
    this.scrollPane.getViewport().setBackground(Color.WHITE); 
    JTextField name = new JTextField(); 
    name.setPreferredSize(new Dimension(scrollPane.getWidth(), 25)); 
    JTextField locationText = new JTextField(); 
    locationText.setPreferredSize(new Dimension(scrollPane.getWidth(), 25)); 
    JPanel textPane = new JPanel(); 
    JLabel nameLabel = new JLabel("Name:"); 
    JPanel textPane1 = new JPanel(); 
    textPane1.setLayout(new BoxLayout(textPane1, BoxLayout.X_AXIS)); 
    textPane1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 
    textPane1.add(Box.createHorizontalGlue()); 
    textPane1.add(nameLabel); 
    textPane1.add(Box.createRigidArea(new Dimension(21, 0))); 
    textPane1.add(name); 
    JLabel locationLabel = new JLabel("Location:"); 
    JPanel textPane2 = new JPanel(); 
    textPane2.setLayout(new BoxLayout(textPane2, BoxLayout.X_AXIS)); 
    textPane2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 
    textPane2.add(Box.createHorizontalGlue()); 
    textPane2.add(locationLabel); 
    textPane2.add(Box.createRigidArea(new Dimension(5, 0))); 
    textPane2.add(locationText); 
    textPane.setLayout(new BoxLayout(textPane, BoxLayout.Y_AXIS)); 
    textPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2)); 
    textPane.add(Box.createVerticalGlue()); 
    textPane.add(textPane1); 
    textPane.add(Box.createRigidArea(new Dimension(0, 5))); 
    textPane.add(textPane2); 
    JPanel contentPane = new JPanel(); 
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 
    contentPane.add(scrollPane); 
    contentPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2)); 
    contentPane.add(Box.createVerticalGlue()); 
    contentPane.add(Box.createRigidArea(new Dimension(0, 5))); 
    contentPane.add(textPane); 
    JPanel pane = new JPanel(new BorderLayout()); 
    pane.setBorder(BorderFactory.createEmptyBorder(15, 0, 0, 0)); 
    pane.setBackground(Color.WHITE); 
    String[] dates = {"12/15/2016", "12/30/2016"}; 
    JList list; 
    list = new JList(dates); 
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
    JLabel label = new JLabel("Browsing Date"); 
    pane.add(label, BorderLayout.NORTH); 
    pane.add(list, BorderLayout.WEST); 
    pane.setPreferredSize(new Dimension(150, 380)); 
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pane, contentPane); 
    JMenuBar organize = new JMenuBar(); 
    organize.setOpaque(true); 
    organize.setBackground(getBackground()); 
    JMenu menu = new JMenu("Organize"); 
    menu.add(remove); 
    remove.setEnabled(false); 
    menu.setOpaque(true); 
    menu.setBackground(getBackground()); 
    menu.setIcon(new ImageIcon(Hello.class.getResource("/rsz_todo-512.png"))); 
    organize.add(menu); 
    JPanel finale = new JPanel(new BorderLayout()); 
    finale.setOpaque(true); 
    finale.add(organize,BorderLayout.NORTH); 
    finale.add(splitPane,BorderLayout.CENTER); 
    setOpaque(true); 
    add(finale); 
} 

我無法弄清楚什麼是創造這個白色區域。這部分我很新。任何人都可以請幫我解決它?請原諒我的愚蠢問題。提前致謝。

+3

1)爲了更好地提供幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)'finale.add(組織,BorderLayout.NORTH);'爲什麼不使用'setJMenuBar(..)'? 3)所有的'setBackground(..)'調用是什麼?這應該由PLAF處理。 –

+0

感謝您的建議,我已經發布了我的完整代碼 – IAmBlake

+0

*「完整的代碼」*我從來沒有建議過,並且它不是任何情況下的完整代碼(缺少進口和將屏幕放在屏幕上的主要方法)。當我提供鏈接時,請*閱讀*他們。 –

回答

2

但是當我運行我的代碼,我得到的白色區域菜單欄

finale.add(organize,BorderLayout.NORTH); 

旁邊不要將菜單欄添加到幀的內容窗格中。

JFrame有一個爲菜單欄保留的特殊位置。

  1. 閱讀JFrame的API找到適當的方法

  2. 閱讀從如何使用菜單的詳細信息和工作實例的Swing指南的部分。

注意從Swing教程中的工作實例將幫助你更好的,讓你的GUI組件在Event Dispatch Thread (EDT)創建的結構你的代碼。

該代碼也更多地以MCVE的形式出現,儘管示例中的代碼過多。既然你的問題是關於一個菜單欄,你需要的只是JFrame,一個JMenuBar和一個JMenu來展示你陳述的問題。

保留一個鏈接到教程方便所有Swing基礎的例子。

相關問題