0
votes

Following an online tutorial I am trying to create a GUI in java using swing. I have tried searching for an ansewer online but i couldn.t find any. Here is the code:

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Container;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing;

//Create JFrame Container
public class River
{
public River()
{
    JFrame jframe = new JFrame();
    JPanel panel = new JPanel();
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPanel = jframe.getContentPane();
    GroupLayout grouplayout = new GroupLayout(contentPanel);
    contentPanel.setLayout(grouplayout);

    JLabel clickMe = new JLabel("Click Here");
    JButton button = new JButton("This Button");

    grouplayout.setHorizontalGroup(
        grouplayout.createSequentialGroup()
            .addComponent(clickMe)
            .addGap(10,20,100)
            .addComponent(button));

    grouplayout.setVerticalGroup(
        groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(clickMe)
            .addComponent(button));

    jframe.setTitle("Our GUI");
    jframe.pack();
    jframe.setVisible(true);
}
public static void main(String[] args) //main method
{
    new River();
} // end of main
}//end River class

However when i try run the code using JGRASP i get the follow errors:

River.java:4: error: cannot find symbol import javax.swing.Container; ^ symbol: class Container location: package javax.swing River.java:8: error: package javax does not exist import javax.swing; ^ River.java:22: error: cannot find symbol Container contentPanel = jframe.getContentPane(); ^ symbol: class Container location: class River River.java:27: error: cannot find symbol JButton button = new JButton("This Button"); ^ symbol: class JButton location: class River River.java:27: error: cannot find symbol JButton button = new JButton("This Button"); ^ symbol: class JButton location: class River River.java:36: error: cannot find symbol groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE) ^ symbol: variable groupLayout location: class River 6 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Any assistance will be much appreciated.

1
even after using import statement - so the trick is to read the API for the class in question to make sure you are using the proper import statement. - camickr

1 Answers

1
votes

Replace

import javax.swing.Container;

with

import java.awt.Container;