2013-02-17 8 views
5

मैं जावा में जीयूआई का उपयोग करने के लिए पूरी तरह से नया हूं, इसलिए मुझे कुछ परेशानी हो रही है कि मुझे जो कुछ भी चाहिए उसे संरेखित करना है। मुझे अपने जेएफआरएएम में पैनल करना है कि मुझे पैनल में केंद्रित होने की आवश्यकता वाले पैनलों में से एक में बाएं (एक बाएं, दाएं से एक) और कुछ बटनों को संरेखित करने की आवश्यकता है। मेरा कोड यहाँ है।मैं जेपीनेल/जेएफआरम्स में तत्वों को कैसे संरेखित कर सकता हूं?

package application; 

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.io.*; 
import java.nio.*; 
import java.util.*; 

public class Main extends JPanel 
{ 
    public static void main(String[] args) 
    { 
     //set the ui to the native OS 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     }catch(ClassNotFoundException | InstantiationException | IllegalAccessException 
     | UnsupportedLookAndFeelException e)         
     { 
     } 

     JFrame frame = new JFrame("Application Name"); 
     Menu menu = new Menu(); 
     JPanel iconPanel = new JPanel(); 
     final JPanel grid = new JPanel(new FlowLayout()); 
     JButton firewallButton = new JButton("Firewall"); 
     JButton networkButton = new JButton("Network"); 
     JButton printerButton = new JButton("Printer"); 

     int iconPanelSizeX; 
     int iconPanelSizeY; 
     int gridSizeX; 
     int gridSizeY; 
     int gridPosition; 

     //frame setting 
     frame.setSize(800, 600); 
     frame.setMinimumSize(new Dimension(800, 600)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

     //add grid and iconPanel JPanels to the frame 
     frame.add(iconPanel); 
     iconPanel.add(firewallButton); 
     iconPanel.add(networkButton); 
     iconPanel.add(printerButton); 
     frame.add(grid);   

     //iconPanel settings 
     iconPanel.setBorder(BorderFactory.createLoweredSoftBevelBorder()); 
     iconPanel.setBackground(Color.gray); 
     iconPanel.setLayout(new FlowLayout()); 
     iconPanel.setSize(new Dimension(100, 600)); 
     iconPanel.setVisible(true); 

     //grid setting 
     grid.setBackground(Color.red); 
     grid.setSize(new Dimension(700, 600)); 
     grid.setVisible(true); 

     //this is for resizing components when the user resizes the window 
     int counter = 0; 
     while(counter == 0) 
     { 
      firewallButton.setSize(new Dimension(iconPanel.getWidth(), 50)); 
      networkButton.setSize(new Dimension(iconPanel.getWidth(), 50)); 
      printerButton.setSize(new Dimension(iconPanel.getWidth(), 50)); 
      iconPanelSizeX = frame.getWidth()/10; 
      iconPanelSizeY = frame.getHeight(); 
      gridSizeX = (frame.getWidth()/10) * 9; 
      gridSizeY = frame.getHeight(); 
      iconPanel.setSize(new Dimension(iconPanelSizeX, iconPanelSizeY)); 
      grid.setSize(new Dimension(gridSizeX, gridSizeY)); 
     } 
    } 
} 

आप देख सकते हैं, दूसरी JPanel (ग्रिड) फ्रेम के दाईं ओर के साथ नहीं मिलाया गया है, और iconTray अंदर बटन या तो केंद्र नहीं है। मुझे एहसास है कि ये शायद दोनों सरल लेआउट फिक्स हैं, लेकिन मुझे कोई संकेत नहीं है कि कहां से शुरू किया जाए।

उत्तर

7

का उपयोग JFrame की साधारण बंटवारे के लिए आप 1 पंक्ति और 2 colums साथ GridLayout उपयोग कर सकते हैं।

frame.setLayout(new GridLayout(1,2,3,3)); //3,3 are gaps 
frame.add(grid); 
frame.add(iconPanel); 

पैनलों में घटकों केंद्रित आप FlowLayout जो JPanels पर डिफ़ॉल्ट सेट कर रहा है उपयोग कर सकते हैं:

यह manualy कर:

grid.setLayout(new FlowLayout()); //Centered components 

grid.setLayout(new FlowLayout(FlowLayout.LEFT,3,3)); //Components aligned to left 

grid.setLayout(new FlowLayout(FlowLayout.RIGHT,3,3)); //Components aligned to right 

यह है कि यह कैसे दिखता है:

enter image description here

इसके अलावा, कुछ अवलोकन:

  • कभी भी अपने घटकों के लिए setXXXSize() विधियों को कॉल न करें;

  • कोशिश JFrame के लिए setSize(); बुला बचने के लिए, बजाय pack(); कॉल करने के लिए;

  • कोड के अंत में setVisible(true); पर कॉल करें;

