File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes gui Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "gui" Watch "gui" New topic


tobias berenson

Joined: Jul 07, 2009
Posts: 3
So im learning how to make a GUI, and it isnt working out. Ive taken a comp sci ap course and we didnt get this far and im lost. when i create a new Button() and choose getAnswer() it creates a panel with no buttons, does nothing. i know im missing what i need but i dont get it.

help is appreciated.

thanks in advance.

import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Button extends JFrame implements ActionListener{
int answer;
private Frame aFrame=new Frame("Calc V .01");
private JButton button=new JButton("2+2+n=???");
private JLabel count=new JLabel ("Answer= "+answer);
private JPanel panel = new JPanel();

public static void main(String args[]){

public void getAnswer(){

FlowLayout flow=new FlowLayout();
Button b=new Button();

public void actionPerformed(ActionEvent e){
count.setText("Answer is "+answer);
Ulises Pulido
Ranch Hand

Joined: Jul 24, 2008
Posts: 81
Well to solve the problem of the GUI not showing the buttons.

create a constructor

add the label and the button to the panel

once you do that add the panel to the frame

usually you need to pack everything and set visible the frame.

you don't need the Frame aFrame instance because you are already extending the JFrame class.

I don't know if it will work with that only but if it is still not showing the JFrame in the main method add

And let me know if it worked

SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCJD, SCEA in progress
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46429
Welcome to JavaRanch
I shall move you to the Swing forum, where that sort of question is usually discussed.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46429
By the way: there is already a Button class in the java.awt package, so you can cause confusion by your choice of class names.
pete stein

Joined: Feb 23, 2007
Posts: 1561
tobias berenson wrote:i know im missing what i need but i dont get it.

what do you think this is going to do?

Regardless if it's a GUI or non-GUI program, and empty main method means that your program will do nothing, no matter how much code the class has in it.

If this were my app, I'd do something like so:
tobias berenson

Joined: Jul 07, 2009
Posts: 3
oops, i didnt mean to take the main out, thank your for that. i was trying to do getAnswer() in the main but got an error about a non static method not being able to be referenced in a static method.

thank you though, it is what i was looking for, i have given credit where i have used your code (not all).
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10836

Welcome to the Ranch.

In future, while posting code, please use code tags.
To get the most out of the Ranch, it might be a good idea to go through some suggestions listed here

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: gui
jQuery in Action, 3rd edition