Re: setting size of elements



On Sat, 27 Aug 2005 17:52:49 -0400, Frances wrote:
....
> basically I want a swing version of this:
>
> http://www.francesdelrio.com/java/win.html
> (code: http://www.francesdelrio.com/java/win.java)

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Win extends JApplet implements ActionListener {
JTextArea ta;
JTextArea ta2;
JButton btn;
FlowLayout layout = new FlowLayout(FlowLayout.LEFT);

public void init() {

ta = new JTextArea("", 14, 35);
ta2 = new JTextArea("", 6, 35);
btn = new JButton("SEND");
//ta.setSize(430,80);
//ta2.setSize(430,50);
Container c = getContentPane();
c.setLayout(layout);
c.add(new JScrollPane(ta, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER ));
c.add(new JScrollPane(ta2, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
c.add(btn);
//btn.setSize(80,20);
btn.addActionListener(this);
}


public void sendMsg() {
String msg = ta2.getText();
//System.out.println(msg);
ta.append(msg + "\n");
ta2.setText("");
// ta.transferFocus();
ta2.requestFocus();
}

public void actionPerformed(ActionEvent ae) {
sendMsg();
}

}
</sscce>

> with the JTextAreas in a JSplitPane..

Put the JScrollPanes in a JSplitPane.

> ...and with JTextAreas exact sizes

Not practical. Java Layouts are intended to give your
UI a logical layout across a variety of PLAFS (including
the tweaks that Sun might do to the default PLAF's
from release to release) using a number of screen
resolutions/ fonts...

Java GUI design is not geared towards giving pixel perfect
representations of UI components, but instead presenting them
at the exact size the end user requires.

Your attempts to create pixel perfect layouts are a
pointless waste of time, and they may be what a developer
wants, but they are rarely what the end user wants.

> when I say I don't understand something it's not b/c I'm too lazy to
> look it up (if you're lazy you have no business trying to become a
> programmer..) but b/c I don't understand some of the explanations..

You should stop at each one and ask. You end up
a) wasting too much time trying to force Java layouts to
fit your misconceptions.
b) Tying yourself up in 'code knots'.

> here..
> http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html#contentpane
>
> it says:
> "Here's the code that the preceding example uses to get a frame's
> content pane and add the yellow label to it:
>
> frame.getContentPane().add(yellowLabel, BorderLayout.CENTER); "
>
> 'frame' refers to what exactly? JApplet? or do I create a JFrame? a
> JRootPane?

??? What would make you think that example that mentions 'frame'
would be referring to an 'applet'?

The process of adding things to a Swing content pane is the
same for both, but I thought that would have been pretty clear
they were referring to a JFrame in that example. (and that
is based purely on the snippets you quoted!)

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"Watch TV and drink your beer."
Charles Manson 'Sick City'
.


Quantcast