package org.parabot.dane.example;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class JFrameExample extends JFrame implements ActionListener {
JPanel panel;
JTextField field;
JButton button;
JLabel label;
JTabbedPane pane;
JPanel[] panePanels;
public static void main(String[] arguments) {
new JFrameExample();
}
public JFrameExample() {
// Self explanatory
this.setTitle("Example");
this.setResizable(false);
// Set the close operation to dispose so we don't have to use a
// WindowListener for the windowClosing event.
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Now we're going to add components to the frame. It's important you do
// this before you pack so the pack method works properly.
this.panel = new JPanel();
// The reason why the size of this panel is important is because it will
// be the exact size of the inside of the frame.
// For instance, if you set the preferred size of the frame itself to
// 256, 256; the actual space inside the frame for contents will be less
// due to the frames insets. (The surrounding frame/ui of the frame)
this.panel.setPreferredSize(new Dimension(256, 256));
// We're setting the layout to null so we have free control of where
// components go within it. (It's just easier that way in my opinion.)
this.panel.setLayout(null);
// It's time to add components to the JPanel.
this.field = new JTextField("I'm a field!");
// For inner components, we only need setBounds because it's much
// smaller than what you'd usually do, and there's no layout anyway so
// we can freely position and size components.
this.field.setBounds(4, 4, 248, 24);
// We've setup the field, now we're just going to add it to the JPanel.
this.panel.add(this.field);
this.label = new JLabel("Click it. ->");
this.label.setBounds(106, 32, 96, 24);
this.panel.add(this.label);
this.button = new JButton("I'm a button!");
this.button.setToolTipText("You should click me.");
this.button.setBounds(256 - 100, 32, 96, 24);
// Now that we've added two useless things, we can go ahead and show you
// how to use a JTabbedPane.
// JTabbedPane's are pretty useful if you want to put categorized stuff
// into a singular space.
this.pane = new JTabbedPane();
this.pane.setBounds(4, 64, 248, 256 - 68);
// We're going to add a bunch of blank tabs. The null parameter can be
// supplied a Component, such as a JPanel. That should be a good enough
// explanation for you.
this.pane.addTab("Tab 1", null);
this.pane.addTab("Tab 2", null);
this.pane.addTab("Tab 3", null);
this.panel.add(this.pane);
// This button is going to have an action when you click it, so we'll
// add an action listener to it and give it a command name.
// We can use 'this' as the action listener, since it implements
// ActionListener.
this.button.addActionListener(this);
// By default, the action command is the same as the caption. so it
// would've been "I'm a button!" if we don't change it.
// These should always be unique unless you want more than one thing
// doing the same action.
this.button.setActionCommand("ButtonAction");
this.panel.add(this.button);
// Setting the location of the panel isn't nessecary because it's going
// to be the main content panel anyway.
// So we just add it to the JFrame.
this.add(panel);
// Pack the frame to fit the window to its contants, rather than give
// the window a specific size.
this.pack();
// Center the frame on the screen.
// It's important we do this after we pack because then we'll have a
// width and size to go off of. (e.g; getWidth()/getHeight())
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screen.width - this.getWidth()) / 2, (screen.height - this.getHeight()) / 2);
// If you don't want it centered, you could always set locatoin by
// platform.
// this.setLocationByPlatform(true);
// Since we're all done, show the window.
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
// I like to use switch statements for my ActionListeners because I
// usually have a fairly abundant amount of commands.
// Switch statements are recommended when there are 3 or more actual
// values it could have. So it's not always neccesary.
switch (command) {
case "ButtonAction": {
JOptionPane.showMessageDialog(this, "Field's Text: "" + this.field.getText() + '"', "Message", JOptionPane.WARNING_MESSAGE, null);
break;
}
default: {
System.out.println("Unknown action command: " + command + '.');
}
}
}
@Override
public void dispose() {
if (JOptionPane.showConfirmDialog(this, "Are you sure you'd like to close this window?", "Select an Option", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null) == JOptionPane.OK_OPTION) {
// INFO: You could do extra stuff here, in-case you plan on having
// things happen when the window is closed.
super.dispose();
}
}
private static final long serialVersionUID = 1577442436026299722L;
}