银行帐户jframe的布局代码

时间:2015-11-30 19:05:39

标签: java swing

enter image description here

上图是我的银行帐户GUI的屏幕截图。基本上我想要初始余额,存款标签和字段,如果有意义的话,将标签和字段撤回到不同的行。

我尝试过使用不同的布局,但是我无法让它们工作,而且我对它们并不满意。

哪种布局最适合我想要的用途?

package assignment;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

    public class accountFrame extends JFrame 
    {
       private static final int FRAME_WIDTH = 300;
       private static final int FRAME_HEIGHT = 250;
       private static final double INITIAL_BALANCE = 0.00;
       double result;
       private JLabel initialLabel;
       private JLabel depositLabel;
       private JLabel withdrawLabel;
       private JTextField depositField;  
       private JTextField withdrawField;
       private JButton depositButton;
       private JButton withdrawButton;
       private final JLabel resultLabel;
       private JPanel controlPanel;
       private final cAccount account; 


       public accountFrame()
       { 

          account = new cAccount();      
          resultLabel = new JLabel("New Balance: £" + account.getBalance());

          createTextField();
          createButton();
          createControlPanel();
          setSize(FRAME_WIDTH, FRAME_HEIGHT);
              setLocationRelativeTo(null);

       }

       private void createTextField()
       {
          final int FIELD_WIDTH = 5;

          initialLabel = new JLabel("Initial Balance £" + INITIAL_BALANCE);       
          depositLabel = new JLabel("Deposit: ");
          depositField = new JTextField(FIELD_WIDTH);
              withdrawLabel = new JLabel("Withdraw: ");
              withdrawField = new JTextField(FIELD_WIDTH);
       }

       private void createButton()
       {

               //Create deposit button and assign an action listener
          depositButton = new JButton("Deposit");

          class DepositListener implements ActionListener
          {
                 @Override
             public void actionPerformed(ActionEvent event)
             {     
                double depositAmount = Double.parseDouble(depositField.getText());


                account.deposit(depositAmount);
                result = account.getBalance();
                resultLabel.setText("New Balance: " + result);
                    depositField.setText("0.00");
             }           
          }

          ActionListener d = new DepositListener();

          depositButton.addActionListener(d);     

              //Implement action listener for withdraw button
          withdrawButton = new JButton("Withdraw");

          class WithdrawListener implements ActionListener
          {
                 @Override
             public void actionPerformed(ActionEvent event)
             {    
                double withdrawl = Double.parseDouble(withdrawField.getText());

                    account.withdraw(withdrawl);

                result = account.getBalance();
                resultLabel.setText("New Balance: " + result);
                    withdrawField.setText("0.00");
             }           
          }

          ActionListener w = new WithdrawListener();
          withdrawButton.addActionListener(w);
       }

       private void createControlPanel()        
       {

          controlPanel = new JPanel();
          controlPanel.add(initialLabel);
          controlPanel.add(depositLabel);
          controlPanel.add(depositField);
          controlPanel.add(depositButton);
          controlPanel.add(withdrawLabel);
          controlPanel.add(withdrawField);
          controlPanel.add(withdrawButton);
          controlPanel.add(resultLabel);
          add(controlPanel);
       }

     }

2 个答案:

答案 0 :(得分:5)

我可以说设计这个的一种方法是创建JFrame并在JFrame中添加JPanel。对于第一个JPanel,您将它声明为GridLayout 4行高1列。

接下来,您可以为每个单独的行定义新的JPanel,只使用常规的旧流布局,并允许布局管理来处理这些。现在,如果你想要在间距等方面获得更多限制,你可以通过各种方式使用不同的布局来控制更多的硬盘级别。

我喜欢这种方法的原因是因为您可以根据需要调整每一行。这是一个非常基本的示例,但可以根据需要进行调整。

public class Bank extends JFrame {
    JLabel initialBalance, balanceVal, deposit, withdrawl,newBalanceLbl, newBalanceField;
    JTextField txtDeposit, txtWithdrawl;
    JButton btnDeposit, btnWithdrawl;
    JPanel mainPanel, initialPanel, depositPanel, withdrawlPanel, newBalance;

