2011-10-19 11 views
4

嗨,我想嘗試使用編碼大小,佈局等自己製作一個計算器(試圖不使用NetBeans,它不是一個家庭作業)。但我面臨着一個關於空白空間的問題。我有一個TextArea和按鈕,但正如你可以看到下面我不能處理這個空間問題。這裏是我的代碼,關於簡單計算器中的佈局

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JSeparator; 
import javax.swing.JTextArea; 

public class calculator extends JFrame { 

    public calculator(){ 

     initComponents(); 

    } 

    private void initComponents(){ 

     JPanel panelScreen = new JPanel(new GridLayout(0,1)); 

     JTextArea screen = new JTextArea(); 
     panelScreen.add(screen); 

     JFrame frame = new JFrame("CALCULATOR"); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 

     JPanel panelButtons = new JPanel(new GridLayout(3,3)); 

     JButton oneButton = new JButton("1"); 
     panelButtons.add(oneButton); 

     JButton twoButton = new JButton("2"); 
     panelButtons.add(twoButton); 

     JButton threeButton = new JButton("3"); 
     panelButtons.add(threeButton); 

     JButton fourButton = new JButton("4"); 
     panelButtons.add(fourButton); 

     JButton fiveButton = new JButton("5"); 
     panelButtons.add(fiveButton); 

     JButton sixButton = new JButton("6"); 
     panelButtons.add(sixButton); 

     JButton sevenButton = new JButton("7"); 
     panelButtons.add(sevenButton); 

     JButton eightButton = new JButton("8"); 
     panelButtons.add(eightButton); 

     JButton nineButton = new JButton("9"); 
     panelButtons.add(nineButton); 

     frame.getContentPane().add(panelScreen, BorderLayout.NORTH); 
     //frame.getContentPane().add(new JSeparator(), BorderLayout.CENTER); 
     frame.getContentPane().add(panelButtons, BorderLayout.SOUTH); 
     frame.setBounds(50, 50, 500, 500); 
     frame.setResizable(false); 
     //frame.pack(); 
     frame.setVisible(true); 


    } 


    public static void main(String[] args) { 

     new calculator(); 

    } 

} 

和這個程序的圖片;

enter image description here

我很感激,如果你能幫助我。反正感謝:)

+0

不錯qeustion +1 – mKorbel

+1

你可以看看它是如何在這個[計算器代碼]完成(http://stackoverflow.com/questions/7441625/how-to-find-a - 按鈕 - 源在-AWT-計算器功課/ 7441804#7441804)。這可能不是你使用時所需要的,但可能會給你一些想法。 –

+0

BTW哪裏是'0'?你是否認爲0不是數字的人之一?我的第二個最小的妹妹就在你身邊(概念上)。 –

回答

4

Calculator GUI

import java.awt.*; 

import javax.swing.*; 
import javax.swing.border.EmptyBorder; 

// no need to extend frame! 
//public class Calculator extends JFrame { 
public class Calculator { 

    public Calculator(){  
     initComponents();  
    } 

    private void initComponents(){ 
     // I find it easier to create a panel and SET it as the content pane 
     JPanel gui = new JPanel(new BorderLayout(5,5)); 
     // add some padding to the main GUI 
     gui.setBorder(new EmptyBorder(4,4,4,4)); 

     // not needed if only a single compoinent is to be added! 
     //JPanel panelScreen = new JPanel(new GridLayout(0,1)); 

     // add some constraints to make the output field bigger. 
     // if it is intended to be single line, a JTextField should be used. 
     JTextArea screen = new JTextArea(2,25); 
     gui.add(screen, BorderLayout.NORTH); 
     //panelScreen.add(screen); 

     JFrame frame = new JFrame("CALCULATOR"); 
     frame.setContentPane(gui); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // add padding around the buttons 
     JPanel panelButtons = new JPanel(new GridLayout(3,3,4,4)); 

     JButton oneButton = new JButton("1"); 
     panelButtons.add(oneButton); 

     JButton twoButton = new JButton("2"); 
     panelButtons.add(twoButton); 

     JButton threeButton = new JButton("3"); 
     panelButtons.add(threeButton); 

     JButton fourButton = new JButton("4"); 
     panelButtons.add(fourButton); 

     JButton fiveButton = new JButton("5"); 
     panelButtons.add(fiveButton); 

     JButton sixButton = new JButton("6"); 
     panelButtons.add(sixButton); 

     JButton sevenButton = new JButton("7"); 
     panelButtons.add(sevenButton); 

     JButton eightButton = new JButton("8"); 
     panelButtons.add(eightButton); 

     JButton nineButton = new JButton("9"); 
     panelButtons.add(nineButton); 

     //frame.getContentPane().add(new JSeparator(), BorderLayout.CENTER); 

     // Add the buttons to the CENTER and they will 
     // fill whatever space they are provided. 
     gui.add(panelButtons, BorderLayout.CENTER); 
     //frame.setBounds(50, 50, 500, 500); 
     //frame.setResizable(false); 
     frame.pack(); 
     frame.setVisible(true); 
    }  

    public static void main(String[] args) {  
     java.awt.EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      new Calculator(); 
     } 
     });  
    }  
} 
+2

