2017-02-25 70 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的高度,因爲它實際上超薄,我希望它有更高的高度 –