1

我尝试用 Netbeans 制作计算器。但是直到我最大化窗口,计算器的组件才会显示。为什么?

这是源代码:

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 的外观。

计算器

4

1 回答 1

1

正如对现已删除的答案的评论中所述,组件未显示的原因是因为在添加它们之前将 GUI 设置为可见,并且没有后续调用会导致框架正确布局,它们会没出现。

这是所需 GUI 的粗略复制品。阅读评论中的提示。根据需要调整int p更多或更少的空间。

在此处输入图像描述

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);
    }
}
于 2013-04-21T12:33:05.863 回答