Home > Archive > Java Help > August 2007 > Swing GUI
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Where can I found examples for a gui with a big textarea and some buttons ?
Thanks.
| |
| RedGrittyBrick 2007-08-22, 8:11 am |
| lando wrote:
> Where can I found examples for a gui with a big textarea and some buttons ?
> Thanks.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ABigTextAreaAndSOmeButtons {
// constructor
ABigTextAreaAndSOmeButtons() {
JPanel p = new JPanel();
p.add(new JTextArea(40,80));
p.add(new JButton("This"));
p.add(new JButton("That"));
p.add(new JButton("Other"));
JFrame f = new JFrame("A big text area with some buttons");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ABigTextAreaAndSOmeButtons();
}
});
}
}
| |
| RedGrittyBrick 2007-08-22, 8:11 am |
| RedGrittyBrick wrote:
> lando wrote:
>
>
<snip>
Here's a more useful example, just in case I seem too sarcastic :-)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ABigTextAreaAndSomeButtons implements ActionListener {
private static final String THIS="This", THAT="That", OTHER="Other";
private JTextArea area = new JTextArea(40,80);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ABigTextAreaAndSomeButtons();
}
});
}
ABigTextAreaAndSomeButtons() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(area, BorderLayout.CENTER);
JPanel bp = new JPanel();
bp.add(makeJButton(THIS));
bp.add(makeJButton(THAT));
bp.add(makeJButton(OTHER));
p.add(bp, BorderLayout.SOUTH);
JFrame f = new JFrame("A big text area with some buttons");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setVisible(true);
}
private JButton makeJButton(String label) {
JButton b = new JButton(label);
b.addActionListener(this);
return b;
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
area.append("\n" + command);
if (OTHER.equals(command))
area.append(" **bonus** ");
}
}
| |
|
|
| Andrew Thompson 2007-08-22, 8:11 am |
| RedGrittyBrick wrote:
(buttons)
...[color=darkred]
>Here's a more useful example, ...
I hoped your first example might have begun to make the OP
realize that perhaps they might have gone about asking that
question in a different (and more specific) way.
>...just in case I seem too sarcastic :-)
If they did not get the wider message, perhaps you were
not sarcastic enough. ;-)
To the OP. I read your question and was going to post
a code example like RedGrittyBrick, but since I had no
idea what you wanted in this GUI (and to be quite truthful,
I do not care deeply) I did not even bother to do that.
Further, I do not think that finding a few codes examples
as you stated, will progress your own technical problem
much (which, I am guessing, is to build a UI that has a
large text area and a few buttons).
What will help you a great deal more, in the long run,
is to understand how Java GUI's are construced using
*layouts*. For that knowledge, I highly recommend the
layout section of the Java Tutorial.
<http://java.sun.com/docs/books/tutorial/uiswing/layout/>
<http://java.sun.com/docs/books/tuto...yout/using.html>
Note that the 'Examples Index' linked from the top
of the first page linked above, contains dozens
(from memory) of code samples.
One thing I suspect that is not covered well in those
pages, was demonstrated in RGB's second example,
which uses a 'nested layout'. This is what I call putting
a container with one layout (The JPanel that holds the
buttons gets a FlowLayout, by default) into a larger
container with a (usually different) layout.
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via http://www.javakb.com
| |
| Knute Johnson 2007-08-23, 4:24 am |
| lando wrote:
> Where can I found examples for a gui with a big textarea and some buttons ?
> Thanks.
I posted this yesterday but it didn't show up. This includes an example
of how to add actions to buttons and how to do a simple layout.
import java.awt.*;
import java.awt.event.*;
public class test8 extends Frame implements ActionListener {
TextArea ta;
public test8() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = c.gridy = 0; c.insets = new Insets(2,2,2,2);
ta = new TextArea("Hello World!\n",20,80);
Button b1 = new Button("One");
Button b2 = new Button("Two");
Button b3 = new Button("Three");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.gridwidth = 3;
add(ta,c);
c.gridwidth = 1; ++c.gridy;
c.weightx = 1.0; c.anchor = GridBagConstraints.EAST;
add(b1,c);
++c.gridx;
c.weightx = 0.0; c.anchor = GridBagConstraints.CENTER;
add(b2,c);
++c.gridx;
c.weightx = 1.0; c.anchor = GridBagConstraints.WEST;
add(b3,c);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("One")) {
ta.append(ac + "\n");
} else if (ac.equals("Two")) {
ta.append(ac + "\n");
} else if (ac.equals("Three")) {
ta.append(ac + "\n");
}
}
public static void main(String[] args) {
new test8();
}
}
--
Knute Johnson
email s/nospam/knute/
| |
|
|
|
|
|
|
|