2016-04-13 10 views
0

私はもう一日JFrameの別の質問をして問題を突き止めることができましたが、別の問題に直面しました。私は私のUIが写真のように見えるようにする必要がありますが、LayoutManagerを使ってそれらを重ねる方法を理解することはできません。両方でBorderLayout Southを使用すると、最後に追加されたものだけが表示されます。任意のヒント?JFrameとアラインメントの使用

それがどのように見えるか相続人

enter image description here

そして、ここに私のコードです:

JFrame frame = new JFrame(); 
    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0)); 
    JPanel textPanel = new JPanel(); 

    textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS)); 

    JTextField textField = new JTextField(); 
    textField.setMaximumSize((new Dimension(10000,25))); 

    textPanel.add(new JLabel("Filename:")); 
    textPanel.add(textField); 
    textPanel.add(new JButton("Load")); 

    frame.add(textPanel); 

    buttonPanel.add(new JButton("Play")); 
    buttonPanel.add(new JButton("Stop")); 


    frame.add(buttonPanel, BorderLayout.SOUTH); 

    frame.setSize(500, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    frame.setVisible(true); 
+0

化合物の成分は、 – MadProgrammer

答えて

1

Compound Panels

textPanebuttonPaneを置くために別の容器を利用します
import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new BorderLayout()); 

      JPanel textPane = new JPanel(new GridBagLayout()); 
      JTextField textField = new JTextField(20); 

      textPane.add(new JLabel("Filename:")); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.weightx = 1; 
      gbc.fill = GridBagConstraints.HORIZONTAL; 
      textPane.add(textField, gbc); 
      textPane.add(new JButton("Load")); 

      JPanel buttonPane = new JPanel(); 
      buttonPane.add(new JButton("Play")); 
      buttonPane.add(new JButton("Stop")); 

      JPanel southPane = new JPanel(new GridLayout(2, 0)); 
      southPane.add(textPane); 
      southPane.add(buttonPane); 

      add(new JLabel("I'm in the center")); 
      add(southPane, BorderLayout.SOUTH); 
     } 

    } 

} 
関連する問題