12-18-2013 GUI Bits...

posted Dec 19, 2013, 8:17 AM by Samuel Konstantinovich   [ updated Dec 19, 2013, 8:26 AM ]
Demo 1:   No content... basic window

import javax.swing.*;
public class Gui1 extends JFrame {
    public Gui1() {
        this.setTitle("My first GUI");
        this.setSize(600,400);
        this.setLocation(100,100);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Gui1 g = new Gui1();
        g.setVisible(true);
    }
}

Demo 2: 

import javax.swing.*;
import java.awt.*; //needed for pane

public class Gui2 extends JFrame {
    private Container pane;
    private JButton exitButton,b;
    private JLabel l;
    private JTextField text;
    private Container buttons;
    private JCheckBox box;

    public Gui2() {
this.setTitle("My first GUI");
this.setSize(600,400);
this.setLocation(100,100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
pane = this.getContentPane();
pane.setLayout(new FlowLayout());
//pane.setLayout(new GridLayout(4,0));

l = new JLabel("This is a label",null,JLabel.CENTER);
exitButton = new JButton("Exit");
b = new JButton("doSomething");
text = new JTextField(40);

/*make some buttons in a container to organize them*/
buttons = new Container();
buttons.setLayout(new FlowLayout());
//buttons.setLayout(new GridLayout(0,3));
buttons.add(exitButton);
buttons.add(b);
box = new JCheckBox("upcase");
buttons.add(box);

//Add things to your pane
pane.add(buttons);
pane.add(l);
pane.add(text);
    }

    public static void main(String[] args) {
Gui2 g = new Gui2();
g.setVisible(true);
    }

}

Version 3: ( For 12/19/2013 class)
import javax.swing.*;
import java.awt.*; //needed for pane

public class GuiDoStuff extends JFrame {
    private Container background,buttons;
    private JButton exitButton,doStuffButton;
    private JLabel L1,L2;
    private JTextField text;
    private JCheckBox box;

    public GuiDoStuff() {
setTitle("GUI that does stuff demo:");
setSize(600,400);
setLocation(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
        
background = this.getContentPane();
background.setLayout(new GridLayout(3,1));
L1 = new JLabel("Text to process:");
L2 = new JLabel("Results:");


exitButton = new JButton("Exit");
doStuffButton = new JButton("Process");

text = new JTextField(40);

/*make some buttons in a container to organize them*/
buttons = new Container();
buttons.setLayout(new GridLayout(1,3));
buttons.add(doStuffButton);
buttons.add(exitButton);

//Add things to your pane
background.add(buttons);

Container c = new Container();
c.setLayout(new FlowLayout() );
c.add(L1);
c.add(text);
background.add(c);
box = new JCheckBox("upcase");
c.add(box);
background.add(L2);
    }

    public static void main(String[] args) {
GuiDoStuff g = new GuiDoStuff();
g.setVisible(true);
    }

}






Comments