如何在JFrame中手动设置面板内按钮的位置

时间:2016-01-30 05:40:31

标签: java swing jframe jpanel jbutton

目前,我有这个:

Frame

这是我的代码:

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent.*;
import java.awt.event.ActionListener.*;
import javax.swing.*;
import javax.swing.JFrame.*;

public class Test extends JFrame //implements ActionListener
{
    //setting private JFrame variables 
    private JMenuBar menu;
    private JMenuItem m1,m2;
    private JFrame f;
    private JPanel panel;
    private JButton btnUnit,btnInfo,btnSearch,btnExit;

    public Test()
    {
        try
        {
            //creating new Jframe variable f
            f = new JFrame();

            //creating new JMenubar
            menu = new JMenuBar();

            //creating new JMenuItem
            m1 = new JMenuItem("File");
            m2 = new JMenuItem("Exit");

            menu.add(m1);
            menu.add(m2);

            panel = new JPanel(new GridLayout(9,1));
            panel.add(menu);
            this.add(panel);

            btnUnit = new JButton("Unit");
            //btnUnit.setLayout(new GridLayout(1,3,100,0));
            btnInfo = new JButton("Information");
            btnSearch = new JButton("Search");
            btnExit = new JButton("Exit");

            panel.add(btnUnit);
            panel.add(btnInfo);
            panel.add(btnSearch);
            panel.add(btnExit); 

            this.setTitle("MyFrame");
            this.setDefaultLookAndFeelDecorated(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
            this.setSize(300,200);
            this.setLocationRelativeTo(null);
            this.setResizable(false);       
            this.setVisible(true);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] a)
    {
        Test test = new Test();  
    }
}

我是一个穷人,对java知之甚少。我想学习GUI,所以我在我的程序中做了试错。我现在的目标是在顶部有一个JFrame菜单,下面有4个按钮,但我不知道如何设置按钮的大小并在该面板上手动设置它们的位置。

我真的想学习,请帮助我。任何评论,建议和评论都会被接受并受到好评。

这是我的目标输出: Frame Output

1 个答案:

答案 0 :(得分:2)

您可以通过多种方式实现这一目标,例如......

http://sqlmag.com/sql-server-2000/referential-integrity-sql-server

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                JMenuBar menu = new JMenuBar();

                //creating new JMenuItem
                JMenuItem m1 = new JMenuItem("File");
                JMenuItem m2 = new JMenuItem("Exit");

                menu.add(m1);
                menu.add(m2);

                JFrame frame = new JFrame("Testing");
                frame.setJMenuBar(menu);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridLayout(2, 2));
            add(new FillerPane(new JButton("Unit")));
            add(new FillerPane(new JButton("Information")));
            add(new FillerPane(new JButton("Search")));
            add(new FillerPane(new JButton("Exit")));
        }

        public class FillerPane extends JPanel {

            public FillerPane(JButton button) {
                setBorder(new EmptyBorder(10, 10, 10, 10));
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.ipady = 20;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.weightx = 1;
                add(button, gbc);
            }

        }

    }

}

...或

Layout

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                JMenuBar menu = new JMenuBar();

                //creating new JMenuItem
                JMenuItem m1 = new JMenuItem("File");
                JMenuItem m2 = new JMenuItem("Exit");

                menu.add(m1);
                menu.add(m2);

                JFrame frame = new JFrame("Testing");
                frame.setJMenuBar(menu);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBorder(new EmptyBorder(10, 10, 10, 10));
            setLayout(new GridLayout(2, 2, 10, 10));
            add(makeButton("Unit"));
            add(makeButton("Information"));
            add(makeButton("Search"));
            add(makeButton("Exit"));
        }

        protected JButton makeButton(String text) {
            JButton btn = new JButton(text);
            btn.setMargin(new Insets(10, 10, 10, 10));
            return btn;
        }

    }

}

请查看Layout了解详情

  

我想知道菜单项,我们m1.add(m2);并删除menu.add(m2);

使用JMenuItem,例如......

JMenuBar mb = new JMenuBar();
JMenu m = new JMenu("File");
JMenuItem exit = new JMenuItem("Exit");
m.add(exit);
mb.add(m);

有关详细信息,请查看Laying Out Components Within a Container