Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Swing to Work

 
Al Wells
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make your setVisible last thing
 
Al Wells
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now wasn't that worth looking through 10 books and hours of frustation? Thanks Miguel.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's what forums are for...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as an aside, please format your code with [CODE) [/CODE], using the CODE button (bellow Instant UBB Code), when posting... code
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic