2013-03-26 77 views
1

我需要鍵盤上數字鍵盤的佈局。我得到了大部分,我唯一的問題是,「進入」按鈕應該是直到底部。但是,當我使用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);  
} 
} 
+0

可以添加圖片或因此看到它看起來怎麼樣,你如何想要它? – Smit 2013-03-26 17:30:08

+0

我沒有20個代表。對不起,但這裏是它應該看起來像什麼的鏈接。 http://i.imgur.com/bjdLRfj.png,這是我有http://i.imgur.com/6XIUNEh.png – FJam 2013-03-26 17:34:47

回答

1

無需重新安排好一切,你可以simlpy巢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); 
      } 
     }); 
    } 
} 
+0

當我這樣做時,由於某種原因,中心消失 – FJam 2013-03-26 18:34:08

+0

@FJam你確定你沒有兩次加入中心?如果您運行我提供的確切代碼,問題是否仍然存在?如果沒有,那麼你的代碼中可能有一些錯誤,你應該試着找出你的代碼與我的不同。 – 2013-03-26 18:43:48

+0

哦謝謝,我得到它的工作,我錯過了其中的一條線。 – FJam 2013-03-26 19:10:11

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); 
+0

你知道我可以使用它的工作佈局嗎? – FJam 2013-03-26 18:11:01

+0

@FJam你應該可以通過BorderLayout和GridLayout的組合來完成它。查看本頁面上已有的幾個示例。 – whiskeyspider 2013-03-26 18:12:30