1

我試圖用Netbeans製作計算器。但是,只有在我最大化窗口之前,計算器的組件纔會顯示出來。爲什麼?爲什麼JButton和JTextField在最大化窗口後顯示?

這是源代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

class Kalkulator extends JFrame implements ActionListener { 

    JTextField tampilan; 
    JLabel label; 
    JButton tom1, tom2, tom3, tom4, tom5, tom6, tom7, tom8, tom9, tom0, 
      tompl, tomkr, tombg, tomkl, tomsd, tomac; 
    double hasil, bil1, bil2; 
    String tanda; 

    public Kalkulator() { 
//DESAIN 
//FRAME 
     super("Kalkulator"); 
     this.setSize(350, 350);//mengatur size frame 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//membuat program berhenti ketika close diklik 
     this.setVisible(true);//membuat GUI jadi tampak 
     this.getContentPane().setLayout(null);//tidak menggunakan layout apapun, sehingga ukuran konten harus diatur secara manual 

//signature 
     label = new JLabel(); 
     label.setText("Created By NovusZ");//setText untuk mengatur teks dari pada label 
     label.setBounds(20, 250, 150, 100);//setBounds untuk mengatur posisi dan besar dari objek yang dimaksud 
     this.getContentPane().add(label);//menambahkan objek yang dimaksud kedalam frame 
//LAYAR 
//tampilan 
     tampilan = new JTextField(); 
     tampilan.setBounds(10, 20, 300, 40); 
     tampilan.setBackground(Color.WHITE); 
     tampilan.setForeground(Color.BLUE); 
     this.getContentPane().add(tampilan); 

//TOMBOL 
//Tombol1 
     tom1 = new JButton("1"); 
     tom1.setBounds(10, 70, 45, 30); 
     this.getContentPane().add(tom1); 
//Tombol2 
     tom2 = new JButton("2"); 
     tom2.setBounds(10 + 60, 70, 45, 30); 
     this.getContentPane().add(tom2); 
//Tombol3 
     tom3 = new JButton("3"); 
     tom3.setBounds(10 + 120, 70, 45, 30); 
     this.getContentPane().add(tom3); 
//Tombol4 
     tom4 = new JButton("4"); 
     tom4.setBounds(10, 70 + 40, 45, 30); 
     this.getContentPane().add(tom4); 
//Tombol5 
     tom5 = new JButton("5"); 
     tom5.setBounds(10 + 60, 70 + 40, 45, 30); 
     this.getContentPane().add(tom5); 
//Tombol6 
     tom6 = new JButton("6"); 
     tom6.setBounds(10 + 120, 70 + 40, 45, 30); 
     this.getContentPane().add(tom6); 
//Tombol7 
     tom7 = new JButton("7"); 
     tom7.setBounds(10, 70 + 80, 45, 30); 
     this.getContentPane().add(tom7); 
//Tombol8 
     tom8 = new JButton("8"); 
     tom8.setBounds(10 + 60, 70 + 80, 45, 30); 
     this.getContentPane().add(tom8); 
//Tombol9 
     tom9 = new JButton("9"); 
     tom9.setBounds(10 + 120, 70 + 80, 45, 30); 
     this.getContentPane().add(tom9); 
//Tombol0 
     tom0 = new JButton("0"); 
     tom0.setBounds(10, 70 + 120, 165, 30); 
     this.getContentPane().add(tom0); 
//TombolPLUS 
     tompl = new JButton("plus"); 
     tompl.setBounds(10 + 200, 70, 100, 30); 
     this.getContentPane().add(tompl); 
//Tombolkurang 
     tomkr = new JButton("Kurang"); 
     tomkr.setBounds(10 + 200, 70 + 40, 100, 30); 
     this.getContentPane().add(tomkr); 
//Tombolbagi 
     tombg = new JButton("BAGI"); 
     tombg.setBounds(10 + 200, 70 + 80, 100, 30); 
     this.getContentPane().add(tombg); 
//TombolSAMADENGAN 
     tomkl = new JButton("kali"); 
     tomkl.setBounds(10 + 200, 70 + 120, 100, 30); 
     this.getContentPane().add(tomkl); 
//TombolSAMADENGAN 
     tomsd = new JButton("(HASIL)"); 
     tomsd.setBounds(10 + 200, 70 + 160, 100, 30); 
     this.getContentPane().add(tomsd); 
//Tombolreset 
     tomac = new JButton("Reset"); 
     tomac.setBounds(10, 70 + 160, 165, 30); 
     this.getContentPane().add(tomac); 

     tom1.addActionListener(this); 
     tom2.addActionListener(this); 
     tom3.addActionListener(this); 
     tom4.addActionListener(this); 
     tom5.addActionListener(this); 
     tom6.addActionListener(this); 
     tom7.addActionListener(this); 
     tom8.addActionListener(this); 
     tom9.addActionListener(this); 
     tom0.addActionListener(this); 
     tompl.addActionListener(this); 
     tomkr.addActionListener(this); 
     tomkl.addActionListener(this); 
     tomsd.addActionListener(this); 
     tombg.addActionListener(this); 
     tomac.addActionListener(this); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     //event 
     //input 
     if (e.getSource() == tom1) { 
      tampil("1"); 
     } else if (e.getSource() == tom2) { 
      tampil("2"); 
     } else if (e.getSource() == tom3) { 
      tampil("3"); 
     } else if (e.getSource() == tom4) { 
      tampil("4"); 
     } else if (e.getSource() == tom5) { 
      tampil("5"); 
     } else if (e.getSource() == tom6) { 
      tampil("6"); 
     } else if (e.getSource() == tom7) { 
      tampil("7"); 
     } else if (e.getSource() == tom8) { 
      tampil("8"); 
     } else if (e.getSource() == tom9) { 
      tampil("9"); 
     } else if (e.getSource() == tom0) { 
      tampil("0"); 
     } //tombol reset 
     else if (e.getSource() == tomac) { 
      hasil = 0; 
      bil1 = 0; 
      bil2 = 0; 
      tanda = ""; 
      tampilan.setText(""); 
      tampil(""); 
     } //tombol + 
     else if (e.getSource() == tompl) { 
      tanda = "+";//membuat tanda sebagai detektor untuk mengetahui operasi apa yang dipergunakan 
      bil1 = Double.parseDouble(tampilan.getText());//mengisi nilai bil1 dari nilai yang ada di tampilan 
      tampilan.setText(""); 
      tampilan.setBackground(Color.YELLOW); 
      tampilan.setForeground(Color.BLUE); 
//tombol - 
     } else if (e.getSource() == tomkr) { 
      tanda = "-"; 
      bil1 = Double.parseDouble(tampilan.getText()); 
      tampilan.setText(""); 
      tampilan.setBackground(Color.YELLOW); 
      tampilan.setForeground(Color.BLUE); 
     } //tombol X 
     else if (e.getSource() == tomkl) { 
      tanda = "x"; 
      bil1 = Double.parseDouble(tampilan.getText()); 
      tampilan.setText(""); 
     } //tombol bagi 
     else if (e.getSource() == tombg) { 
      tanda = "/"; 
      bil1 = Double.parseDouble(tampilan.getText()); 
      tampilan.setText(""); 
      tampilan.setBackground(Color.YELLOW); 
      tampilan.setForeground(Color.BLUE); 
     } else if (e.getSource() == tomsd) { 
      bil2 = Double.parseDouble(tampilan.getText());//mengisi nilai bil2 dengan nilai yang ada pada tampilan setelah tombol tanda operasi ditekan 
      tampilan.setBackground(Color.WHITE); 
      tampilan.setForeground(Color.BLUE); 

      //operasi perhitungan 
      //operasi penjumlahan 
      if (tanda.equals("+")) { 
       hasil = bil1 + bil2; 
       tampilan.setText("" + hasil); 
       tampilan.setForeground(Color.YELLOW); 
       tampilan.setBackground(Color.BLUE); 
      }//operasi pengurangan 
      else if (tanda.equals("-")) { 
       hasil = bil1 - bil2; 
       tampilan.setText("" + hasil); 
       tampilan.setForeground(Color.YELLOW); 
       tampilan.setBackground(Color.BLUE); 
      }//operasi perkalian 
      else if (tanda.equals("x")) { 
       hasil = bil1 * bil2; 
       tampilan.setText("" + hasil); 
       tampilan.setForeground(Color.YELLOW); 
       tampilan.setBackground(Color.BLUE); 
      }//operasi pembagian 
      else if (tanda.equals("/")) { 
       hasil = bil1/bil2; 
       tampilan.setText("" + hasil); 
       tampilan.setForeground(Color.YELLOW); 
       tampilan.setBackground(Color.BLUE); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     Kalkulator x = new Kalkulator(); 
    } 

    public void tampil(String teks) {//membuat method utk menampilkan apa yang telah ditekan pada tombol 
     tampilan.setText(tampilan.getText() + teks);//set tulisan pada label tampilan. yang diisikan adalah teks yang sudah ditetapkan pada masing-masing tombol dan teks sebelumnya yang sudah ditekan 
    } 
} 

以供參考,這是GUI應該如何看。

Kalkulator

+1

之前this.setVisible(真);用戶this.pack();它應該沒問題 – DRastislav 2013-04-21 11:37:56

+0

對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程!順便說一句,在獲得2090行代碼之前,您應該已經意識到存在問題了。編譯&運行經常! – 2013-04-21 11:43:01

+0

*「如何實現這種佈局?」*會是一個更好的問題(海事組織)。該GUI可以使用2個邊界佈局和3個網格佈局來實現。 – 2013-04-21 12:11:46

回答

1

如到現在刪除答案的評論中提到,該組件沒有顯示出來的原因是因爲GUI設置可見被添加之前給他們,並沒有後續調用,將導致框架要正確擺放,它們不會出現。

這是所需GUI的粗略傳真。閱讀評論中的提示。根據需要調整int p或多或少的空間。

enter image description here

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

public class Kalkulator { 

    public Component getGUI() { 
     // layout padding 
     int p = 10; 
     // the GUI as seen by the user (without frame) 
     JPanel gui = new JPanel(new BorderLayout(p,p)); 
     gui.setBorder(new EmptyBorder(8, 8, 8, 8)); 

     JTextField tf = new JTextField(10); 
     tf.setFont(tf.getFont().deriveFont(22f)); 
     gui.add(tf, BorderLayout.PAGE_START); 

     JLabel l = new JLabel("Crafted with Luv - by monsters"); 
     gui.add(l, BorderLayout.PAGE_END); 

     JPanel lhs = new JPanel(new BorderLayout(p,p)); 
     gui.add(lhs, BorderLayout.CENTER); 

     JPanel numpad = new JPanel(new GridLayout(3,3,p,p)); 
     lhs.add(numpad, BorderLayout.CENTER); 
     for (int ii=1; ii<10; ii++) { 
      numpad.add(new JButton("" + ii)); 
     } 
     JPanel extraButtons = new JPanel(new GridLayout(0,1,p,p)); 
     lhs.add(extraButtons, BorderLayout.PAGE_END); 
     extraButtons.add(new JButton("0")); 
     extraButtons.add(new JButton("Reset")); 

     JPanel rhs = new JPanel(new GridLayout(0,1,p,p)); 
     rhs.add(new JButton("plus")); 
     rhs.add(new JButton("Kurang")); 
     rhs.add(new JButton("BAGI")); 
     rhs.add(new JButton("kali")); 
     rhs.add(new JButton("(HASIL)")); 
     gui.add(rhs, BorderLayout.LINE_END); 

     return gui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       Kalkulator k = new Kalkulator(); 

       JFrame f = new JFrame("Kalkulator"); 
       f.add(k.getGUI()); 
       // Ensures JVM closes after frame(s) closed and 
       // all non-daemon threads are finished 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       // See http://stackoverflow.com/a/7143398/418556 for demo. 
       f.setLocationByPlatform(true); 

       // ensures the frame is the minimum size it needs to be 
       // in order display the components within it 
       f.pack(); 
       // should be done last, to avoid flickering, moving, 
       // resizing artifacts. 
       f.setVisible(true); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html 
     SwingUtilities.invokeLater(r); 
    } 
} 
+1

表示鍵綁定一個相關的示例引用[這裏](http://stackoverflow.com/a/7371397/230513)。 – trashgod 2013-04-21 13:18:03

+0

謝謝。我不知道setVisible()的位置是否影響程序的輸出 – novusZ 2013-04-24 01:53:53

+0

我不明白那個評論。 – 2013-04-24 01:57:58