Java Reference
In-Depth Information
import java.awt.*;
import javax.swing.*;
public class JDialogDemo extends JDialog
{
private JButton ok, cancel;
public JDialogDemo(String title)
{
this.setTitle(title);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
JLabel message = new JLabel(“Continue?”);
message.setBounds(70, 20, 125, 20);
ok = new JButton(“OK”);
ok.setBounds(15,50, 60, 20);
cancel = new JButton(“Cancel”);
cancel.setBounds(90, 50, 80, 20);
contentPane.add(message);
contentPane.add(ok);
contentPane.add(cancel);
}
public static void main(String [] args)
{
JDialog f = new JDialogDemo(“JDialogDemo”);
f.setSize(200,100);
f.setResizable(false);
f.setVisible(true);
}
}
Lab 12.1: Using JFrame
The purpose of this lab is to become familiar with creating a JFrame.
1. Write a class named Calculator that extends JFrame.
2. Within the constructor of the Calculator class, use panels and layout
managers to create a GUI similar to the one shown in Figure 12.13.
Note that the class uses JButton for the buttons and JTextField for
the display above the buttons.
3. Add a main() method to your Calculator class that instantiates and
displays your Calculator GUI.
4. Save, compile, and run the Calculator class.
Your Calculator should be similar to the one in Figure 12.13. Do not
worry if it is not exactly the same, especially because we have not dis-
cussed the details of the various components yet.
Search WWH ::




Custom Search