Problem running GUI in "Java Application" mode - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem running GUI in "Java Application" mode

DavefromCanadaDavefromCanada Posts: 36Member
I have a problem. I'm trying to display my textfields, labels, and a button and run it as a Java Application, but all I got is an empty frame with grey-coloured background as my output. What did I do wrong?

Here's the program:

[code]import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class AppletViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FIELD_WIDTH = 5;
final JTextField xField = new JTextField(FIELD_WIDTH);
final JTextField yField = new JTextField(FIELD_WIDTH);

JButton moveButton = new JButton("Move");
JLabel l1 = new JLabel("X =");
JLabel l2 = new JLabel("Y =");
JPanel somePanel = new JPanel();

somePanel.add(l1);
somePanel.add(xField);
somePanel.add(l2);
somePanel.add(yField);
somePanel.add(moveButton);

frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private static final int FRAME_WIDTH = 900;
private static final int FRAME_HEIGHT = 500;
}[/code]


thanks,

DFC

Comments

  • pahpah Posts: 83Member
    : I have a problem. I'm trying to display my textfields, labels, and a button and run it as a Java Application, but all I got is an empty frame with grey-coloured background as my output. What did I do wrong?
    :
    : Here's the program:
    :
    : [code]import javax.swing.JFrame;
    : import java.awt.BorderLayout;
    : import java.awt.event.ActionListener;
    : import java.awt.event.ActionEvent;
    : import javax.swing.JButton;
    : import javax.swing.JPanel;
    : import javax.swing.JButton;
    : import javax.swing.JTextArea;
    : import javax.swing.JTextField;
    : import javax.swing.JLabel;
    : import java.applet.Applet;
    : import java.awt.Graphics;
    : import java.awt.Graphics2D;
    : import java.awt.Rectangle;
    :
    : public class AppletViewer
    : {
    : public static void main(String[] args)
    : {
    : JFrame frame = new JFrame();
    : final int FIELD_WIDTH = 5;
    : final JTextField xField = new JTextField(FIELD_WIDTH);
    : final JTextField yField = new JTextField(FIELD_WIDTH);
    :
    : JButton moveButton = new JButton("Move");
    : JLabel l1 = new JLabel("X =");
    : JLabel l2 = new JLabel("Y =");
    : JPanel somePanel = new JPanel();
    :
    : somePanel.add(l1);
    : somePanel.add(xField);
    : somePanel.add(l2);
    : somePanel.add(yField);
    : somePanel.add(moveButton);
    :
    : [red]frame.getContentPane().add(somePanel);[/red]
    : frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    : frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : frame.setVisible(true);
    : }
    :
    : private static final int FRAME_WIDTH = 900;
    : private static final int FRAME_HEIGHT = 500;
    : }[/code]
    :
    :
    : thanks,
    :
    : DFC
    :

  • DavefromCanadaDavefromCanada Posts: 36Member
    Thanks, Pah for the advice. I can get the output working. Now, I want to implement an Actionlistener for the "moveButton" feature. However, though, when I modified the code and compiled, I get an error stating that there's an "Illegal start of expression" from the paintComponent method. Can anybody help me on this one? Here's the modified program:

    [code]import javax.swing.JFrame;
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;

    public class AppletViewer
    {
    public static void main(String[] args)
    {
    Rectangle box = new Rectangle(10,20,30,40);

    JFrame frame = new JFrame();
    final int FIELD_WIDTH = 5;
    final JTextField xField = new JTextField(FIELD_WIDTH);
    final JTextField yField = new JTextField(FIELD_WIDTH);

    JButton moveButton = new JButton("Move");
    JLabel l1 = new JLabel("X =");
    JLabel l2 = new JLabel("Y =");
    JPanel somePanel = new JPanel();

    somePanel.add(l1);
    somePanel.add(xField);
    somePanel.add(l2);
    somePanel.add(yField);
    somePanel.add(moveButton);

    class MyListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    int x = Integer.parseInt(xField.getText());
    int y = Integer.parseInt(yField.getText());
    box.setLocation(x,y);
    repaint();
    }
    }

    ActionListener listener = new MyListener();
    moveButton.addActionListener(listener);

    public void paintComponent(Graphics g)// <---- error: Illegal start of expression
    {
    super.paintComponent(g);
    g.draw(box);
    }


    frame.getContentPane().add(somePanel);
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    private static final int FRAME_WIDTH = 900;
    private static final int FRAME_HEIGHT = 500;
    }[/code]


    thanks,

    DFC
Sign In or Register to comment.