brownie124
Programmer
Hi,
I am very new to Java and am trying to do something that seems simple. I am drawing a rectangle and then attempting to put some components (Checkboxes, Lists, etc.) in the rectangle (NOTE: I cannot use the swing package so this is all AWT and Graphics).
I am having all kinds of problems with this. I am able to put one or the other up, but not both the rectangle and the components. Also, I am trying to setLocation() of my component and that isn't working at all. Here is the code:
Any help or guidance would be very much appreciated.
Thanks,
- Michael
I am very new to Java and am trying to do something that seems simple. I am drawing a rectangle and then attempting to put some components (Checkboxes, Lists, etc.) in the rectangle (NOTE: I cannot use the swing package so this is all AWT and Graphics).
I am having all kinds of problems with this. I am able to put one or the other up, but not both the rectangle and the components. Also, I am trying to setLocation() of my component and that isn't working at all. Here is the code:
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Test1
{
public static void main(String [] args)
{
try
{
AppWindow appwin = new AppWindow();
appwin.setSize(new Dimension(appwin.WIDTH, appwin.HEIGHT));
appwin.setBackground(Color.lightGray);
appwin.setTitle("Test");
appwin.center();
appwin.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
} // End Test1 Class
class AppWindow extends Frame
{
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
public static final String BOXCAPTION = "Show:";
public AppWindow()
{
addWindowListener (
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
dispose();
System.exit(0);
}
}
);
}
public void paint(Graphics g)
{
// Create our group box.
GroupBox gb = new GroupBox(15, 35, WIDTH - 15, 105, g, BOXCAPTION);
createCheckBoxes();
}
public void createCheckBoxes()
{
// Add our check boxes to the group box.
Checkbox cb1 = new Checkbox("Check box 1", true);
Checkbox cb2 = new Checkbox("Check box 2", false);
cb1.setLocation(80, 100);
cb2.setLocation(80, 160);
add(cb1);
add(cb2);
cb1.setVisible(true);
cb2.setVisible(true);
}
public void center()
{
Dimension dim = getToolkit().getScreenSize();
Rectangle abounds = getBounds();
setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2);
}
} // End AppWindow Class
class GroupBox
{
public GroupBox(int x, int y, int intWidth, int intHeight, Graphics g)
{
this(x, y, intWidth, intHeight, g, "");
}
public GroupBox(int x, int y, int intWidth, int intHeight, Graphics g, String strCaption)
{
FontMetrics fm = g.getFontMetrics();
int intSpace = 0;
g.drawLine(x, y, x, intHeight); // Left side of box.
g.drawLine(x, y, 23, y); // Top left part of box -- before caption.
if (strCaption.length() > 0)
{
g.drawString(strCaption, x + 10, y + 5); // Caption.
intSpace = 2;
}
g.drawLine(23 + fm.stringWidth(strCaption) + intSpace, y, intWidth, y); // Top right part of the box -- after caption.
g.drawLine(intWidth, y, intWidth, intHeight); // Right side of box.
g.drawLine(x, intHeight, intWidth, intHeight); // Draw bottom of box.
}
} // End GroupBox Class
Any help or guidance would be very much appreciated.
Thanks,
- Michael