java - 一起使用GroupLayout和FlowLayout : OutPut is not appearing in Center

标签 java swing layout-manager flowlayout grouplayout

我正在尝试使用布局在 Java swing 中创建 UI。我想要单选按钮和 2 个文本字段和按钮。对于单选按钮,我使用了流布局,对于文本字段和按钮,我使用了组布局。但我得到的输出是分散的,并且文本字段被拉伸(stretch)。我希望所有这些组件都位于窗口的中心。以下是我的代码。

  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是推荐的灵活而强大的布局管理器之一。

小例子:

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();
    }
}

关于java - 一起使用GroupLayout和FlowLayout : OutPut is not appearing in Center,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44427271/

相关文章:

java - 在@MappedSuperclass 的属性上使用@NamedEntityGraph

java - 如何在不每次加载 URL 的情况下从 JPanel 调整子 WebView/JFXPanel 的大小?

java - 为什么当我按下 JFrame 时,焦点永远不会从我的组件中丢失?

java - 如何在 JPanel 内左对齐 JLabel?

java - 使用 MigLayout 自动换行

java - 在 miglayou 中调整表格大小

java - 按钮仅在触摸后改变颜色

java - 如何获取当前运行的jar的路径和名称?

java - 从两个大整数创建 ASN.1

Java SwingWorker 现在更新模型