Fenstergröße ändern, Panel soll gleich bleiben

Progrmmierer89

Neues Mitglied
Servus,

Ich möchte momentan ein Spiel programmieren bei dem eine Bestenliste nur dann angezeigt wird, wenn man auf einen Button klickt.

Mein Fenster soll um die Breite der Tabelle breiter werden und das Panel, welches schon angezeigt wird, gleich breit bleiben.

Wie kann ich das programmieren?
 
Hier hab ich mal ein kleines Testprogram zum probieren geschrieben.

Java:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

public class GridBagLayoutDemo extends JFrame {
   
    public GridBagLayoutDemo() {
       
        super("Demo");
       
        setDefaultCloseOperation(EXIT_ON_CLOSE);
       
        JPanel panel = new JPanel();
       
        panel.setSize(1000, 800);
       
        GridBagLayout gbl2 = new GridBagLayout();
       
        setLayout(gbl2);
       
        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.fill = GridBagConstraints.BOTH;
       
        gbc2.gridx = 0;
        gbc2.gridy = 0;
        gbc2.weightx = 1.0;
        gbc2.weighty = 1.0;
        gbl2.setConstraints(panel, gbc2);       
       
                       
        GridBagLayout gbl = new GridBagLayout();
       
        panel.setLayout(gbl);
       
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
       
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;           
        gbc.gridheight = 2;
        JButton btn = new JButton("Klick mich");
        gbl.setConstraints(btn, gbc);
        panel.add(btn);
       
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;           
        gbc.gridheight = 1;
        JButton btn2 = new JButton("Klick mich");
        gbl.setConstraints(btn2, gbc);
        panel.add(btn2);
       
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;           
        gbc.gridheight = 1;
        JButton btn3 = new JButton("Klick mich");
        gbl.setConstraints(btn3, gbc);
        panel.add(btn3);
       
        gbc2.gridx = 2;
        gbc2.gridy = 0;
        gbc2.weightx = 1.0;
        gbc2.weighty = 1.0;
        JButton btn4 = new JButton("Klick mich");
        gbl2.setConstraints(btn4, gbc2);
        btn4.setVisible(false);
       
        btn.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
               
                if(btn4.isVisible()){
                    btn4.setVisible(false);
                    setSize(1000, 800);
                    }
                    else{
                        btn4.setVisible(true);
                        setSize(1500, 800);
                    }
               
            }
           
        });
       
        add(panel);
        add(btn4);
       
        setSize(1000, 800);
       
    }

    public static void main(String[] args) {
       
        GridBagLayoutDemo x = new GridBagLayoutDemo();
        x.setVisible(true);

    }

}

Mein Fenster wird zwar wie gewünscht breiter, allerdings wird mein Panel schmaler.
 

Zurück
Oben