Border problems
From: JS (dsa._at_asdf.com)
Date: 11/29/04
- Next message: Andrew Thompson: "Re: Border problems"
- Previous message: Thomas Weidenfeller: "Re: Swing scrolling"
- Next in thread: Andrew Thompson: "Re: Border problems"
- Reply: Andrew Thompson: "Re: Border problems"
- Reply: Thomas Weidenfeller: "Re: Border problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 29 Nov 2004 13:55:48 +0100
I am trying to make some borders in my graphic window but I get this error:
IllegalArgumentException: adding a window to a container
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at MyWriter2.<init>(ConvertATemperature.java:22)
at CelsiusToFahrenheit2.main(ConvertATemperature.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
>
Here is the code:
import java.awt.*;
import javax.swing.*;
import java.text.*;
class MyWriter2 extends JFrame
{ private int width;
private int height;
private String sentence = "";
private int x_position = 50;
private int y_position = 80;
public MyWriter2(int w, int h)
{ width = w;
height = h;
x_position = width / 5;
y_position = height / 2;
JFrame my_frame = new JFrame();
my_frame.getContentPane().add(this);
my_frame.setTitle("MyWriter");
my_frame.setSize(width, height);
my_frame.setVisible(true);
}
public void paintComponent(Graphics g)
{ makeBorder(g);
g.setColor(Color.red);
g.drawString(sentence, x_position, y_position);
}
private void makeBorder(Graphics pen)
{ pen.setColor(Color.blue);
pen.fillRect(0, 0, width, height);
int border_size = 20;
int center_rectangle_width = width - (2 * border_size);
int center_rectangle_height = height - (2 * border_size);
pen.setColor(Color.white);
pen.fillRect(border_size, border_size,
center_rectangle_width, center_rectangle_height);
}
public void writeSentence(String s)
{ sentence = s;
this.repaint();
}
public void repositionSentence(int new_x, int new_y)
{ x_position = new_x;
y_position = new_y;
this.writeSentence(sentence);
}
}
class CelsiusToFahrenheit2
{ public static void main(String[] args)
{ MyWriter2 writer = new MyWriter2(300, 200);
String s =
JOptionPane.showInputDialog("Type an integer Celsius temperature:");
int c = new Integer(s).intValue();
double f = ((9.0 / 5.0) * c) + 32;
DecimalFormat formatter = new DecimalFormat("0.0");
String h = formatter.format(f);
writer.writeSentence("Degrees Fahrenheit = " + h);
}
}
Hope someone can help
JS
-- Mvh Johs
- Next message: Andrew Thompson: "Re: Border problems"
- Previous message: Thomas Weidenfeller: "Re: Swing scrolling"
- Next in thread: Andrew Thompson: "Re: Border problems"
- Reply: Andrew Thompson: "Re: Border problems"
- Reply: Thomas Weidenfeller: "Re: Border problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|