    public Bank(){
        super("Bank Application");
        mainPanel = new JPanel(new GridLayout(4,1));
        mainPanel.add(configureInitial());
        mainPanel.add(configureDeposit());
        mainPanel.add(configureWithdrawl());
        mainPanel.add(configureNew());
        add(mainPanel);
        this.pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private JPanel configureInitial(){
        initialPanel = new JPanel();
        initialBalance = new JLabel("Inital Balance: ");
        balanceVal = new JLabel("0.00");
        initialPanel.add(initialBalance);
        initialPanel.add(balanceVal);
        return initialPanel;
    }

    private JPanel configureDeposit(){
        depositPanel = new JPanel();
        deposit = new JLabel("Deposit");
        txtDeposit = new JTextField(10);
        btnDeposit = new JButton("Deposit");
        depositPanel.add(deposit);
        depositPanel.add(txtDeposit);
        depositPanel.add(btnDeposit);
        return depositPanel;
    }

    private JPanel configureWithdrawl(){
        withdrawlPanel = new JPanel();
        withdrawl = new JLabel("Withdrawl");
        txtWithdrawl = new JTextField(10);
        btnWithdrawl = new JButton("Withdrawl");
        withdrawlPanel.add(withdrawl);
        withdrawlPanel.add(txtWithdrawl);
        withdrawlPanel.add(btnWithdrawl);
        return withdrawlPanel;
    }

    private JPanel configureNew(){
        newBalance = new JPanel();
        newBalanceLbl = new JLabel("New Balance: ");
        newBalanceField = new JLabel("0.00");
        newBalance.add(newBalanceLbl);
        newBalance.add(newBalanceField);
        return newBalance;
    }

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

}

一般来说,它看起来像这样:

enter image description here

答案 1 :(得分:1)

另一种方法是使用BoxLayoutFlowLayout的组合。我建议您尝试使用不同的Layout Managers并阅读How to do a Runnable Example,这样我们就不需要从头开始向您展示这样的内容(例如,您可以删除不必要的代码你发布在这里)。

import java.awt.*;
import javax.swing.*;
public class LayoutsExample {
    JFrame frame;

    JPanel contentPanel;
    JPanel depositPanel;
    JPanel withdrawPanel;

    JLabel depositLabel;
    JLabel withdrawLabel;
    JLabel finalbalanceLabel;
    JLabel initialBalanceLabel;

    JTextField depositField;
    JTextField withdrawField;

    JButton depositButton;
    JButton withdrawButton;

    public static void main (String args[]) {       
        new LayoutsExample();
    }
    LayoutsExample () {
        frame = new JFrame("Layouts Example");
        depositPanel = new JPanel(new FlowLayout());
        withdrawPanel = new JPanel(new FlowLayout());
        depositLabel = new JLabel("Deposit: ");
        withdrawLabel = new JLabel("Withdraw: ");
        finalbalanceLabel = new JLabel("New Balance: ");
        initialBalanceLabel = new JLabel("Initial Balance: ");
        depositField = new JTextField("", 10);
        withdrawField = new JTextField("", 10);
        depositButton = new JButton("Deposit");
        withdrawButton = new JButton("Withdraw");

        depositPanel.add(depositLabel);
        depositPanel.add(depositField);
        depositPanel.add(depositButton);

        withdrawPanel.add(withdrawLabel);
        withdrawPanel.add(withdrawField);
        withdrawPanel.add(withdrawButton);

        JPanel pane = new JPanel(new FlowLayout());
        pane.add(initialBalanceLabel); //Added label to panel to center it

        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
        frame.add(pane);
        frame.add(depositPanel);
        frame.add(withdrawPanel);

        pane = new JPanel(new FlowLayout()); 
        pane.add(finalbalanceLabel); //Added label to pane to center it

        frame.add(pane);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }
}