• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting Swing to Work

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make your setVisible last thing
 
Al Wells
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's what forums are for...
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic