Creating a JApplet I have 2 Text Fields, a button and a Text Area.
private JPanel addressEntryPanel = new JPanel(new GridLayout(1,3));
private JPanel outputPanel = new JPanel(new GridLayout(1,1));
private JTextField serverTf = new JTextField("");
private JTextField pageTf = new JTextField("");开发者_StackOverflow社区
private JTextArea outputTa = new JTextArea();
private JButton connectBt = new JButton("Connect");
private JScrollPane outputSp = new JScrollPane(outputTa);
public void init()
{
setSize(500,500);
setLayout(new GridLayout(3,1));
add(addressEntryPanel);
addressEntryPanel.add(serverTf);
addressEntryPanel.add(pageTf);
addressEntryPanel.add(connectBt);
addressEntryPanel.setPreferredSize(new Dimension(50,50));
addressEntryPanel.setMaximumSize(addressEntryPanel.getPreferredSize());
addressEntryPanel.setMinimumSize(addressEntryPanel.getPreferredSize());
add(outputPanel);
outputPanel.add(outputSp);
outputTa.setLineWrap(true);
connectBt.addActionListener(this);
The problem is when debugging and putting it in a page the components / panels resize depending on the applet size. I don't want this. I want the textfields to be a certain size, and the text area to be a certain size. I've put stuff in there to set the size of them but they aren't working. How do I go about actually setting a strict size for either the components or the JPanel.
The first layout sin is to ignore a user-resize.
However, what you want can be achieved by using no layout manager.That is
private JPanel addressEntryPanel = new JPanel();
addressEntryPanel .setLayout(null);
精彩评论