2016-03-24 16 views
0

Kullanıcı arabirimi oluşturmaya çalışıyorum, ancak sipariş edilen bileşenleri yapamadım. JPanel bileşenimi ikinci resim gibi nasıl sipariş edebilirim, herhangi bir fikir var mı? Benim kodları nedenJFrame bileşenleri nasıl sipariş edilir?

ben bu enter image description here

gibi oluşturmak istiyorum enter image description here

burada MigLayout ile bu sorunu çözmek benim java kodları

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


public class ApplicationWindow2 { 

    private JFrame mainJFrame; 
    private JPanel topPanel; 
    private JPanel centerPanel; 
    private JPanel bottomPanel; 


    public ApplicationWindow2(){ 

     mainJFrame = new JFrame("setupbox"); 

     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (InstantiationException e) { 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } catch (UnsupportedLookAndFeelException e) { 
      e.printStackTrace(); 
     } 

     mainJFrame.setMaximumSize(new Dimension(600,600)); 
     mainJFrame.setMinimumSize(new Dimension((int) mainJFrame.getMaximumSize().getWidth()-1,(int) mainJFrame.getMaximumSize().getHeight()-1)); 
     mainJFrame.setResizable(false); 
     mainJFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

     int windowHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight(); 
     int windowWidth = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth(); 
     mainJFrame.setLocation((windowWidth-mainJFrame.getWidth())/2, (windowHeight-mainJFrame.getHeight())/2);// 

     // components 
     mainJFrame.getContentPane().add(getTopPanel(), BorderLayout.PAGE_START); 
     mainJFrame.getContentPane().add(getCenterPanel(), BorderLayout.CENTER); 
     mainJFrame.getContentPane().add(getBottomPanel(), BorderLayout.PAGE_END); 

     mainJFrame.setVisible(true); 
     mainJFrame.pack(); 

    } 

    private JPanel getTopPanel(){ 

     if (topPanel == null){ 

      topPanel = new JPanel(); 
      topPanel.add(new JLabel("top panel")); 
     } 

     return topPanel; 
    } 

    private JPanel getBottomPanel(){ 

     if(bottomPanel == null){ 
      bottomPanel = new JPanel(); 
      bottomPanel.add(new Label("bottom panel")); 
     } 

     return bottomPanel; 
    } 

    private JPanel getCenterPanel(){ 

     if(centerPanel == null){ 

      centerPanel = new JPanel(); 
      centerPanel.setSize(new Dimension((int) mainJFrame.getMaximumSize().getWidth()-100,(int) mainJFrame.getMaximumSize().getHeight()-100)); 
      centerPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 

      BoxLayout boxLayout = new BoxLayout(centerPanel, BoxLayout.Y_AXIS); 
      centerPanel.setLayout(boxLayout); 

      centerPanel.add(getTest()); 
      centerPanel.add(getTest1()); 
      centerPanel.add(Box.createVerticalStrut(30)); 
      centerPanel.add(getTest()); 

     } 

     return centerPanel; 
    } 

    private JPanel getTest(){ 

     JPanel testPanel = new JPanel(new GridBagLayout()); 
     testPanel.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40)); 
     testPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 

     GridBagConstraints c = new GridBagConstraints(); 
     c.fill = GridBagConstraints.NONE; 
     c.gridy = 0; 
     c.insets = new Insets(5, 5, 5, 5); 

     JCheckBox box1 = new JCheckBox("asdasdasdasdasdasd"); 
     c.gridx = 0; 
     c.anchor = GridBagConstraints.WEST; 
     testPanel.add(box1, c); 

     JLabel l3 = new JLabel("lbl3asdssssssssssssssssssssssssss"); 
     l3.setPreferredSize(new Dimension(20,30)); 
     l3.setBorder(BorderFactory.createLineBorder(Color.black)); 
     c.gridx = 1; 
     c.anchor = GridBagConstraints.LINE_END; 
     testPanel.add(l3, c); 

     return testPanel; 

    } 

    private JPanel getTest1(){ 

     JPanel testPanel = new JPanel(new GridBagLayout()); 
     testPanel.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40)); 
     testPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 

     GridBagConstraints c = new GridBagConstraints(); 
     c.fill = GridBagConstraints.NONE; 
     c.gridy = 0; 
     c.insets = new Insets(5, 5, 5, 5); 

     JCheckBox box1 = new JCheckBox("aass1133"); 
     c.gridx = 0; 
     c.anchor = GridBagConstraints.LINE_START; 
     testPanel.add(box1, c); 

     JLabel l3 = new JLabel("lbl3as"); 
     l3.setPreferredSize(new Dimension(20,30)); 
     l3.setBorder(BorderFactory.createLineBorder(Color.black)); 
     c.gridx = 1; 
     c.anchor = GridBagConstraints.LINE_END; 
     testPanel.add(l3, c); 

     return testPanel; 
    } 
} 
+0

'JTable'? 'MigLayout'? – MadProgrammer

+0

Hücrenin farklı boyutunu migLayout olarak ayarlayabilir miyiz? ve intellij'i nasıl ithal edebiliriz? iyi gibi görünüyor – enes

+0

Ben kişisel olarak 'MigLayout' kullanmayın, ama bu tür sorunları çözebilir önerildi. Temel olarak, Jar dosyasını indirmeniz ve projenize eklemeniz gerekir. Intellij ile aşina değil: P – MadProgrammer

cevap

0

ve burada benim olayım olmasıdır kodlama enter image description here

private JPanel getTest1(){ 

     MigLayout layout = new MigLayout("debug"); 
     JPanel content = new JPanel(layout); 
     content.setMaximumSize(new Dimension(getCenterPanel().getWidth(), 40)); 

     JCheckBox a1 = new JCheckBox("d"); 
     content.add(a1, "gapright 10, height 30, width 250, cell 0 0"); 

     JLabel label = new JLabel(); 
     label.setBackground(Color.RED); 
     label.setOpaque(true); 
     content.add(label, "gapright 10, height 30, width 30, cell 1 0"); 

     JButton btn = new JButton("try again"); 
     content.add(btn,"height 30, width 80, cell 2 0"); 

     return content; 

    }