Why does my really simple java program sometimes work and sometimes it doesn’t work?
I've just started using Java and only used PHP before - it's hard to find object - oriented things I am using the eclipse ide
I'm trying to make a program that can tell you the weight on another planet - it looks simple
What I have done so far is to create half the interface in swing (this is called?)
Sometimes I run it and it will appear as I expected, title, text box, etc Other times (when absolutely nothing has changed), it will only show a blank screen
The image shows how it works When it doesn't work, there are no objects It works about 20% of the time
I think this may be because of my drop-down menu – or JCombo@R_939_2419 @, this has always been a headache – eclipse asked me to add "< Object >" every time I mentioned it JCombo@R_939_2419 @After – it says“ JCombo@R_939_2419 @Is the original type For generic types JCombo@R_939_2419 @References to should be parameterized
I don't know why, and I may just be really thick. Sorry, if this is a stupid problem, but how can I solve this problem and 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 @R_939_2419@es and Lists JTextField txtInputMass = new JTextField(5); JCombo@R_939_2419@<Object> comInputUnits; String arrUnits[] = {"Kilos","Stone","Pounds"}; comInputUnits = new JCombo@R_939_2419@<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, this is a long question, and I will be very grateful for any suggestions or answers. As I said, I know about Java barley, I've just started - thank you:)
Solution
You should
>Only manipulate swing components in event dispatch thread; > Setvisible (true) is called only after all components are added to the framework; > Spelling only one n planet, though not so important
< Object > "eclipse lets you add" is called a generic type Read tutorial on genetics