Java 使用borderlayout将构件添加到框架

Java 使用borderlayout将构件添加到框架,java,swing,layout,jframe,Java,Swing,Layout,Jframe,如何使用布局获得相同的外观 package item; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author isslam */ public class MyFrameMain extends JFrame{ Equipment newq = new Equipment(); private final JLabel iLabel; private

如何使用布局获得相同的外观

    package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *
 * @author isslam
 */
public class MyFrameMain extends JFrame{
    Equipment newq = new Equipment();
    private final JLabel iLabel;
    private final JLabel nLabel;
    private final JTextField iJTextField;
    private final JTextField nJTextField;
    private final JTextField swTextField;
    private final JTextField hwTextField;
    private final JLabel jItemCounter;
    private final JTextArea reSoulte;
    private final JButton addButton;
    private final JButton showButton;
    private final JButton copyButton;
    private final JButton exitButton;

    public MyFrameMain(String title){
    super(title);
    setTitle(title);
    setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);

    iJTextField = new JTextField();
    nJTextField = new JTextField();
    swTextField = new JTextField();
    hwTextField = new JTextField();
    nLabel = new JLabel("ID: ");
    iLabel = new JLabel("Name: ");
    jItemCounter = new JLabel("Number of current Item");

    reSoulte = new JTextArea(15,20);
    reSoulte.setEditable(false);

    addButton = new  JButton("Add an item into the Array");
    showButton = new JButton("Show all items in the Array");
    copyButton = new JButton("Copy Array into File");
    exitButton = new JButton("Exite");


    JRadioButton rButton1 = new JRadioButton("SW Version",false);
    JRadioButton rButton2 = new JRadioButton("HW Type",false);
    JRadioButton rButton3 = new JRadioButton("General",true);    

     ButtonGroup BGroup = new ButtonGroup();
     BGroup.add(rButton1);
     BGroup.add(rButton2);
     BGroup.add(rButton3);

     JPanel panel = new JPanel(new GridLayout(5,1));
     panel.add(nLabel);
     panel.add(iLabel);
     panel.add(rButton1);
     panel.add(rButton2);
     panel.add(rButton3);

     JPanel bpanel = new JPanel(new GridLayout(2,2));
     bpanel.add(addButton);
     bpanel.add(showButton);
     bpanel.add(copyButton);
     bpanel.add(exitButton);

     JPanel jtfPanel = new JPanel(new GridLayout(5,1));
     jtfPanel.add(iJTextField);
     jtfPanel.add(nJTextField);
     jtfPanel.add(swTextField);
     jtfPanel.add(hwTextField);
     jtfPanel.add(jItemCounter);

     JPanel jtaPanel = new JPanel(new GridLayout(5,1));
     jtaPanel.add(reSoulte);

     Container pane = getContentPane();
     pane.setLayout(null);
     pane.add(panel);
     pane.add(bpanel);
     pane.add(jtfPanel);
     pane.add(jtaPanel);



     Insets insets = pane.getInsets();
     setSize(500 + insets.left + insets.right,500 + insets.top + insets.bottom);

     Dimension size = panel.getPreferredSize();
     panel.setBounds(0 + insets.left, 18 + insets.top,size.width, size.height);

     size = bpanel.getPreferredSize();
     bpanel.setBounds(0 + insets.left, 409 + insets.top,115+size.width, size.height);

     size = jtfPanel.getPreferredSize();
     jtfPanel.setBounds(180 + insets.left, 25 + insets.top,170 +size.width, size.height);

     size = jtaPanel.getPreferredSize();
     jtaPanel.setBounds(0 + insets.left, 150 + insets.top,265 +size.width, size.height);


     exitButton.addActionListener(new ButtonWatcher());

    }

     private class ButtonWatcher implements ActionListener{

         public void actionPerformed(ActionEvent a){
             System.exit(0);
        }
     }     
}
你能告诉我如何将绝对位置替换为边界布局吗

