Java GUI将按钮移动到页面底部

时间:2014-11-29 04:15:30

标签: java swing user-interface button layout-manager

我想知道如何将按钮移动到框架/窗口的底部。

我现在拥有的是:

我希望它看起来像这样:

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;

public class checkbook{
   static JButton Button[] = new JButton[8];
   JLabel begin, accountName, balance;
   JFrame frame;
   JPanel jpanel, jpanel1;
   Container contentPane;
   private JTextField textAccount, textBalance;

   public static void main(String[] args){

      checkbook checkbook = new checkbook();
      checkbook.startFrame();
   }

   checkbook(){
      frame = new JFrame("Checkbook");

   }

   public void startFrame(){

      String bottomButtons[] = {
            "Create a New Account", "Load Trans from a file", "Add New Transactions", "Search Transactions",
            "Sort Transactions", "View/Delete Transactions", "Backup Transactions", "Exit"
         };  

      Container contentPane = frame.getContentPane(); 
      contentPane.setLayout(new FlowLayout());
      Font beginFont = new Font("Arial", Font.PLAIN + Font.BOLD, 22);
      textAccount = new JTextField(" ", 15);
      textBalance = new JTextField("0.0", 15);


      begin = new JLabel("Use The Buttons below To Manage Transactions");
      begin.setFont(beginFont);
      contentPane.add(begin);
     //Container contentPane1 = frame.getContentPane();
     //contentPane1.setLayout(new FlowLayout());

      JPanel jpanel = new JPanel();
      accountName = new JLabel ("Account Name: ");
      jpanel.add(accountName);
      jpanel.add(textAccount);

      balance = new JLabel ("Balance: ");
      jpanel.add(balance);
      jpanel.add(textBalance);

      JPanel jpanel1 = new JPanel();
      jpanel1.setLayout(new GridLayout(2,4)); 

      for(int i = 0; i < 8; i++){
         Button[i] = new JButton(bottomButtons[i]);
         jpanel1.add(Button[i]);
         //Button[i].addActionListener(AL);
      }




      frame.pack();
      frame.setSize(800, 300);
      frame.setResizable(false);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true); 
      frame.add(jpanel);
      frame.add(jpanel1);    


   }
}

所以我想知道如何将按钮向下移动到底部。我是否必须使用BorderLayout并使用BorderLayout.SOUTH等,或者是否有办法使用我已有的GridLayout。如果我必须使用BorderLayout,我将如何按顺序保存按钮(2行,4列)。

1 个答案:

答案 0 :(得分:2)

  

我是否必须使用BorderLayout并使用BorderLayout.SOUTH

是的,但最好的答案是你试试看看会发生什么。你为什么不呢?

  

如果我必须使用BorderLayout,我如何按顺序保存按钮(2行,4列)。

Nest JPanels。使用JPanel将按钮放在GridLayout中,并放置JPanel BorderLayout.SOUTH,或者更好的是BorderLayout.PAGE_END

如,

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.*;

@SuppressWarnings("serial")
public class NestedPanels extends JPanel {
   private static final String TITLE_TEXT = "Use The Buttons Below To Manage Transactions";
   private static final String[] BTN_TEXTS = { "Create a New Account",
         "Load a Trans from a File", "Add New Transactions",
         "Search Transactions", "Sort Transactions",
         "View/Delete Transactions", "Backup Transaction", "Exit" };
   private static final int TITLE_POINTS = 24;

   public NestedPanels() {
      JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
      titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD,
            TITLE_POINTS));
      JPanel titlePanel = new JPanel();
      titlePanel.add(titleLabel); // put it in a JPanel so it will expand to fill BoxLayout

      JPanel accountBalancePanel = new JPanel();
      accountBalancePanel.add(new JLabel("Account Name:"));
      accountBalancePanel.add(new JTextField(10));
      accountBalancePanel.add(Box.createHorizontalStrut(20));
      accountBalancePanel.add(new JLabel("Balance:"));
      accountBalancePanel.add(new JTextField(10));

      JPanel northPanel = new JPanel();
      northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.PAGE_AXIS));
      northPanel.add(titlePanel);
      northPanel.add(accountBalancePanel);

      JPanel southBtnPanel = new JPanel(new GridLayout(2, 4, 1, 1));
      for (String btnText : BTN_TEXTS) {
         southBtnPanel.add(new JButton(btnText));
      }

      setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
      setLayout(new BorderLayout());
      add(northPanel, BorderLayout.NORTH);
      add(Box.createRigidArea(new Dimension(400, 400))); // just an empty placeholder
      add(southBtnPanel, BorderLayout.SOUTH);
   }

   private static void createAndShowGui() {
      NestedPanels mainPanel = new NestedPanels();

      JFrame frame = new JFrame("Nested Panels Example");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

显示为:

enter image description here