Hello
I have some Java code which I would like to develop, but I am not sure how to add a label or a submit button.
To the following code, why can't I simply add:
JButton button = new JButton ("Click to open" after the:
JButton button = new JButton ("Click to close"????
Why won't this line add another button?
I would appreciate any help.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingFrame
{
public static void main (String args[])
throws Exception
{
JFrame frame = new JFrame("Swing Demo"
JLabel label = new JLabel("Swing Version"
JButton button = new JButton ("Click to close"
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
});
// Get content pane
Container pane = frame.getContentPane();
// Set layout manager
pane.setLayout( new FlowLayout() );
// Add to pane
pane.add( label );
pane.add( button );
frame.pack();
// Center the frame
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension scrnsize = toolkit.getScreenSize();
// Get the frame size
Dimension framesize= frame.getSize();
// Set X,Y location
frame.setLocation ( (int) (scrnsize.getWidth()
- frame.getWidth() ) / 2 ,
(int) (scrnsize.getHeight()
- frame.getHeight()) / 2);
frame.setVisible(true);
}
}
I have some Java code which I would like to develop, but I am not sure how to add a label or a submit button.
To the following code, why can't I simply add:
JButton button = new JButton ("Click to open" after the:
JButton button = new JButton ("Click to close"????
Why won't this line add another button?
I would appreciate any help.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingFrame
{
public static void main (String args[])
throws Exception
{
JFrame frame = new JFrame("Swing Demo"
JLabel label = new JLabel("Swing Version"
JButton button = new JButton ("Click to close"
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
});
// Get content pane
Container pane = frame.getContentPane();
// Set layout manager
pane.setLayout( new FlowLayout() );
// Add to pane
pane.add( label );
pane.add( button );
frame.pack();
// Center the frame
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension scrnsize = toolkit.getScreenSize();
// Get the frame size
Dimension framesize= frame.getSize();
// Set X,Y location
frame.setLocation ( (int) (scrnsize.getWidth()
- frame.getWidth() ) / 2 ,
(int) (scrnsize.getHeight()
- frame.getHeight()) / 2);
frame.setVisible(true);
}
}