简体   繁体   中英

JFrame shows Content only when u drag the frame bigger?

I got very simple code it shows a very simple calculator GUI without function, but unfortunately the content only appears when I drag the window bigger. I think it's a problem whit the code structure, but I have no idea how to fix that.

Maybe it is a bug from my computer or problem with my IDE, I'm using IntelliJ. I searched a lot in web, but the problem is kinda hard to find.

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

public class Calculator {

    public void showGUI(){
        JFrame window = new JFrame("Calculator");
        Container cont = window.getContentPane();
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.setSize(300,300);
        window.setVisible(true);

        JTextField textfieldfirstinput = new JTextField("                ");
        JTextField textfieldsecondinput = new JTextField("                ");
        JTextField textfieldsolutionoutput = new JTextField("                ");

        JButton buttonadden = new JButton("+");
        JButton buttonmultiply = new JButton("*");
        JButton buttonclear = new JButton("Clear");
        JButton buttonexit = new JButton("Exit");

        JPanel buttonpanel = new JPanel();
        JPanel textfieldpanel = new JPanel();

        textfieldpanel.add(textfieldfirstinput,textfieldsecondinput);
        textfieldpanel.add(textfieldsolutionoutput);
        //textfieldpanel.setLayout();
        textfieldpanel.setVisible(true);

        buttonpanel.add(buttonadden);
        buttonpanel.add(buttonmultiply);
        buttonpanel.add(buttonclear);
        buttonpanel.add(buttonexit);
        //buttonpanel.setLayout(new FlowLayout());
        buttonpanel.setVisible(true);

        cont.add(buttonpanel,BorderLayout.CENTER);
        cont.add(textfieldpanel,BorderLayout.CENTER);
        cont.setLayout(new GridLayout(3,4));
        window.add(cont);


    }
    public static void main(String[] args){
        Calculator calc = new Calculator();
        calc.showGUI();
    }
}

There are 2 main issues in your code:

  1. window.setVisible(true); should be called last in your code, after you've added everything to it. That will solve the issue of the resizing.

  2. You also get an error when running it:

     Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself at java.awt.Container.checkAddToSelf(Unknown Source) at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at javax.swing.JFrame.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at com.sof.frakcool.Calculator.showGUI(Calculator.java:47) at com.sof.frakcool.Calculator.main(Calculator.java:56)

    This means that you're adding a component to itself.

    When you call Container cont = window.getContentPane(); , you're not removing that component from its parent, just creating a reference to it, so there's no need for this line

     window.add(cont);

    And then you'll have your program running just fine.

Also there's no need to call:

textfieldpanel.setVisible(true);
buttonpanel.setVisible(true);

And as an additional tip, you can replace this:

JTextField textfieldfirstinput = new JTextField("                ");

With

JTextField textfieldfirstinput = new JTextField(10);

Which will set the number of columns it will expand, you won't have to remove all the spaces when you write into it.

And please follow Java Naming Conventions using camel-case to name your properties, and give them meaningful names, such as firstNumberInput rather than textfieldfirstinput

  • FirstWordUpperCaseClass
  • firstWordLowerCaseVariable
  • firstWordLowerCaseMethod()
  • ALL_WORDS_UPPER_CASED_CONSTANT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM