View RSS Feed

fatabass

A simple GUI code

Rating: 2 votes, 1.00 average.
by , 12-18-2011 at 01:25 PM (1758 Views)
Java Code:
package myPackage;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.*;
@SuppressWarnings("serial")
public class SamsLearning extends JFrame {
	public SamsLearning() {
		super();
		this.setTitle("My Title");
		this.setLookAndFeel();
		this.setSize(500,500);
		MyButtonClass myButton = new MyButtonClass("Play");
		MyButtonClass myButton2 = new MyButtonClass("Stop");
		MyJLabelClass myLabel = new MyJLabelClass();
		myLabel.setText("This is a label!");
		JLabel heyo = new JLabel("heyp", JLabel.LEFT);
		JCheckBox jumboSize = new JCheckBox("Jumbo Size",false);
		JCheckBox mercedes = new JCheckBox("Mercedes");
		JCheckBox bmw = new JCheckBox("Bmw");
		JCheckBox audi = new JCheckBox("Audi");
		ButtonGroup carbrands = new ButtonGroup();
		carbrands.add(mercedes);
		carbrands.add(bmw);
		carbrands.add(audi);
		FlowLayout flo = new FlowLayout();
		this.setLayout(flo);
		JPanel topRow = new JPanel();
		topRow.setName("No name");
		this.add(topRow);
		topRow.add(mercedes);
		topRow.add(bmw);
		topRow.add(audi);
		JTextArea comments = new JTextArea("Hello\nHow are you?",8,40);
		this.add(comments);
		this.add(myButton);
		this.add(myButton2);
		this.add(myLabel);
		this.add(heyo);
		this.add(jumboSize);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}//end constructor
	private void setLookAndFeel() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
			);
		} catch(Exception exc) {
			System.out.println("Somethinw went wrong. Using the default LookAndFeel..");
		}
	}
}//end class SamsLearning
Java Code:
package myPackage;
public class SamsLearningTest {
	public static void main(String[] args) {
		SamsLearning myLearning = new SamsLearning();
	}
}
Name:  1.png
Views: 230
Size:  11.6 KB

Submit "A simple GUI code" to Facebook Submit "A simple GUI code" to Digg Submit "A simple GUI code" to del.icio.us Submit "A simple GUI code" to StumbleUpon Submit "A simple GUI code" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. DarrylBurke's Avatar
    • |
    • permalink
    It's a good example of poor coding practices.

    -- Misuse of inheritance (extends JFrame)
    -- Uses a Sun proprietary class com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
    -- Swing components constructed on the main Thread and not on the EDT
    -- Inane comments like //end class SamsLearning
    -- No vertical whitespace (see Code Conventions for the Java(TM) Programming Language: Contents )

    db
    JavaForums likes this.
  2. fatabass's Avatar
    • |
    • permalink
    Thanks for the feedback.
  3. fatabass's Avatar
    • |
    • permalink
    Why is extending JFrame a bad practice ?
  4. fatabass's Avatar
    • |
    • permalink
    What is EDT ?
  5. DarrylBurke's Avatar
    • |
    • permalink
    Quote Originally Posted by fatabass
    Why is extending JFrame a bad practice ?
    Because you unnecessarily expose the JFrame API to users of your class.

    Should another class that uses your class be allowed to (for example) set the title of your application? Should it be allowed to change the content and menus?

    By not extending JFrame but rather constructing a JFrame, adding the components and setting it visible, any other class can't easily* do any of that.

    db

    *Frame.getFrames() or Window.getWindows() can be used to obtain a reference to the JFrame created by your application.
  6. DarrylBurke's Avatar
    • |
    • permalink