微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

布局和框架顶部之间的空间 Java Swing

如何解决布局和框架顶部之间的空间 Java Swing

我第一次尝试使用 Swing 创建 GUI。 我想创建响应式,所以我使用布局。

问题出在面板和框架顶部之间的空间,因为有一个很大的空白空间:

enter image description here

是我第一次使用 Swing,我试图找出发生这种情况的原因,但我找不到解决方案。这是我的简单代码

public class GUI2{

    public static void main(String[] args){
        // Create frame with title Registration Demo
        JFrame frame= new JFrame(); 
        //frame.setLocationRelativeto(null);
        frame.setTitle("HUPA Project ML Algorithm Selection");
         
        // Panel to define the layout. We are using GridBagLayout
        JPanel mainPanel = new JPanel();
        BoxLayout bl = new BoxLayout(mainPanel,BoxLayout.Y_AXIS);
        mainPanel.setLayout(bl);
 
        JPanel headingPanel = new JPanel();
        JLabel headingLabel = new JLabel("Choose the path of the files,the output and the ML Algorithm");
        headingPanel.add(headingLabel);
         
        // Panel to define the layout. We are using GridBagLayout
        JPanel panel = new JPanel(new GridBagLayout());
        // Constraints for the layout
        GridBagConstraints constr = new GridBagConstraints();

        //natural height,maximum width
        constr.fill = GridBagConstraints.HORIZONTAL;
        constr.insets = new Insets(5,3,3);     
        //constr.anchor = GridBagConstraints.WEST;

        // Declare the required Labels
        JLabel userNameLabel = new JLabel("Enter your name :");
        JLabel pwdLabel = new JLabel("Enter your password :");
        JLabel emailLabel = new JLabel("Enter email :");
         
        // Declare Text fields
        JTextField userNameTxt = new JTextField(20);
        jpasswordfield pwdTxt = new jpasswordfield(20);
        JTextField emailTxt = new JTextField(20);
        
        //navegadores de directorio
        JButton navCSV = new JButton("CSV File");
        navCSV.setPreferredSize(new Dimension(18,18)); //ancho alto
        JButton navArff = new JButton("ARFF");
        navArff.setPreferredSize(new Dimension(18,18));
        JButton navOutput = new JButton("Output");
        navOutput.setPreferredSize(new Dimension(18,18));
        
        //Botones de ML
        JButton RT_weights = new JButton("Random Tree Weights");
        RT_weights.setPreferredSize(new Dimension(125,100));
        JButton SA = new JButton("Random Tree No Weights");
        SA.setPreferredSize(new Dimension(125,100));
        JButton NB = new JButton("Naive Bayes");
        NB.setPreferredSize(new Dimension(125,100));
        
        constr.gridx=0; constr.gridy=0;
        panel.add(RT_weights,constr);
        constr.gridx=1; constr.gridy=0;
        panel.add(SA,constr);
        constr.gridx=2; constr.gridy=0;
        panel.add(NB,constr);

        // Set the initial grid values to 0,0
        constr.gridx=0; constr.gridy=1;
        panel.add(userNameLabel,constr);
        constr.gridx=1; constr.gridy=1;
        panel.add(userNameTxt,constr);
        constr.gridx=2; constr.gridy=1;
        panel.add(navCSV,constr);
        
        constr.gridx=0; constr.gridy=2;
        panel.add(pwdLabel,constr);
        constr.gridx=1; constr.gridy=2;
        panel.add(pwdTxt,constr);
        constr.gridx=2; constr.gridy=2;
        panel.add(navArff,constr);
         
        constr.gridx=0; constr.gridy=3;
        panel.add(emailLabel,constr);
        constr.gridx=1; constr.gridy=3;
        panel.add(emailTxt,constr);
        constr.gridx=2; constr.gridy=3;
        panel.add(navOutput,constr);
        
        /*constr.gridwidth = 2;
        constr.anchor = GridBagConstraints.CENTER;*/
  
        // Button with text "Register"
        JButton button = new JButton("Register");
        // add a listener to button
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                headingLabel.setText("Thanks for registering. We'll get back to you shortly.");
                userNameTxt.setText("");
                pwdTxt.setText("");
                emailTxt.setText("");
            }
        });

        // Add label and button to panel
        constr.gridx=0; constr.gridy=4; 
        constr.gridwidth = 3; //gridwith cuantas casillas ocupa del layout,aquí 3
        panel.add(button,constr);
  
        mainPanel.add(headingPanel);
        mainPanel.add(panel);
 
        // Add panel to frame
        frame.add(mainPanel);
        frame.pack();
        frame.setSize(600,600);
        frame.setLocationRelativeto(null);
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
       //panel.setBorder(BorderFactory.createraisedBevelBorder());     
       // panel.setBorder(BorderFactory.createEmptyBorder(10,10,10));
    }
}

如果您需要了解更多信息,请告诉我。提前致谢!

解决方法

你有几个问题。默认:

  1. GridBagLayout 将组件在可用空间中居中
  2. 当有额外空间可用时,BoxLayout 将按比例为每个组件分配空间(最多可达组件的最大大小)。

在您使用的代码中:

    frame.pack();
    frame.setSize(600,600);

setSize(...) 将覆盖 pack() 计算,导致框架过大。所以额外的空间被分配给“headingPanel”和“panel”。

删除 setSize() 语句以查看差异。

但是,如果您将框架拖得更大,您现在会看到空间重新出现。

解决方法是使用:

  1. JFrame 的默认 BorderLayout 和
  2. GridBagLayout 的包装面板,以防止组件垂直居中

去掉你的“mainPanel”,你的代码会是这样的:

//mainPanel.add(headingPanel);
//mainPanel.add(panel);
//frame.add(mainPanel);

JPanel wrapper = new JPanel();
wrapper.add( panel );

frame.add(headingPanel,BorderLayout.PAGE_START);
frame.add(wrapper,BorderLayout.CENTER);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。