Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem running GUI in "Java Application" mode Posted by DavefromCanada on 21 Oct 2006 at 11:51 AM
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:

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;
}



thanks,

DFC
Report
Re: Problem running GUI in "Java Application" mode Posted by pah on 23 Oct 2006 at 5:05 AM
: 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:
:
:
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.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;
: }

:
:
: thanks,
:
: DFC
:

Report
Re: Problem running GUI in "Java Application" mode Posted by DavefromCanada on 23 Oct 2006 at 6:01 PM
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:

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;
}



thanks,

DFC



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.