सभी अपने विशाल कोड कर सकते हैं यह करने के लिए "उजागर किया गया":

import javax.swing.*; 
import java.awt.*; 


public class Main extends JPanel 
{ 
    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("Application Name"); 
     JPanel iconPanel = new JPanel(); 
     JPanel grid = new JPanel(new FlowLayout()); 
     JButton firewallButton = new JButton("Firewall"); 
     JButton networkButton = new JButton("Network"); 
     JButton printerButton = new JButton("Printer"); 


     frame.add(iconPanel); 
     iconPanel.add(firewallButton); 
     iconPanel.add(networkButton); 
     iconPanel.add(printerButton); 
     grid.setBackground(Color.GREEN); 

     frame.setLayout(new GridLayout(1,2,3,3)); 
     frame.add(grid); 
     frame.add(iconPanel); 


     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+0

धन्यवाद, यह बहुत मदद की। वास्तव में एक बात है जिसे मैं पूछना भूल गया था, और यही है कि बटन को लंबवत रूप से संरेखित करना है - मुझे कुछ बाद में जोड़ना है। 'पैक() 'के लिए – user2067364

+1

+1; अधिक [यहां] (http://stackoverflow.com/a/14927280/230513)। – trashgod

+0

आपको कुछ अन्य लेआउट प्रबंधक का उपयोग करना होगा। बहुत ही बुनियादी संरेखण के लिए आप 'ग्रिडलाउट' या 'बॉक्सलाउट' का उपयोग कर सकते हैं, लेकिन कुछ और जटिल के लिए मैं 'ग्रिडबैडआउटआउट' या 'मिगलाउट' के उपयोग का सुझाव देता हूं। बस उस पर गूगल। –

1

मैं अपने JFrame में पैनलों है कि मैं संरेखित करने के लिए पैनल मैं पैनल में केंद्रित करने की आवश्यकता है में से एक में कुछ बटन (बाईं ओर एक, सही करने के लिए एक) और जरूरत के लिए है। मेरा कोड यहाँ है।

मुझे एहसास है कि ये शायद दोनों साधारण लेआउट फिक्सेस हैं, लेकिन मेरे पास सुराग शुरू नहीं है।

सरल FlowLayout से अधिक जटिल लेआउट का उपयोग करें जिसका आप वास्तव में उपयोग कर रहे हैं। मेरा सुझाव है कि आप

  • GridBagLayout
  • BoxLayout

चेक references here

2

मैं सुझाव है कि आप कुछ समय A Visual Guide to Layout Managers माध्यम से जा रहा ले। यह आपको लेआउट प्रबंधकों से परिचित होने में मदद करेगा जो मानक API के साथ उपलब्ध हैं। यह पता लगाने के लिए कुछ अनुभव और कड़ी मेहनत होती है कि आप कौन सी सटीक दिखने के लिए सही टूल हैं। एक बार जब आप मानक एपीआई से उपलब्ध हो जाते हैं, तो आपको तीसरे पक्ष के लेआउट मैनेजर एपीआई के लिए भी देखना चाहिए जो अन्य विकल्प प्रदान करता है।

5

बटन को लंबवत रूप से संरेखित कैसे करें?

यह उदाहरण फ्रेम के डिफ़ॉल्ट BorderLayout की WEST क्षेत्र में एक ऊर्ध्वाधर Box उपयोग करता है:

enter image description here

import java.awt.*; 
import javax.swing.*; 

/** @see http://stackoverflow.com/a/14927280/230513 */ 
public class Main { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       display(); 
      } 
     }); 
    } 

    private static void display() throws HeadlessException { 
     JFrame frame = new JFrame("Application Name"); 
     JButton firewallButton = new JButton("Firewall"); 
     JButton networkButton = new JButton("Network"); 
     JButton printerButton = new JButton("Printer"); 

     //iconPanel settings 
     Box iconPanel = new Box(BoxLayout.Y_AXIS); 
     iconPanel.add(firewallButton); 
     iconPanel.add(networkButton); 
     iconPanel.add(printerButton); 
     iconPanel.setBackground(Color.gray); 
     iconPanel.setVisible(true); 
     frame.add(iconPanel, BorderLayout.WEST); 

     //grid setting 
     JPanel grid = new JPanel() { 

      @Override 
      // arbitrary placeholder size 
      public Dimension getPreferredSize() { 
       return new Dimension(320, 230); 
      } 
     }; 
     grid.setBackground(Color.red); 
     frame.add(grid, BorderLayout.CENTER); 

     //frame setting 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+0

यह भी देखें [* प्रारंभिक थ्रेड *] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) और यह [क्यू एंड ए] (http://stackoverflow.com/q/7229226/ 230,513)। – trashgod

संबंधित मुद्दे

 संबंधित मुद्दे