2016-12-17 3 views
-1

두 개의 splitPanel과 메뉴 모음이 포함 된 대화 상자를 만들려고합니다. 두 splitPanel 모두 jlist를 포함합니다.jmenubar 옆에 흰색 영역이 나타나는 이유는 무엇입니까?

하지만 내 코드를 실행할 때 메뉴 막대 옆에 흰색 영역이 표시됩니다. 아래 그림에서 흰색 영역은 옆에 생성되고으로 구성됩니다.

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

나는이 흰색 영역을 만드는 것을 알아낼 수 없습니다 :

enter image description here

다음은 코드 내 일부입니다. 나는이 섹션에서 아주 새로운 것이다. 누구든지 나를 고칠 수 있도록 도와 주시겠습니까? 내 어리석은 질문을 용서해주세요. 미리 감사드립니다. 난 내 코드를 실행하면

+3

1) 더 나은 도움을 받으려면 [MCVE] 또는 [Short, Self Contained, Correct Example] (http://www.sscce.org/)를 게시하십시오. 2)'finale.add (organize, BorderLayout.NORTH);'setJMenuBar (..)'를 사용하지 않는 이유는 무엇입니까? 3) 모든'setBackground (..)'호출은 무엇입니까? 이것은 PLAF에서 처리해야합니다. –

+0

제안을 주셔서 감사합니다. 지금 내 전체 코드를 게시했습니다. – IAmBlake

+0

* "전체 코드"* 제안하지 않았으며 어떠한 경우에도 전체 코드가 아닙니다 (가져 오기가 누락되어있어 패널을 화면에 표시하는 기본 방법이 없음).). 링크를 제공 할 때 * 읽으십시오. –

답변

2

는하지만 난 프레임의 내용 창에 메뉴 표시 줄을 추가하지 마십시오 메뉴 바

finale.add(organize,BorderLayout.NORTH); 

옆에 흰색 영역을 얻는다.

JFrame에는 메뉴 표시 줄 용으로 예약 된 특별한 장소가 있습니다.

  1. 는 적절한 방법

  2. 더 많은 정보와 작업 예제 메뉴를 사용하는 방법에 대한 스윙 튜토리얼에서 섹션을 읽기를 찾기 위해 JFrame의 API를 읽어보십시오.

주 스윙 튜토리얼에서 작업 예제는 GUI 구성 요소가 Event Dispatch Thread (EDT)에 생성되도록 당신이 더 나은 코드를 구성하는 데 도움이됩니다.

코드는 MCVE 형태로 더 많습니다. 예제의 코드조차도 과도합니다. 질문은 메뉴 막대에 관한 것이므로 JFrame, JMenuBar 및 JMenu 만 있으면 문제를 설명 할 수 있습니다.

모든 스윙 기본 사항에 대한 예제는 편리한 튜토리얼 링크를 유지하십시오.

관련 문제