Question

I try to make calculator with Netbeans. But the component of calculator does not show until I maximize the window. Why?

This is the source code:

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
    }
}

For reference, this is how the GUI is supposed to look.

Kalkulator

Was it helpful?

Solution

As mentioned in a comment to a now deleted answer, the reason the components were not showing up was because the GUI was set visible prior to them being added, and without a subsequent call that would cause the frame to be laid out correctly, they would not appear.

Here is a crude facsimile of the desired GUI. Read the tips in comments. Adjust the int p for more or less space as required.

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top