使用Grouplayout和Flowlayout一起使用:输出不会出现在中心



我正在尝试使用布局创建一个UI。我想要广播按钮和2个文本字段和按钮。对于无线电按钮,我使用了流布局,用于文本字段和按钮,我使用的是组布局。但是我要获得的输出是分散的,并且文本字段被拉伸。我希望所有这些组件都位于窗户的中央。以下是我的代码。

  package DecodeTool1;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      public class SwingLayoutDemo {
          private JFrame mainFrame;
            private JLabel file_1;
          private JLabel file_2;
        private JPanel RPanel;
         private JPanel TextPanel;
         private JPanel panel;

         public SwingLayoutDemo(){
                prepareGUI();
         }
              public static void main(String[] args){
            SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();  
            swingLayoutDemo.showGroupLayoutDemo();       
       }
       private void prepareGUI(){
          mainFrame = new JFrame("Java SWING Examples");
         mainFrame.setSize(400,400);
              mainFrame.setLayout(new GridLayout(3, 2,20,10));

  file_1 = new JLabel("",JLabel.CENTER);
  file_2 = new JLabel("",JLabel.CENTER);        
  file_2.setSize(100,100);
             mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent){
                 System.exit(0);
                 }        
          });    
         panel = new JPanel();
          TextPanel = new JPanel();
            mainFrame.add(file_1);
           mainFrame.add(file_2);
          mainFrame.setVisible(true);  
      }
        private void showGroupLayoutDemo(){

           RPanel = new JPanel();
           RPanel.setSize(50,50);
            RPanel.setBounds(150, 0, 50, 50);
           TextPanel = new JPanel();
             TextPanel.setSize(50,50);

            JRadioButton r1=new JRadioButton("Airport");  
            JRadioButton r2 = new JRadioButton("Apex");
            JRadioButton r3 = new JRadioButton("IN");
             ButtonGroup bg=new ButtonGroup();    
             FlowLayout layoutFL = new FlowLayout();
              layoutFL.setAlignment(FlowLayout.CENTER);
                RPanel.setLayout(layoutFL);
               layoutFL.setHgap(15);
                 RPanel.add(bg);
                 RPanel.add(comboApex);
               RPanel.add(comboINAV);

               file_1.setText("File1");  
                file_2.setText("File2");
                JTextField text1 = new JTextField(10);
                JTextField text2 = new JTextField(10);
                   text1.setSize(15, 5);
                  text2.setSize(15,5);

                  GroupLayout layout = new GroupLayout(TextPanel);
              layout.setAutoCreateGaps(true);
             layout.setAutoCreateContainerGaps(true);

               JButton btn1 = new JButton("Browse");
                  JButton btn2 = new JButton("Browse");

              layout.setHorizontalGroup(layout.createSequentialGroup()
                //  .addGroup(layout.createSequentialGroup()
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
     .addComponent(file_1)  
     .addComponent(file_2))
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)  
     .addComponent(text1)
     .addComponent(text2))
          .addGroup(layout.createParallelGroup(
          GroupLayout.Alignment.CENTER)
             .addComponent(btn1))
         .addComponent(btn2)
          .addGroup(layout.createSequentialGroup()
             .addComponent(btn3)));

           layout.setVerticalGroup(layout.createSequentialGroup()
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
          .addComponent(file_1)  
          .addComponent(text1)
          .addComponent(btn1))
          .addComponent(btn3)
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)  
             .addComponent(file_2)
                     .addComponent(text2)
                     .addComponent(btn2)));

                TextPanel.setLayout(layout); 
               mainFrame.add(RPanel);
             mainFrame.add(TextPanel);
              mainFrame.add(panel);
              mainFrame.pack();
                 mainFrame.setVisible(true);  
      }    
      }

您可以使用BoxLayout布局和GridBaglayout。

GridBaglayout是推荐作为灵活且功能强大的布局管理器之一。

Little exampele:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridBagLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Framework {
    public Framework(){
        init();
    }
    public void init(){
        JFrame frame = new JFrame("Exo");
        //frame.setSize(300, 300); -@Andrew comment: frame.setSize(300, 300); will be negated by.. frame.pack();. Only do the latter. 
        frame.setLayout(new CardLayout());
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
        JButton bt1 = new JButton("Say");
        JButton bt2 = new JButton("Say");
        JButton bt3 = new JButton("Say");
        centerPanel.add(bt1);
        centerPanel.add(bt2);
        centerPanel.add(bt3);
        centerPanel.add(Box.createVerticalGlue()); 
        JPanel panelConstrain = new JPanel(new GridBagLayout());
        panelConstrain.add(centerPanel);
        frame.add(panelConstrain, BorderLayout.CENTER);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        new Framework();
    }
}

相关内容

  • 没有找到相关文章

最新更新