我怎样才能更改面板高度?

时间:2017-02-18 02:34:46

标签: java swing jframe layout-manager

我只想改变我的面板高度,我该怎么办?

实际上我做了:

setPreferredSize(new Dimension(700,250));

但是我想改变高度而不是触及宽度,我该怎么办? (我希望我的窗户完全适合我的按钮)

谢谢你!

编辑:我的整个代码:

    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;

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 BorderLayout());
    //setSize(new Dimension(750, 550));
    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(700,250));
    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.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();
      }
    });
  }
}

0 个答案:

没有答案