什么是Java可调整大小的布局管理器?

时间:2017-02-25 01:09:15

标签: java layout jframe layout-manager

我想调整我的JFrame布局管理器(实际上是边框布局)

所以我使用了setPreferredSize但没有任何反应?有些人告诉我使用另一个布局管理器,那么我应该使用什么布局管理器才能扩展我的某个面板的尺寸? 谢谢 ! 我的代码:

import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.SwingUtilities;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Graphics;

public class OptimiserMonWifi extends JFrame implements MouseListener {
  private JPanel panel;
  private JPanel panel2;

  private JButton bouton1;
  private JButton bouton2;
  private JButton bouton3;
  private JButton bouton4;

  private JCheckBox check1;
  private JCheckBox check2;


  public OptimiserMonWifi()
  {
    super("Optimiser Mon Wifi");
    //this.setLayout(new CardLayout());
    this.setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    this.bouton1 = new JButton("Afficher");
    this.bouton2 = new JButton("Réinitialiser");
    this.bouton3 = new JButton("Précédent");
    this.bouton4 = new JButton("Suivant");
    this.check1 = new JCheckBox("Emission");
    this.check2 = new JCheckBox("Coordonnées");
    this.panel = new JPanel();
    //panel.setLayout(new GridLayout(4,2));
    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
    panel.setBackground(Color.blue);
    this.panel2 = new JPanel();
    panel2.setBackground(Color.red);
    setPreferredSize(new Dimension(1000,500));
    panel2.setPreferredSize(new Dimension(1000,500));
    panel2.addMouseListener(this);



    this.panel.add(bouton1);
    this.panel.add(bouton2);
    this.check1.addActionListener(new StateListener());
    this.check2.addActionListener(new StateListener());
    this.panel.add(check1);
    this.panel.add(check2);
    this.panel.add(bouton3);
    this.panel.add(bouton4);

    /*this.getContentPane().add(panel, BorderLayout.NORTH);
    this.getContentPane().add(panel2, BorderLayout.CENTER);*/

    /*this.getContentPane().add(panel, BUTTONPANNEL);
    this.getContentPane().add(panel2, TEXTPANNEL);*/


    this.pack();
    this.setVisible(true);
  }


  public void mouseClicked(MouseEvent e) {
    int x=e.getX();
    int y=e.getY();
    System.out.println("x : "+x+" ; y : "+y);//these co-ords are relative to the component
  }

  //Méthode appelée lors du survol de la souris

  public void mouseEntered(MouseEvent event) { }

  //Méthode appelée lorsque la souris sort de la zone du bouton

  public void mouseExited(MouseEvent event) { }

  //Méthode appelée lorsque l'on presse le bouton gauche de la souris

  public void mousePressed(MouseEvent event) { }

  //Méthode appelée lorsque l'on relâche le clic de souris

  public void mouseReleased(MouseEvent event) { }       


  class StateListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
      System.out.println("source : " + ((JCheckBox)e.getSource()).getText() + " - état : " + ((JCheckBox)e.getSource()).isSelected());
    }
  }



  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        new OptimiserMonWifi();
      }
    });
  }
}

1 个答案:

答案 0 :(得分:0)

您没有调整布局管理器的大小;您调整JFrame或其子(Ren)的大小。你想要“扩大我的某个面板的尺寸”是什么意思?你什么时候想要这样做 - 在使JFrame可见之后,或者最初?在没有向我们展示您尝试的一些代码的情况下,我们只能进行猜测。