Reputation: 51
I need to write an applet that converts char values to ascii values. Right now I'm just trying to get the layout of the applet right, but for some reason my JTextFields are showing up very strangely and I don't know how to go about fixing it. One is very large and the other is extremely small.
I had some trouble getting GridLayout to work the way I wanted, and I have some feeling that the problems have something to do with that but I don't know for sure.
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Ex1_2 extends JApplet implements ActionListener
{
private JTextField charInput;
private JLabel display1;
private JLabel dummy;
private JLabel display2;
private JLabel welcome;
private JTextField ascii;
private JLabel error;
Container pane = getContentPane();
public void init()
{
pane.setLayout(new BorderLayout());
pane.setBackground(Color.orange);
display1 = new JLabel("Character", SwingConstants.CENTER);
display2 = new JLabel("ASCII code", SwingConstants.CENTER);
dummy = new JLabel("", SwingConstants.CENTER);
dummy.setBackground(Color.orange);
welcome = new JLabel("Char <-> ASCII converter", SwingConstants.CENTER);
welcome.setForeground(Color.blue);
pane.add(welcome, BorderLayout.NORTH);
pane.add(dummy, BorderLayout.SOUTH);
//charInput.addActionListener(this);
add(addMiddle());
}
JPanel addMiddle()
{
JPanel p = new JPanel();
p.setLayout(new GridLayout(0, 2, 10, -5));
p.setBackground(Color.green);
p.add(display1, BorderLayout.SOUTH);
p.add(display2, BorderLayout.SOUTH);
p.add(dummy);
p.add(addInnerLeft());
p.add(addInnerRight());
p.add(dummy);
return p;
}
JPanel addInnerLeft()
{
JPanel p2 = new JPanel(new GridLayout(0, 2));
p2.setBackground(Color.yellow);
pane.add(p2);
charInput = new JTextField("");
charInput.setForeground(Color.black);
//p2.add(charInput);
return p2;
}
JPanel addInnerRight()
{
JPanel p3 = new JPanel();
p3.setBackground(Color.yellow);
ascii = new JTextField("");
ascii.setForeground(Color.black);
//p3.add(ascii);
return p3;
}
public void actionPerformed(ActionEvent e)
{
}
}
Upvotes: 1
Views: 68
Reputation: 285403
A quick fix:
Consider setting your JTextFields's column property. For instance, if you use:
ascii = new JTextField("", 5);
your ascii JTextField now gets some preferred size width.
A better long-term fix:
Study, learn and use the layout managers to better advantage.
Note that a problem with JApplets is that the size is specified by the HTML code and not by the layout managers (as far as I understand things), and so you must take care to make sure that the GUI's are sized big enough.
e.g., with layouts, I got this:
Upvotes: 4
Reputation: 551
The second text field is sized that way because you initialized it:
ascii = new JTextField("");
This represents that the text field has a string value for its name where the length of the string is zero. The text field is being sized automatically according to the text it contains. Since it contains a string with zero length, the size of the text field is so small.
You could use the setColumns(int columns)
method on the text field to give it a fixed width, so that it can get a preferred size.
Upvotes: 4