torsdag 1 januari 2009

GUIdemo - hela programmet

import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

public class GUIdemo {
// Create buttons, panels etc
//
private GUIdemo() {
//Create and set up the window.
JFrame frame = new JFrame("GUIdemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create panels
JPanel mp = new JPanel(); // Main panel
JPanel bp = new JPanel(); // Button panel
JPanel np = new JPanel(); // North Panel
mp.setLayout(new BorderLayout());
bp.setLayout(new FlowLayout());
np.setLayout(new FlowLayout());
np.setBorder(new TitledBorder("North Panel"));
bp.setBorder(new TitledBorder("Buttonpanel"));
mp.setBorder(new TitledBorder("Main Panel"));

// Create buttons to button panel
bp.add(new JButton("Panelbutton1"));
bp.add(new JButton("Panelbutton2"));

mp.add(bp, BorderLayout.SOUTH);

// Add stuff to north panel
np.add(new JLabel("JLabel"));
np.add(new JButton("JButton"));

// Create Choice-list and add to np.
Choice ch = new Choice();
ch.add(new String("Choice"));
ch.addItem(new String("-list"));
np.add(ch);

// Create textarea
JTextArea ta = new JTextArea(10, 20);
ta.append("JTextArea\ninside a JScrollPane");
ta.setLineWrap(true);

JScrollPane scrPane = new JScrollPane(ta);
scrPane.setBorder(new TitledBorder("JScrollPane"));

// Add panels and labels to main panel
mp.add(np, BorderLayout.NORTH);
mp.add(bp, BorderLayout.SOUTH);
mp.add(new JLabel("East"), BorderLayout.EAST);
mp.add(new JLabel("West"), BorderLayout.WEST);
mp.add(scrPane, BorderLayout.CENTER);


// Add main panel to mainframe
frame.getContentPane().add(mp);

// Tweak the Frame
frame.setMinimumSize(new Dimension(300, 300));
frame.setLocation(400, 400);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUIdemo g = new GUIdemo();
}
});
}
}

Inga kommentarer:

Skicka en kommentar