• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

gui

 
tobias berenson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
panel.setLayout(flow);
panel.setVisible(true);
Button b=new Button();
b.setSize(300,500);
b.setVisible(true);
panel.add(button);
panel.add(count);
button.addActionListener(this);
}

public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
answer+=4;
count.setText("Answer is "+answer);
}
}
}
 
Ulises Pulido
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
I shall move you to the Swing forum, where that sort of question is usually discussed.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias,
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic