aspose file tools*
The moose likes Beginning Java and the fly likes Getting Swing to Work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting Swing to Work" Watch "Getting Swing to Work" New topic
Author

Getting Swing to Work

Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
Other than the fact that I am just learning Java and am yet to figure out what I am doing, can you tell me why the following does not put the panel and button on the frame? I get no compiler errors when I run this but all that displays is the frame. This project is just for learning's sake so I am not desperate but before I crush my screen, I would be obliged if someone could assist me (and I don't like replacing screens).

Thanks so much!

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

public class MainScreen extends JFrame{

public static void main (String [] args) {

MainScreen rounds = new MainScreen();
rounds.mainPanel();


}

public void mainPanel(){

JFrame theScreen = new JFrame();
theScreen.setTitle("SM O2 Rounds");
theScreen.setSize(1000,600);
theScreen.setDefaultCloseOperation(EXIT_ON_CLOSE);
theScreen.setVisible(true);

JPanel panel1 = new JPanel(); //Create a panel to add to the frame
JButton button1 = new JButton("Submit"); //Create a button to add to a panel
panel1.add(button1); //Add the button to the panel
JLabel label1 = new JLabel("Login"); //Create label to add to panel
panel1.add(label1); //Add label to panel

//Add panel components to frame
theScreen.getContentPane().add(BorderLayout.CENTER, panel1);



}


}
[ May 21, 2005: Message edited by: Al Wells ]
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
BTW, I failed to copy all of the imports. I do have import javax.swing.*; in the first line. The awt and awt.event imports are for other functions that I will be attempting once I can get this one to work.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
make your setVisible last thing


java amateur
Al Wells
Ranch Hand

Joined: Apr 18, 2005
Posts: 87
Now wasn't that worth looking through 10 books and hours of frustation? Thanks Miguel.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
that's what forums are for...
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
For future reference, we have a forum that specializes in GUI programming. You may want to post similar questions there in the future. Of course, you should feel free to come back here with any general Java questions you have.

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
as an aside, please format your code with [CODE) [/CODE], using the CODE button (bellow Instant UBB Code), when posting... code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Swing to Work