GridBagConstraints使按钮成为中心

时间:2015-03-15 00:10:29

标签: java swing jframe

因此,无论我将gridx和gridy更改为什么,它都不会执行任何操作。我使用了setBorderLayout和weightx和weighty,但没有。这是现在的代码:

package goatSoftware;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class CreateJFrame extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;

public CreateJFrame(){
    super("Farm Calculator");

    JPanel a = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    final String user = System.getProperty("user.name");

    JButton button = new JButton();
    button.setText("Add Doe");
    button.setVisible(true);

    JButton button2 = new JButton();
    button2.setText("Add Buck");
    button2.setVisible(true);

    gbc.gridx = 5;
    gbc.gridy = 5;
    a.add(button, gbc);
    gbc.gridx = 4;
    gbc.gridy = 5;
    a.add(button2, gbc);
    a.setVisible(true);

    setSize(600,400);
    setLocation(400,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    add(a);

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //Frame
            final JFrame newJFrame = new JFrame();
            //Panel2
            JPanel delta = new JPanel();
            //Panel3
            JPanel eta = new JPanel();
            //Panel
            JPanel alpha = new JPanel();
            //Area
            final JTextField beta = new JTextField();
            beta.setColumns(13);
            //Button
            JButton omega = new JButton();
            omega.setText("Submit");

            omega.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try{

                        final File file = new File("/Users/"+user+"/config.plist");

                        // if file doesn't exists, then create it
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                        FileWriter fileOut = new FileWriter(file.getAbsoluteFile(), true);
                        String getInfo = beta.getText();
                        fileOut.write("\n<"+getInfo+">"+getInfo+"</"+getInfo+">");
                        fileOut.close();
                        newJFrame.dispatchEvent(new WindowEvent(newJFrame, WindowEvent.WINDOW_CLOSING));
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }

                }
            });

            beta.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try{
                        final File file = new File("/Users/"+user+"/config.plist");

                        // if file doesn't exists, then create it
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                        FileWriter fileOut = new FileWriter(file.getAbsoluteFile(), true);
                        String getInfo = beta.getText();
                        fileOut.write("\n<"+getInfo+">"+getInfo+"</"+getInfo+">");
                        fileOut.close();
                        newJFrame.dispatchEvent(new WindowEvent(newJFrame, WindowEvent.WINDOW_CLOSING));
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }

                }
            });
            //Field
            JTextArea gamma = new JTextArea();
            gamma.setText("Doe's name:");
            gamma.setEditable(false);

            alpha.add(beta);
            delta.add(gamma);
            delta.setVisible(true);
            alpha.setVisible(true);
            eta.setVisible(true);
            eta.add(omega);

            newJFrame.add(alpha, BorderLayout.CENTER);
            newJFrame.add(delta, BorderLayout.WEST);
            newJFrame.setSize(275, 125);
            newJFrame.setVisible(true);
            newJFrame.add(eta, BorderLayout.SOUTH);


        }
    });

    button2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //Frame
            final JFrame newJFrame = new JFrame();
            //Panel2
            JPanel delta = new JPanel();
            //Panel3
            JPanel eta = new JPanel();
            //Panel
            JPanel alpha = new JPanel();
            //Area
            final JTextField beta = new JTextField();
            beta.setColumns(13);
            //Button
            JButton omega = new JButton();
            omega.setText("Submit");

            omega.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try{

                        final File file = new File("/Users/"+user+"/config.plist");

                        // if file doesn't exists, then create it
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                        FileWriter fileOut = new FileWriter(file.getAbsoluteFile(), true);
                        String getInfo = beta.getText();
                        fileOut.write("\n<buck><"+getInfo+">"+getInfo+"</"+getInfo+"></buck>");
                        fileOut.close();
                        newJFrame.dispatchEvent(new WindowEvent(newJFrame, WindowEvent.WINDOW_CLOSING));
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }

                }
            });

            beta.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try{
                        final File file = new File("/Users/"+user+"/config.plist");

                        // if file doesn't exists, then create it
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                        FileWriter fileOut = new FileWriter(file.getAbsoluteFile(), true);
                        String getInfo = beta.getText();
                        fileOut.write("\n<buck><"+getInfo+">"+getInfo+"</"+getInfo+"></buck>");
                        fileOut.close();
                        newJFrame.dispatchEvent(new WindowEvent(newJFrame, WindowEvent.WINDOW_CLOSING));
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }

                }
            });
            //Field
            JTextArea gamma = new JTextArea();
            gamma.setText("Buck's name:");
            gamma.setEditable(false);

            alpha.add(beta);
            delta.add(gamma);
            delta.setVisible(true);
            alpha.setVisible(true);
            eta.setVisible(true);
            eta.add(omega);

            newJFrame.add(alpha, BorderLayout.CENTER);
            newJFrame.add(delta, BorderLayout.WEST);
            newJFrame.setSize(275, 125);
            newJFrame.setVisible(true);
            newJFrame.add(eta, BorderLayout.SOUTH);


        }
    });

}


//DON'T TOUCH THIS!!!! EVER!!!!
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}
}

按钮不会进入5,5和5,4,它们应该是。

谢谢!

!托马斯

1 个答案:

答案 0 :(得分:0)

  

我认为5,5和5,4是JFrame上的坐标,我错了吗?

它们是面板上的网格位置(不是框架),ASSUMING面板上有25个组件。你只有两个组件,所以说组件在第5行和第5列是没有意义的。

如果您只想尝试在框架顶部显示两个组件,请尝试:

JPanel panel = new JPanel( new FlowLayout(FlowLayout.CENTER) );
panel.add(button1);
panel.add(button2);
frame.add(panel, BorderLayout.NORTH);