請注意,'BorderLayout'是一個* layout *,用於組件中包含的組件定位**,而'EmptyBorder'是一個*邊框*,**包圍組件**(面板)。這是否會爲您解決任何困惑,還是我誤解了您的問題? –

+0

是的,我終於明白了。謝謝 :) – quartaela

5

幾點建議:

  1. 不要設置Jframe的大小,而事實上沒有設置任何尺寸。
  2. 給所有組件調用包以設置自己的尺寸。
  3. 如果您希望更大的按鈕,請考慮更改其字體的大小。

例如,

Calc2 GUI

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

import javax.swing.*; 

public class Calc2 { 
    public static final String[][] BUTTON_TEXTS = { 
     {"7", "8", "9", "+"}, 
     {"4", "5", "6", "-"}, 
     {"1", "2", "3", "*"}, 
     {"0", ".", "/", "="} 
    }; 
    public static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 24); 

    private static void createAndShowGui() { 
     JTextField field = new JTextField(10); 
     field.setFont(BTN_FONT.deriveFont(Font.PLAIN)); 
     JPanel btnPanel = new JPanel(new GridLayout(BUTTON_TEXTS.length, 
      BUTTON_TEXTS[0].length)); 

     for (int i = 0; i < BUTTON_TEXTS.length; i++) { 
     for (int j = 0; j < BUTTON_TEXTS[i].length; j++) { 
      JButton btn = new JButton(BUTTON_TEXTS[i][j]); 
      btn.setFont(BTN_FONT); 
      btnPanel.add(btn); 
     } 
     } 

     JPanel mainPanel = new JPanel(new BorderLayout()); 
     mainPanel.add(field, BorderLayout.PAGE_START); 
     mainPanel.add(btnPanel, BorderLayout.CENTER); 


     JFrame frame = new JFrame("Calc2"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

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

,但如果我不調整JFrame的大小,它的尺寸非常小。我想要更大的按鈕與文本area_? – quartaela

+3

再次,設置字體,但不設置大小,否則您會看到上面顯示的問題,並且在其他操作系統或此操作系統上以不同的屏幕分辨率運行時,會出現不兼容的GUI。見上面的編輯。 –

+0

@Andrew:請畫一個截圖。 :) –

4
  1. Laying Out Components Within a Container
  2. 實施適當的佈局(S)

編輯 -

快速解決方案 - 用BoxLayout替換JFrame佈局管理器。 setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)))。

+0

其實我剛剛完成那個教程5分鐘前:D。然後我開始製作這個計算器。 – quartaela

+0

@quartaela:這是要走的路,1+ –

+0

@quartaela,請參閱編輯。 – mre

-2

快速和髒溶液:

取消註釋的setBounds()並將其更改爲frame.setBounds(50,50,150,125);然而,這並不是理想的解決方案。

我會重新設計我使用我的佈局面板的方式,使其更靈活,使用不同的頂層佈局。當前的BorderLayout(默認值)不適合此類設計。

+0

是的,但這將只是這個應用程序的工作:D – quartaela

+1

@quart:你不想這樣做,不僅不是理想的,它只是錯誤的。 –

+0

我會低頭羞愧:) – Ewald

4

您可能會喜歡研究這個遵循@HFOE和@mre建議的example。請注意,「大小」在代碼中無處顯示。

+0

謝謝我會考慮這個例子:) – quartaela