简体   繁体   中英

Swing GUI in Eclipse

I'm trying to create a Swing GUI in Eclipse by following the guide in Oracle's documentation. However, they use Netbeans in the tutorial and I believe that might be the cause of the problem.

I've tried to create a temperature converter but the label for the fahrenheit number is repeatedly complaining that it cannot be resolved. Here is the source code:

private JPanel contentPane;
private JTextField tempTextField; // Not sure if I need the other components here as well?
                                  // Could be needed in order to be used in the 
                                  // actionPerformed method.

...

JButton convertButton = new JButton("Convert");
    convertButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            //Parse degrees Celsius as a double and convert to Fahrenheit.
            int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
                    * 1.8 + 32);
            fahrenheitLabel.setText(tempFahr + " Fahrenheit"); // fahrenheitLabel is what's 
        }                                                     //highlighted in Eclipse.
    });
    convertButton.setBounds(6, 38, 134, 29);
    contentPane.add(convertButton);

    JLabel fahrenheitLabel = new JLabel("Fahrenheit");
    fahrenheitLabel.setBounds(152, 43, 78, 16);
    contentPane.add(fahrenheitLabel);

The link to the Oracle documentation is here: http://docs.oracle.com/javase/tutorial/uiswing/learn/creatinggui.html

I should point out the error persists even when fahrenheitLabel is declared prior to the method - so it definitely can't be the ordering of the code that matters (I think).

If it's anything to do with the components having incorrect names I would be surprised, but here is a screenshot:

在此处输入图片说明

Many thanks to anyone who can give suggestions on the matter!

EDIT: Thank you to those who have already given suggestions to fiddle with the ordering of the code. However, it then tells me I need to make the fahrenheitLabel final, which I would have thought conflicts with the ability to dynamically change upon the user entering different inputs.

EDIT 2: Apparently making the variable final is valid. Thank you to all for your suggestions.

Of course it doesnt know the label, because you declare it after the method. Try putting

JLabel fahrenheitLabel = new JLabel("Fahrenheit");

before

convertButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        //Parse degrees Celsius as a double and convert to Fahrenheit.
        int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
                * 1.8 + 32);
        fahrenheitLabel.setText(tempFahr + " Fahrenheit"); // fahrenheitLabel is what's 
    }                                                     //highlighted in Eclipse.
});
public class SimpleDemo extends JFrame {

    JLabel jLabel = new JLabel("Changing label");
    JButton button = new JButton("Click");
    static int i = 0;



    public SimpleDemo() {
            // TODO Auto-generated constructor stub
            this.setSize(500, 500);

            this.add(button);
            this.add(jLabel);
            this.setLayout(new FlowLayout(2, 2, 2));
            this.setVisible(true);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);

            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    // TODO Auto-generated method stub
                    jLabel.setText("Changed Text" + i++);

                }
            });
        }

        public static void main(String args[]) {
            System.out.println("Woring fine********");
            SimpleDemo demo = new SimpleDemo();

        }
    }

Check this one might be useful to u.

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