简体   繁体   中英

Why does my really really simple Java program which doesn't yet do anything sometimes work and sometimes doesn't?

I'm just starting out in Java, and only ever used PHP before - finding it hard to get my head round the object-orientated thing. I am using the Eclipse IDE.

I am trying to make a program that will tell you your weight on another planet - seems simple enough

All I have done so far is make half the interface in Swing (is that what it's called?)

Sometimes I run it, and it comes up as I would expect, with the title, textboxes etc.... other times (when absolutely no changes have been made), it just comes up with a blank screen 在此输入图像描述

The image shows what it looks like when it is working. When it's not working, there are just no objects. It works about 20% of the time.

I think this might have been because of my drop down menu - or JComboBox, this has been such a head ache - Eclipse made me add "< Object>" after each mention of JComboBox - it said "JComboBox is a raw type. References to generic type JComboBox should be parameterized"

I have no idea why this is, and I'm probably just being really thick, sorry if this is a stupid question, but how can I fix this problem, what's wrong with my code?

package calc;

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

public class View extends JFrame {

static String titleText = "Calculate your Mass on another Plannet";

public View(){
    super(titleText);
    setSize(500,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    FlowLayout flo = new FlowLayout();
    setLayout(flo);

    JPanel inputData = new JPanel();


    //Labels
    JLabel lblTitle = new JLabel (titleText, JLabel.CENTER);
    lblTitle.setFont(new Font("Arial", Font.BOLD, 24));
    JLabel lblInputMass = new JLabel ("Weight", JLabel.LEFT);
    JLabel lblInputUnits = new JLabel("Units");     


    //Input Boxes and Lists
    JTextField txtInputMass = new JTextField(5);

    JComboBox<Object> comInputUnits;
    String arrUnits[] = {"Kilos", "Stone", "Pounds"};
    comInputUnits = new JComboBox<Object>(arrUnits);
    comInputUnits.setSelectedIndex(1);


    //Buttons
    JButton btnCalculate = new JButton("Calculate");

    //Append objects
    add(lblTitle);
    inputData.add(lblInputMass);
    inputData.add(txtInputMass);
    inputData.add(lblInputUnits);
    inputData.add(comInputUnits);
    inputData.add(btnCalculate);

    add(inputData);


}
/**
 * @param args
 */
public static void main(String[] args) {
    View sal = new View();


}

}

Sorry it's quite a long question, and I'd be so grateful to any suggestions or answers, as I said, I know barley anything about Java, and am just starting out - thank you :)

You should

  • only manipulate Swing components inside the event dispatch thread ;
  • call setVisible(true) only after all the components have been added to the frame;
  • spell Planet with only one n , although that's less important.

The <Object> that "Eclipse made you add" are called generic types. Read the tutorial on generics .

You need to do everything dealing with Swing components inside the EventDispatchThread (EDT).

wrap the call to new View() in a call to SwingUtilities.invokeAndWait()

Always call pack() . This version works reliably (at least to appear).

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

public class View extends JFrame {

    static String titleText = "Calculate your Weight on another Planet";

    public View(){
        super(titleText);
        // not now!
        //setSize(500,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setVisible(true);

        FlowLayout flo = new FlowLayout();
        setLayout(flo);

        JPanel inputData = new JPanel();

        //Labels
        JLabel lblTitle = new JLabel (titleText, JLabel.CENTER);
        lblTitle.setFont(new Font("Arial", Font.BOLD, 24));
        JLabel lblInputMass = new JLabel ("Weight", JLabel.LEFT);
        JLabel lblInputUnits = new JLabel("Units");     

        //Input Boxes and Lists
        JTextField txtInputMass = new JTextField(5);

        JComboBox comInputUnits;
        String arrUnits[] = {"Kilos", "Stone", "Pounds"};
        comInputUnits = new JComboBox(arrUnits);
        comInputUnits.setSelectedIndex(1);

        //Buttons
        JButton btnCalculate = new JButton("Calculate");

        //Append objects
        add(lblTitle);
        inputData.add(lblInputMass);
        inputData.add(txtInputMass);
        inputData.add(lblInputUnits);
        inputData.add(comInputUnits);
        inputData.add(btnCalculate);

        add(inputData);

        // force the container to layout the components.  VERY IMPORTANT!
        pack();
        setSize(500,400);
        setVisible(true);
    }

    public static void main(String[] args) {
        // This is what JB Nizet was alluding to..
        // 'start (or update) the GUI on the EDT'
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new View();
            }
        });
    }
}

Try this out:

public static void main(String[] args) {
            View sal = new View();
            sal.setSize(500, 400);
            sal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            sal.setVisible(true);

}

Comment these lines after the constructor:

super(titleText);
setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

Hope that helps.

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