2017-02-25 37 views
-1

我想调整我的JFrame布局管理器(实际上是边框布局)什么是Java可调整大小的布局管理器?

所以我用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(); 
     } 
    }); 
    } 
} 
+0

如果你想面板比父容器大,那么你需要使用'JScrollPane' – MadProgrammer

+0

我不希望我的面板比我的父容器大?为什么?它实际上是我的代码试图做什么? (如果是的话,我不知道?) –

+1

好的,你有没有尝试过使用'GridBagLayout'?它会尊重你的组件的首选大小 – MadProgrammer

回答

0

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

+0

我的意思是“扩大我的pannels之一的大小”,但我会编辑我的问题发布我的代码,所以你将能够看到: –

+0

我想延长我的panel2的高度,因为它实际上超薄,我希望它有更高的高度 –