java - 为什么最大化窗口后会显示 JButton 和 JTextField?

标签 java swing user-interface layout-manager null-layout-manager

我尝试使用 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 的外观。

Kalkulator

最佳答案

正如对现已删除的答案的评论中提到的,组件未显示的原因是因为 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);
    }
}

关于java - 为什么最大化窗口后会显示 JButton 和 JTextField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16130689/

相关文章:

java - 如何将列表的 Jackson 自定义反序列化器添加到模块?

java - 标注 Adempiere 中的确认(是/否)对话框

java - JOptionPane 未返回正确的值

java - 访问已经创建JFrame

java - 打包 JMeter Gui

javax.xml.ws.WebServiceException : Method X is exposed as WebMethod, 但是没有对应的wsdl操作

java - OpenAM 与 Maven

java - 在 JFrame 中更改 JPanel 后如何请求对 JComponent 的关注

google-app-engine - Google AppEngine 数据存储的 GUI

java - 在 Java 中绘制多个对象