2013-09-14 3 views
0

Как я могу заставить кнопки отображаться в поле со списком здесь? Другими словами, есть кнопка 1 и кнопка 2 прямо под полем со списком?Размещение кнопок под полем со списком

public class GUI extends JFrame implements ListSelectionListener, ActionListener { 

    private JPanel myPanelA; 
    private JSplitPane itemPane; 

    public static void startWindowsGui () { 

     SwingUtilities.invokeLater (new Runnable () { 
      public void run () { 

       GUI gui = new GUI (); 


       gui.setVisible (true); 
      } 
     }); 
    } 

    public GUI() { 

     // Set the layout to a grid 
     setLayout (new BorderLayout (5, 5)); 


     setTitle ("UI "); 
     setSize (800, 600); 
     setDefaultCloseOperation (EXIT_ON_CLOSE); 
     setBackground (new Color (15, 255, 10)); 


     addComponents (); 
    } 

    private void addComponents () { 

     JSplitPane mainPane = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT); 
     itemPane = new JSplitPane (JSplitPane.VERTICAL_SPLIT); 

     mainPane.add (PanelA (), JSplitPane.LEFT); 
     mainPane.add (itemPane, JSplitPane.RIGHT); 
     mainPane.setOneTouchExpandable (true); 

     itemPane.setOpaque(true); 
     itemPane.setBackground(new Color(0xffffffc0)); 
     BufferedImage myPicture = null; 
     try { 
      myPicture = ImageIO.read(new File("C:/Users/Desktop/image.jpg")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     JLabel picLabel = new JLabel(new ImageIcon(myPicture)); 
     //add(picLabel); 
     itemPane.add(picLabel); 

     add (mainPane, BorderLayout.CENTER); 
    } 


    private JPanel PanelA () { 

     myPanelA = new JPanel (); 


     myPanelA.setLayout (new BorderLayout (0, 0)); 


     myPanelA.add (buttonPanel (), BorderLayout.NORTH); 

     myPanelA.setBorder (new EmptyBorder (0, 0, 0, 0)); 
     myPanelA.setBackground(new Color(0,0,0)); 

     return myPanelA; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 


    } 

    @Override 
    public void valueChanged(ListSelectionEvent arg0) { 


    } 


    private JPanel buttonPanel () { 
     // Create the panel 
     JPanel addButton = new JPanel (); 

     JPanel cards; //a panel that uses CardLayout 
     String BUTTONPANEL = "Card with JButtons"; 
     String TEXTPANEL = "Card with JTextField"; 

     JPanel comboBoxPane = new JPanel(); //use FlowLayout 
     String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; 
     JComboBox cb = new JComboBox(comboBoxItems); 
     cb.setEditable(false); 
      //cb.addItemListener(this); 
      comboBoxPane.add(cb); 

      //Create the "cards". 
      JPanel card1 = new JPanel(); 
      card1.add(new JButton("Button 1")); 
      card1.add(new JButton("Button 2")); 

      JPanel card2 = new JPanel(); 
      card2.add(new JTextField("TextField", 10)); 

      //Create the panel that contains the "cards". 
      cards = new JPanel(new CardLayout()); 
      cards.add(card1, BUTTONPANEL); 
      cards.add(card2, TEXTPANEL); 

      addButton.add(comboBoxPane, BorderLayout.PAGE_START); 
      addButton.add(cards, BorderLayout.CENTER); 

     return addButton; 
    } 
} 

enter image description here

ответ

2

Использование BoxLayout для панели вместо него дефолтный макет, который FlowLayout.

См. Эту ссылку: How to Use BoxLayout. В вашем случае поможет BoxLayout.Y_AXIT.

Например:

JPanel panel = new JPanel(); 
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); 
+0

Вы "могли бы" использовать 'GridBagLayout' а;) – MadProgrammer

+0

@MadProgrammer:' GridBag' это один из моих любимых :) – Azad

1

Вам нужно организовать макет, GridLayout очень хорошо.

Необходимо сначала до GridLayout(2, 1).

В первой строке будет назначена в поле со списком,

и второй ряд для панели с GridLayout(1, 2) для двух кнопок.

setLayout(new GridLayout(2, 1)); 
add(comboBox); 
JPanel inner = new JPanel(); 
inner.setLayout(new GridLayout(1, 2)); 
add(inner); 
inner.add(button1); 
inner.add(button2); 
Смежные вопросы