main: import javax.swing.*; rectangle: W Writer ign in Rulkr Eye Pratechon tocha
ID: 3857797 • Letter: M
Question
main:
import javax.swing.*;
rectangle:
W Writer ign in Rulkr Eye Pratechon tochan Mode New Windcw Cl Synchranaus s Screen L-you: Mep Al t' rraMSF-NFORMAT ON-60-2-517[1) (1].dnrx × E norurnant3 E213 513.d New Dument Recent documents Original configuration, and while mouse button is not held down: L860.212 S17.dac 1 couRSE NFORMATION-00212517m nl Resizable rectangle k, clarMither.ince Open. New Blank Ucounnere Nw trem Orlne Templa New from Deta.it Templae New from Cther Templetes PaQE Nun: pagR 1/2 S.chcn: 1/1 | Rcw:29 Calurm:681 words: 148/229 1:11 AM 41 ENG 2011-07-11Explanation / Answer
import java.awt.*;
import javax.swing.*;
// A Swing application inherits from top-level container javax.swing.JFrame
public class TestDisplayAreaAndBorder extends JFrame {
/** Constructor to setup the GUI */
public TestDisplayAreaAndBorder() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
JTextArea comp = new JTextArea(10, 25); // row and columns
comp.setBackground(new Color(200, 200, 200));
comp.setForeground(Color.BLUE);
comp.setBorder(BorderFactory.createLineBorder(Color.CYAN, 15));
// set border to line-border with the given color and thickness
comp.setPreferredSize(new Dimension(350, 200));
cp.add(comp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Appearance and Border");
setSize(400, 300);
setVisible(true);
StringBuffer msg = new StringBuffer();
msg.append("Width = " + comp.getWidth());
msg.append(" Height = " + comp.getHeight());
msg.append(" Origin-X = " + comp.getX());
msg.append(" Origin-Y = " + comp.getY());
msg.append(" Origin-X (on screen) = " + comp.getLocationOnScreen().x);
msg.append(" Origin-Y (on screen) = " + comp.getLocationOnScreen().y);
Insets insets = comp.getInsets();
msg.append(" Insets (top, right, bottom, left) = "
+ insets.top + "," + insets.right + "," + insets.bottom + "," + insets.left);
msg.append(" Real Width = " + (comp.getWidth() - insets.left - insets.right));
msg.append(" Real Height = " + (comp.getHeight() - insets.top - insets.bottom));
comp.setText(msg.toString());
}
/** The entry main() method */
public static void main(String[] args) {
// Run the GUI codes on Event-Dispatching thread for thread safety
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestDisplayAreaAndBorder(); // Let the constructor do the job
}
});
}
// Set methods (in java.awt.Window)
// (x, y) specifies the origin (top-left corner) of the window on the screen
public void setSize(int width, int height)
public void setLocation(int x, int y)
public void setBounds(int x, int y, int width, int height)
public void setSize(Dimension dim)
public void setLocation(Point origin)
public void setBounds(Rectangle r) // JDK 1.6
// The associated get methods (in java.awt.Component) are:
public int getWidth()
public int getHeight()
public int getX()
public int getY()
public Dimension getSize()
public Point getLocation()
public Rectangle getBounds()
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.