Hi all,
I want to be able to let the user press a button and then either open a new JFrame or change the current one. The following code is taken from this java website, however the changes i made don't seem to work (seperated by //@@@@@@@@@@@@@@) when I run this example the window craps out instead of changing the buttons. Any help would be much appreciated
Cheers
Drb2k2
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
/*
* ButtonDemo.java is a 1.4 application that requires the following files:
* images/right.gif
* images/middle.gif
* images/left.gif
*/
public class main extends JPanel implements ActionListener
{
protected JButton b1, b2, b3;
public main()
{
b1 = new JButton("New Buttons");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_C);
b1.setActionCommand("New Buttons");
b2 = new JButton("ASFDJASDFKJ");
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
b2.setMnemonic(KeyEvent.VK_M);
b2.setActionCommand("Custom");
b3 = new JButton("USADFIJSD");
//Use the default text position of CENTER, TRAILING (RIGHT).
b3.setMnemonic(KeyEvent.VK_T);
b3.setActionCommand("Custom");
//b3.setEnabled(true);
//Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b3.addActionListener(this);
b2.addActionListener(this);
b1.setToolTipText("Click this button to open a new window.");
b2.setToolTipText("Click this button to do nothing.");
b3.setToolTipText("Click this button to do nothing.");
//Add Components to this container, using the default FlowLayout.
add(b1);
add(b2);
add(b3);
}
public void actionPerformed(ActionEvent e)
{
if ("New Buttons".equals(e.getActionCommand()))
{
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
secondwin window = new secondwin();
setVisible(false);
window.setVisible(true);
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
b2.setEnabled(false);
b1.setEnabled(false);
b3.setEnabled(true);
}
else
{
b2.setEnabled(true);
b1.setEnabled(true);
b3.setEnabled(false);
}
}
public static void main(String[] args)
{
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
main newContentPane = new main();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
and then
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
/*
* secondwin.java is a 1.4 application that requires the following files:
* images/right.gif
* images/middle.gif
* images/left.gif
*/
public class secondwin extends JPanel
implements ActionListener {
protected JButton b1, b2, b3;
public secondwin() {
b1 = new JButton("Disable middle button");
b1.setVerticalTextPosition(AbstractButton.CENTER);
b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("disable");
b2 = new JButton("Middle button");
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
b2.setMnemonic(KeyEvent.VK_M);
b3 = new JButton("Enable middle button");
//Use the default text position of CENTER, TRAILING (RIGHT).
b3.setMnemonic(KeyEvent.VK_E);
b3.setActionCommand("enable");
b3.setEnabled(false);
//Listen for actions on buttons 1 and 3.
b1.addActionListener(this);
b3.addActionListener(this);
b1.setToolTipText("Click this button to disable the middle button.");
b2.setToolTipText("This middle button does nothing when you click it.");
b3.setToolTipText("Click this button to enable the middle button.");
//Add Components to this container, using the default FlowLayout.
add(b1);
add(b2);
add(b3);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
b2.setEnabled(false);
b1.setEnabled(false);
b3.setEnabled(true);
} else {
b2.setEnabled(true);
b1.setEnabled(true);
b3.setEnabled(false);
}
}
public static void main(String[] args) {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("secondwin");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
secondwin newContentPane = new secondwin();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}