Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么在我最大化窗口后显示JButton和JTextField?_Java_Swing_User Interface_Layout Manager_Null Layout Manager - Fatal编程技术网

Java 为什么在我最大化窗口后显示JButton和JTextField?

Java 为什么在我最大化窗口后显示JButton和JTextField?,java,swing,user-interface,layout-manager,null-layout-manager,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, to

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


如对现已删除的答案的评论中所述,组件未显示的原因是,在添加组件之前,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);
    }
}

import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
公共级Kalkulator{
公共组件getGUI(){
//布局填充
int p=10;
//用户看到的GUI(无框架)
jpanelgui=newjpanel(newborderlayout(p,p));
setboorder(新的EmptyBorder(8,8,8,8));
JTextField=新的JTextField(10);
setFont(tf.getFont().deriveFont(22f));
添加(tf,BorderLayout.PAGE_START);
JLabel=新的JLabel(“用Luv制作-由怪物制作”);
添加(l,BorderLayout.PAGE_END);
JPanel lhs=新JPanel(新边界布局(p,p));
添加(左侧,BorderLayout.CENTER);
JPanel numpad=新JPanel(新网格布局(3,3,p,p));
添加(numpad,BorderLayout.CENTER);

对于(int ii=1;ii,如对现已删除的答案的评论中所述,组件未显示的原因是,在添加组件之前,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);
    }
}

import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
公共级Kalkulator{
公共组件getGUI(){
//布局填充
int p=10;
//用户看到的GUI(无框架)
jpanelgui=newjpanel(newborderlayout(p,p));
setboorder(新的EmptyBorder(8,8,8,8));
JTextField=新的JTextField(10);
setFont(tf.getFont().deriveFont(22f));
添加(tf,BorderLayout.PAGE_START);
JLabel=新的JLabel(“用Luv制作-由怪物制作”);
添加(l,BorderLayout.PAGE_END);
JPanel lhs=新JPanel(新边界布局(p,p));
添加(左侧,BorderLayout.CENTER);
JPanel numpad=新JPanel(新网格布局(3,3,p,p));
添加(numpad,BorderLayout.CENTER);

对于(int ii=1;iiiBefore this.setVisible(true);user this.pack();应该为代码块使用一致的逻辑缩进。代码缩进旨在帮助人们理解程序流!顺便说一句,在获得超过2090行代码之前,您应该早就意识到存在问题。请经常编译并运行!“如何实现此布局?”是一个更好的问题(IMO)。该GUI可以使用2个边框布局和3个网格布局来实现。在此之前。setVisible(true);user this.pack();应该为代码块使用一致的逻辑缩进。代码缩进是为了帮助人们理解程序流程!顺便说一句,在获得超过2090行代码之前,您应该早就意识到存在问题。经常编译并运行!“如何实现此布局?”应该是一个更好的问题(IMO)。该GUI可以使用2个边框布局和3个网格布局实现。引用了一个说明键绑定的相关示例。谢谢。我不知道setVisible()位置是否影响程序的输出。我不理解该注释。引用了一个说明键绑定的相关示例。谢谢。我不知道setVisible()是否职位影响了程序的输出,我不理解这个评论。