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);
}
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
Das war es. Danke noch einmal. – ulw1
@ 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. –