数字键盘未正确显示

时间:2016-04-06 00:14:37

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

我有一个Java GUI项目,我正在创建一个ATM。我有一切设置,但由于某种原因我左侧的数字键盘显示不正确。它应该显示为一个4x3的数字网格,但它只是显示一个9.我已经检查以确保它在GridLayout中并且我已经检查了我的循环,但我可能已经查看了一些东西。感谢任何帮助,谢谢!

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;

public class ATMProject extends JPanel implements ActionListener {
    private JPanel mainPanel = null;
    private JPanel btnPanel = null;
    private JPanel userBtns = null;
    private JTextArea textArea = null;
    private JPanel keyPanel = null;
    private JTextField numField = null;
    private JPanel numpadPanel = null;
    private JButton[] userButtons = null;
    private JButton[] keypadButtons = null;
    private String[] btnPanelbtns = { "A", "B", "C" };
    private String[] numpadPanelbtns = { "7", "4", "1", "8", "5", "2", "9", "6", "3", "0", ".", "CE" };

    public ATMProject() {
        super();
        mainPanel = new JPanel();
        this.setLayout(new BorderLayout());
        this.add(mainPanel);

        btnPanel = new JPanel();
        btnPanel.setLayout(new GridLayout(3, 1));
        this.add(btnPanel, BorderLayout.EAST);

        textArea = new JTextArea();
        this.add(textArea, BorderLayout.CENTER);

        keyPanel = new JPanel();
        keyPanel.setLayout(new BorderLayout());
        this.add(keyPanel, BorderLayout.WEST);

        numpadPanel = new JPanel();
        numpadPanel.setLayout(new GridLayout(0, 3));
        keyPanel.add(numpadPanel, BorderLayout.CENTER);

        numField = new JTextField();
        keyPanel.add(numField, BorderLayout.NORTH);

        userButtons = new JButton[btnPanelbtns.length];
        for (int i = 0; i < userButtons.length; i++) {
            userButtons[i] = new JButton(btnPanelbtns[i]);
            userButtons[i].addActionListener(this);
            btnPanel.add(userButtons[i]);
        }

        keypadButtons = new JButton[numpadPanelbtns.length];
        for (int i = 0; i < userButtons.length; i++) {
            keypadButtons[i] = new JButton(numpadPanelbtns[i]);
            keypadButtons[i].addActionListener(this);
            numpadPanel.add(keypadButtons[i]);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        MyFrame mf = new MyFrame();
    }

}


import javax.swing.JFrame;

public class MyFrame extends JFrame {
    private ATMProject atm = null;
    public MyFrame(){
        super();
        atm = new ATMProject();
        this.add(atm);
        this.setTitle("ATM");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setSize(800,300);
        this.setVisible(true);
    }
}

这是它应该出现的方式:

enter image description here

2 个答案:

答案 0 :(得分:2)

您已将按钮添加到keyPanel ...

keyPanel.add(keypadButtons[i]);

使用BorderLayout ...

keyPanel = new JPanel();
keyPanel.setLayout(new BorderLayout());

所以只有添加到它的最后一个组件才会被小组布置。

有人想象你应该将它们添加到numpadPanel而不是......

numpadPanel.add(keypadButtons[i]);
  

是的,我有那个numpadPanel.add(keypadButtons [i]);我垂直得到7 8 9。

GridLayout如何运作,你可以通过使用更像

的东西强制进入水平优先模式
numpadPanel.setLayout(new GridLayout(0, 3));

KeyPad

答案 1 :(得分:2)

我的循环写得不正确。 for (int i = 0; i < userButtons.length; i++)我的程序只输出3个按钮,因为userButtons.length只有3个元素。我将userButtons.length更改为numpadPanelbtns.length并修复了它,因为numpadPanelbtns数组中有12个元素。