如何获得NumPad布局

时间:2013-03-26 17:19:49

标签: java swing applet japplet

我需要键盘上的numpad布局。我得到了大部分,我唯一的问题是“输入”按钮应该是直到底部。但是当我使用Panel p4并将其设置为SOUTH时,“enter”按钮不会到达底部。这是代码。

这就是它的样子

enter image description here

这就是我想要的

enter image description here

public class NumPad extends Applet
{   
  public void init() {
  setLayout(new BorderLayout());

  Panel p1 = new Panel();
  p1.setLayout(new GridLayout(1, 0));
  p1.add(new Button("Num"));
  p1.add(new Button("/"));
  p1.add(new Button("*"));
  p1.add(new Button("-"));
  add(p1, BorderLayout.NORTH);

  Panel p2 = new Panel();
  p2.setLayout(new GridLayout(3, 0));
  p2.add(new Button("7"));
  p2.add(new Button("8"));
  p2.add(new Button("9"));
  p2.add(new Button("4"));
  p2.add(new Button("5"));
  p2.add(new Button("6"));
  p2.add(new Button("1"));
  p2.add(new Button("2"));
  p2.add(new Button("3"));
  add(p2, BorderLayout.CENTER);      

  Panel p4 = new Panel();
  p4.setLayout(new GridLayout(1, 0, 30, 40));
  p4.add(new Button("0"));
  p4.add(new Button("."));
  add(p4, BorderLayout.SOUTH);      

  Panel p3 = new Panel();
  p3.setLayout(new GridLayout(2, 0));
  p3.add(new Button("+"));
  p3.add(new Button("Enter"));
  add(p3, BorderLayout.EAST);      
 }
}

2 个答案:

答案 0 :(得分:1)

无需重新安排所有内容,您可以将p2p4嵌套到另一个JPanel中,并在中心添加新面板。只要你不在“主”面板的SOUTH中添加任何内容,就可以了。

顺便说一句,不要使用awt组件,直接转到Swing组件。这样可以节省大量时间并避免麻烦行为(考虑延长JApplet而不是Applet

见这个小例子:

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NumPad extends JPanel {
    public NumPad() {
        setLayout(new BorderLayout());

        JPanel top = new JPanel(new GridLayout(1, 0));
        top.add(new JButton("Num"));
        top.add(new JButton("/"));
        top.add(new JButton("*"));
        top.add(new JButton("-"));
        add(top, BorderLayout.NORTH);
        JPanel p2p4 = new JPanel(new BorderLayout());
        JPanel p2 = new JPanel(new GridLayout(3, 0));
        p2.add(new JButton("7"));
        p2.add(new JButton("8"));
        p2.add(new JButton("9"));
        p2.add(new JButton("4"));
        p2.add(new JButton("5"));
        p2.add(new JButton("6"));
        p2.add(new JButton("1"));
        p2.add(new JButton("2"));
        p2.add(new JButton("3"));
        p2p4.add(p2, BorderLayout.CENTER);

        JPanel p4 = new JPanel();
        p4.setLayout(new GridLayout(1, 0, 30, 40));
        p4.add(new JButton("0"));
        p4.add(new JButton("."));
        p2p4.add(p4, BorderLayout.SOUTH);
        add(p2p4);
        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(2, 0));
        p3.add(new JButton("+"));
        p3.add(new JButton("Enter"));
        add(p3, BorderLayout.EAST);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame(NumPad.class.getSimpleName());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                NumPad numPad = new NumPad();
                frame.add(numPad);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:0)

这就是BorderLayout的工作原理。 BorderLayout.EAST并未延伸到底部。

enter image description here

尝试different layout manager或以不同方式重新排列组件。见这个例子:

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

setLayout(new BorderLayout());

Panel p1 = new Panel();
p1.setLayout(new GridLayout(1, 0));
p1.add(new Button("Num"));
p1.add(new Button("/"));
p1.add(new Button("*"));
panel.add(p1, BorderLayout.NORTH);

Panel p2 = new Panel();
p2.setLayout(new GridLayout(3, 0));
p2.add(new Button("7"));
p2.add(new Button("8"));
p2.add(new Button("9"));
p2.add(new Button("4"));
p2.add(new Button("5"));
p2.add(new Button("6"));
p2.add(new Button("1"));
p2.add(new Button("2"));
p2.add(new Button("3"));
panel.add(p2, BorderLayout.CENTER);

Panel p4 = new Panel();
p4.setLayout(new GridLayout(1, 0, 30, 40));
p4.add(new Button("0"));
p4.add(new Button("."));
panel.add(p4, BorderLayout.SOUTH);

Panel p3 = new Panel();
p3.setLayout(new GridLayout(3, 0));
p3.add(new Button("-"));
p3.add(new Button("+"));
p3.add(new Button("Enter"));

add(panel, BorderLayout.CENTER);
add(p3, BorderLayout.EAST);