2016-11-24 6 views
1

Ich brauche die Komponenten meines menuPanel zu erweitern, wenn der Rahmen erweitert wird. MenuPanel benutzt GridBayLayout und ich habe sowohl mit den Füll- als auch den Gewichtswerten für das Panel experimentiert, aber nichts scheint die Größe der Komponenten mit dem Frame zu verändern.So erzwingen Sie JPanels mit GridBagLayout, um die Größe mit JFrame zu ändern

Ich vermute, etwas stimmt nicht mit der Art, wie ich menuPanel in der Anzeigeklasse hinzufüge. Außerdem habe ich versucht, ein Layout für TankApplication zu erstellen, und das hat auch nicht funktioniert. Ich verstehe nicht etwas Grundlegendes, denke ich.

Haupt App

public class TanksApplication extends JFrame{ 

     public TanksApplication(){ 
      super("Tanks"); 
     } 

     public static void main (String args[]){ 
      Display display = new Display(); 
      TanksApplication frame = new TanksApplication(); 
      frame.add(display); 
      frame.setSize(600,425); 
      frame.setVisible(true); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setLocationRelativeTo(null); 
     } 
    } 

Display-Klasse

public class Display extends JPanel{  
    public Display(){ 
     menuPanel = new JPanel(new GridBagLayout()); 
     GridBagConstraints con = new GridBagConstraints(); 

     mainMenuImageP = new JPanel(); 
     mainMenuImageP.setBackground(Color.BLACK); 
     con.fill = GridBagConstraints.BOTH; 
     con.gridy = 0; 
     con.gridx = 0; 
     con.gridwidth = 2; 
     con.weightx = 0.5; 
     con.weighty = 0.5; 
     con.anchor = GridBagConstraints.CENTER; 
     con.ipadx = 400; 
     con.ipady = 300; 
     con.insets = new Insets(0,20,0,20); 
     menuPanel.add(mainMenuImageP, con); 

     newGameB = new JButton("New Game"); 
     con.fill = GridBagConstraints.HORIZONTAL; 
     con.gridy = 1; 
     con.gridx = 0; 
     con.gridwidth = 1; 
     con.weightx = 0.5; 
     con.weighty = 0.5; 
     con.anchor = GridBagConstraints.CENTER; 
     con.ipadx = 0; 
     con.ipady = 0; 
     con.insets = new Insets(10,10,10,10); 
     menuPanel.add(newGameB, con); 

     loadGameB = new JButton("Load Game"); 
     con.fill = GridBagConstraints.HORIZONTAL; 
     con.gridy = 1; 
     con.gridx = 1; 
     con.gridwidth = 1; 
     con.weightx = 0.5; 
     con.weighty = 0.5; 
     con.anchor = GridBagConstraints.CENTER; 
     con.ipadx = 0; 
     con.ipady = 0; 
     con.insets = new Insets(10,10,10,10); 
     menuPanel.add(loadGameB, con); 

     add(menuPanel); 
    } 

Antwort

1

Ihr Problem ist, dass Sie hinzufügen Ihre GridBagLayout menuPanel in die Anzeige JPanel verwenden, von denen die letztere verwendet JPanel-Standardflowlayout. FlowLayout passt die Größe der hinzugefügten Komponenten nicht an. Die Lösung: Get Rid von MenuPanel und nur die Anzeige JPanel. Geben Sie ihm ein GridBagLayout und fügen Sie ihm die Komponenten hinzu.

so in Ihrer Anzeige Konstruktor haben

setLayout(new GridBagLayout()); 

die JPanel menuPanel = new JPanel(); löschen und überall Sie menuPane.add(...); haben, ändern Sie es einfach add(...);

z.B.

public class Display extends JPanel{  
    public Display(){ 
     // !! menuPanel = new JPanel(new GridBagLayout()); 
     GridBagConstraints con = new GridBagConstraints(); 

     mainMenuImageP = new JPanel(); 
     mainMenuImageP.setBackground(Color.BLACK); 
     con.fill = GridBagConstraints.BOTH; 
     con.gridy = 0; 
     con.gridx = 0; 
     con.gridwidth = 2; 
     con.weightx = 0.5; 
     con.weighty = 0.5; 
     con.anchor = GridBagConstraints.CENTER; 
     con.ipadx = 400; 
     con.ipady = 300; 
     con.insets = new Insets(0,20,0,20); 
     // !! menuPanel.add(mainMenuImageP, con); 
     add(mainMenuImageP, con); // !! 

     newGameB = new JButton("New Game"); 
     con.fill = GridBagConstraints.HORIZONTAL; 
     con.gridy = 1; 
     con.gridx = 0; 
     con.gridwidth = 1; 
     con.weightx = 0.5; 
     con.weighty = 0.5; 
     con.anchor = GridBagConstraints.CENTER; 
     con.ipadx = 0; 
     con.ipady = 0; 
     con.insets = new Insets(10,10,10,10); 
     // !! menuPanel.add(newGameB, con); 
     add(newGameB, con); // !! 

     loadGameB = new JButton("Load Game"); 
     con.fill = GridBagConstraints.HORIZONTAL; 
     con.gridy = 1; 
     con.gridx = 1; 
     con.gridwidth = 1; 
     con.weightx = 0.5; 
     con.weighty = 0.5; 
     con.anchor = GridBagConstraints.CENTER; 
     con.ipadx = 0; 
     con.ipady = 0; 
     con.insets = new Insets(10,10,10,10); 
     // !! menuPanel.add(loadGameB, con); 
     add(loadGameB, con); // !! 

     // !! add(menuPanel); 
    } 

} 

Beachten Sie, dass, wenn das nicht hilft, dann beachten Sie bitte die Erstellung und minimal example program oder SSCCE veröffentlichen.

+0

Das ist eine großartige Idee, danke. Ich werde es versuchen. Ich wusste nicht, dass Display ein Panel war, ich bin mir nicht sicher warum ich nicht darüber nachgedacht habe. – ulw1

+0

Das war es. Danke noch einmal. – ulw1

+0

@ ulw1: Gern geschehen. Wenn Sie unbedingt ein internes JPanel wie menuPanel verwenden müssen, dann möchten Sie Ihr DisplayPanel ein Layout verwenden, das seine Komponenten preferredSizes wie BorderLayout ändert. –

Verwandte Themen