【问题标题】:What are Java resizable Layout Managers please?请问什么是 Java 可调整大小的布局管理器?
【发布时间】:2017-07-16 00:02:45
【问题描述】:

我想调整我的 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();
      }
    });
  }
}

【问题讨论】:

  • 如果希望面板大于父容器,那么需要使用JScrollPane
  • 我不希望我的面板比我的父容器大,为什么?这实际上是我的代码想要做的吗??? (如果是我不知道?)
  • 好的,你试过使用GridBagLayout吗?它将尊重您的组件的首选大小
  • 不,我没有,但我读过它和许多其他布局,不知道该尝试哪个我现在试试这个!
  • 哦,不,它有效,它有效,我只是忘记了“这个”。在“setPreferredSize(new Dimension(1000,500));”前面不管怎样,谢谢你 !我会寻找这个布局只是为了知识

标签: java layout jframe layout-manager


【解决方案1】:

您不会调整布局管理器的大小;您调整 JFrame 或其子项的大小。你是什​​么意思你想“扩大我的一个面板的大小”?您想在什么时候执行此操作 - 在使 JFrame 可见之后,还是最初?如果没有向我们展示您尝试过的一些代码,我们只能做出疯狂的猜测。

【讨论】:

  • 我的意思是“扩展我的一个面板的大小”,但我会编辑我的问题以发布我的代码,以便您可以看到:
  • 我想延长我的 panel2 的高度,因为它实际上是超薄的,我希望它有更大的高度
猜你喜欢
  • 2010-11-29
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 2011-05-03
相关资源
最近更新 更多