我对代码的看法是:

  • “面板”显示在左上角
  • “jtfPanel”显示在右上角
  • “jtaPanel”显示在中间
  • “bPanel”显示在底部
  • BorderLayout有3个垂直区域用于处理北、中和南,因此我建议您将“面板”和“jtfPanel”组合到另一个面板中,然后您将有3个面板添加到BorderLayout。比如:

    JPanel north = new JPanel( new BorderLayout() );
    north.add(panel, BorderLayout.WEST);
    north.add(jtfPanel, BorderLayout.CENTER);
    
    Container contentPane = getContentPane();
    contentPane.add(north, BorderLayout.NORTH);
    contentPane.add(jtaPanel, BorderLayout.CENTER);
    contentPane.add(bPanel, BorderLayout.SOUTH);
    

    主要的一点是,您可以嵌套多个面板以达到所需的效果。

    这里查看此项。我添加了一个主方法来运行它。我使用了
    EmptyBorder
    s作为所需的间距。我还将面板组合在一起,这使得布局更易于使用

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.EmptyBorder;
    
    /**
     *
     * @author isslam
     */
    public class MyFrameMain extends JFrame {
    
        // Equipment newq = new Equipment();
        private final JLabel iLabel;
        private final JLabel nLabel;
        private final JTextField iJTextField;
        private final JTextField nJTextField;
        private final JTextField swTextField;
        private final JTextField hwTextField;
        private final JLabel jItemCounter;
        private final JTextArea reSoulte;
        private final JButton addButton;
        private final JButton showButton;
        private final JButton copyButton;
        private final JButton exitButton;
    
        public MyFrameMain(String title) {
            //super(title);
            setTitle(title);
            setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
    
            iJTextField = new JTextField();
            nJTextField = new JTextField();
            swTextField = new JTextField();
            hwTextField = new JTextField();
            nLabel = new JLabel("ID: ");
            iLabel = new JLabel("Name: ");
            jItemCounter = new JLabel("Number of current Item");
    
            reSoulte = new JTextArea(15, 45);
            reSoulte.setEditable(false);
    
            addButton = new JButton("Add an item into the Array");
            showButton = new JButton("Show all items in the Array");
            copyButton = new JButton("Copy Array into File");
            exitButton = new JButton("Exite");
    
            JRadioButton rButton1 = new JRadioButton("SW Version", false);
            JRadioButton rButton2 = new JRadioButton("HW Type", false);
            JRadioButton rButton3 = new JRadioButton("General", true);
    
            ButtonGroup BGroup = new ButtonGroup();
            BGroup.add(rButton1);
            BGroup.add(rButton2);
            BGroup.add(rButton3);
    
            JPanel panel = new JPanel(new GridLayout(5, 1));
            panel.setBorder(new EmptyBorder(0, 0, 0, 75));
            panel.add(nLabel);
            panel.add(iLabel);
            panel.add(rButton1);
            panel.add(rButton2);
            panel.add(rButton3);
    
            JPanel bpanel = new JPanel(new GridLayout(2, 2));
            bpanel.setBorder(new EmptyBorder(15, 0, 0, 0));
            bpanel.add(addButton);
            bpanel.add(showButton);
            bpanel.add(copyButton);
            bpanel.add(exitButton);
    
            JPanel jtfPanel = new JPanel(new GridLayout(5, 1));
    
            jtfPanel.add(iJTextField);
            jtfPanel.add(nJTextField);
            jtfPanel.add(swTextField);
            jtfPanel.add(hwTextField);
            jtfPanel.add(jItemCounter);
    
            //JPanel jtaPanel = new JPanel(new GridLayout(5, 1));
            //jtaPanel.add(reSoulte);
    
            JPanel topPanel = new JPanel(new BorderLayout());
            topPanel.setBorder(new EmptyBorder(0, 0, 20, 0));
            topPanel.add(panel, BorderLayout.WEST);
            topPanel.add(jtfPanel, BorderLayout.CENTER);
    
            JPanel mainPanel = new JPanel(new BorderLayout());
            mainPanel.setBorder(new EmptyBorder(20, 10, 10, 10));
            mainPanel.add(bpanel, BorderLayout.SOUTH);
            mainPanel.add(reSoulte, BorderLayout.CENTER);
            mainPanel.add(topPanel, BorderLayout.NORTH);
    
            Container pane = getContentPane();
            //pane.setLayout(null);
            //pane.add(panel);
            pane.add(mainPanel);
            //pane.add(jtfPanel);
    
    
            //Insets insets = pane.getInsets();
            //setSize(500 + insets.left + insets.right, 500 + insets.top + insets.bottom);
    
            //Dimension size = panel.getPreferredSize();
            //panel.setBounds(0 + insets.left, 18 + insets.top, size.width, size.height);
    
            //size = bpanel.getPreferredSize();
           // bpanel.setBounds(0 + insets.left, 409 + insets.top, 115 + size.width, size.height);
    
           // size = jtfPanel.getPreferredSize();
           // jtfPanel.setBounds(180 + insets.left, 25 + insets.top, 170 + size.width, size.height);
    
            //size = jtaPanel.getPreferredSize();
            //jtaPanel.setBounds(0 + insets.left, 150 + insets.top, 265 + size.width, size.height);
    
            exitButton.addActionListener(new ButtonWatcher());
    
        }
    
        public static void main(String[] args) {
             SwingUtilities.invokeLater(new Runnable(){
                 public void run(){
                     MyFrameMain frame = new MyFrameMain("Title");
                     frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                     frame.pack();
                     frame.setResizable(false);
                     //frame.setSize(500, 500);
                     frame.setLocationByPlatform(true);
                     frame.setVisible(true);
    
                 }
             });
         }
    
        private class ButtonWatcher implements ActionListener {
    
            public void actionPerformed(ActionEvent a) {
                System.exit(0);
            }
        }
    }
    

    我可以使用绝对位置,但如果您希望人们查看您的代码,是否有任何方法不使用绝对位置,而是遵循Java命名约定。变量名不应以大写字符开头<代码>什么布局更适合将JTextArea居中-您甚至不需要将文本区域添加到框架中。如果您需要更多帮助,请修复代码以使用正确的变量名,并发布一个正确的代码来演示您的问题。您能告诉我应该如何修复吗it@isslam阿克基拉:是的。跟着CAMICKR的指令完全正确。“Gilbert le Blanc好,我谢谢你,但是为什么你删除超级(title)它什么都不做我知道,但我认为它使应用跑得更快是对还是错不需要调用超级(标题)。只要在实例化类时设置标题即可。