PDA

View Full Version : my first working swing program


inkedmn
07-06-2002, 05:00 PM
it's pretty basic, just opens a dialog with one button that opens a color chooser which allows you to change the button's color...

w00t!


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GuiAttempt extends JPanel{

private JButton button1 = new JButton("Colors");

public GuiAttempt(){
buildUI();
buildListeners();
}

private void buildUI() {
this.setLayout(new GridLayout(1,1));
add(button1);
}

private void buildListeners() {
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doStuff();
}
});
}

private void doStuff() {
Color pickColor = button1.getBackground();
pickColor = JColorChooser.showDialog(this,"Pick a color", pickColor);
button1.setBackground(pickColor);
}

public static void main(String [] args) {
int myHeight = 150;
int myWidth = 150;
JFrame theFrame = new JFrame("Color Chooser");
GuiAttempt thePanel = new GuiAttempt();
Container contentPane = theFrame.getContentPane();
contentPane.add(thePanel);
theFrame.setSize(150, 150);
Toolkit toolkit = theFrame.getToolkit();
Dimension screenSize = toolkit.getScreenSize();
Dimension frameSize = theFrame.getSize();
int newX = (int)((screenSize.width / 2) - (frameSize.width / 2));
int newY = (int)((screenSize.height / 2) - (frameSize.height / 2));
theFrame.setLocation(newX, newY);
theFrame.setVisible(true);

theFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

Dru Lee Parsec
07-06-2002, 05:43 PM
Dude! You rock!
;)

Strike
07-06-2002, 05:44 PM
nice
(bitch)

recluse
07-06-2002, 08:12 PM
What's a swing program?

sicarius
07-06-2002, 10:46 PM
Swing is a library of Java classes used to make GUIs. There are two major GUI libraries in Java, AWT, and Swing. AWT is the original package and it was designed with the notion that a GUI in Java should resemble all other GUIs on a native system. The Swing library operates on the notion that since Java is an OS idependant language, all GUIs written in Java should look the same, regardless of what platform it runs on. Over all I think Swing is the most popular and provides better facilities than AWT. AWT does still hold its own though.

Dru Lee Parsec
07-08-2002, 12:18 PM
The Swing library operates on the notion that since Java is an OS idependant language, all GUIs written in Java should look the same, regardless of what platform it runs on.

You actually have a choice on that topic. The main Swing look & feel (formerly called the "metal" L&F) is the default L&F. However, it's not too difficult to switch the L&F to a Microsoft Windows L&F when on an M$ OS, or to a Motif L&F on just about any OS, or even to the Macintosh L&F on a Mac.

You can write code in AWT, but the Swing components have a better thought out API and they look nicer (There's also the concept of lightweight vs. heavy weight components which I won't get into here)

So you're right, Swing does have a standard look across all OS's, but you're not stuck with that look. I've even seen Java programs that are theme-able. I use to run Jext in the "Aqua" theme to make it look like a Mac OSX app running in Linux. :)

inkedmn
07-08-2002, 12:28 PM
Dru-

can you give me some ways to expand on this program? (i.e., give me some other functionality to add to it :))?