Re: Help needed with GUI programing
porky008 wrote:
I need to add a next, previous, first and last button and can not
figure it out. Can some one help me out on this? Also is there some
place that may have something like templates for GUI programming?
Producing a GUI is not a trivial task.
There is a good tutorial at Sun's website:
http://java.sun.com/docs/books/tutorial/uiswing/
You could do something like this as a start
import java.awt.BorderLayout;
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.JPanel;
import javax.swing.SwingUtilities;
public class SimpleGUI extends JPanel implements ActionListener {
JLabel aLabel;
SimpleGUI() {
JPanel buttonPanel = new JPanel();
JButton nextButton = new JButton("Next");
nextButton.addActionListener(this);
buttonPanel.add(nextButton);
JButton previousButton = new JButton("Previous");
previousButton.addActionListener(this);
buttonPanel.add(previousButton);
JButton firstButton = new JButton("First");
firstButton.addActionListener(this);
buttonPanel.add(firstButton);
JButton lastButton = new JButton("Last");
lastButton.addActionListener(this);
buttonPanel.add(lastButton);
JPanel mainPanel = new JPanel();
aLabel = new JLabel("Example");
mainPanel.add(aLabel);
setLayout(new BorderLayout());
add(mainPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("Next".equals(command)) doNext();
else if ("Previous".equals(command)) doPrevious();
else if ("First".equals(command)) doFirst();
else if ("Last".equals(command)) doLast();
aLabel.setText(command);
}
private void doNext() {}
private void doPrevious() {}
private void doFirst() {}
private void doLast() {}
static void createAndShowGUI() {
JFrame frame = new JFrame("Simple GUI Example");
frame.add(new SimpleGUI());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}