Applet not appearing full

◇◆丶佛笑我妖孽 提交于 2019-11-27 05:37:01

As I mentioned in a comment, this question is really about how to layout components in a container. This example presumes you wish to add the extra space to the text fields and labels. The size of the applet is set in the HTML.

200x130

200x150

/*
<applet
  code='FixedSizeLayout'
  width='200'
  height='150'>
</applet>
*/
import java.awt.*;
import javax.swing.*;

public class FixedSizeLayout extends JApplet {
    public void init() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                initGui();
            }
        });
    }

    private void initGui() {
        JTabbedPane tb = new JTabbedPane();
        tb.addTab("Rcon Details", new RconSection());

        setContentPane(tb);
        validate();
    }
}

class RconSection extends JPanel {
    private static String TEST_COMMAND = "test";
    private static String CLEAR_COMMAND = "clear";
    private static JTextField ipText = new JTextField();
    private static JTextField portText = new JTextField();
    private static JTextField rPassText = new JTextField();

    public RconSection() {
        super(new BorderLayout(3,3));
        JLabel ip = new JLabel("IP");
        JLabel port = new JLabel("Port");
        JLabel rPass = new JLabel("Rcon Password");

        JButton testButton = new JButton("Test");
        testButton.setActionCommand(TEST_COMMAND);

        JButton clearButton = new JButton("Clear");
        clearButton.setActionCommand(CLEAR_COMMAND);

        JPanel panel = new JPanel(new GridLayout(3,2));
        panel.add(ip);
        panel.add(ipText);
        panel.add(port);
        panel.add(portText);
        panel.add(rPass);
        panel.add(rPassText);

        JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
        panel1.add(testButton);
        panel1.add(clearButton);

        add(panel, BorderLayout.CENTER);
        add(panel1, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Container c = new RconSection();
                JOptionPane.showMessageDialog(null, c);
            }
        });
    }
}

Here's another variation on your layout. Using @Andrew's tag-in-source method, it's easy to test from the command line:

$ /usr/bin/appletviewer HomeApplet.java

// <applet code='HomeApplet' width='400' height='200'></applet>
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class HomeApplet extends JApplet {

    @Override
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    private void createGUI() {
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Rcon1", new RconSection());
        tabbedPane.addTab("Rcon2", new RconSection());
        this.add(tabbedPane);
    }

    private static class RconSection extends JPanel implements ActionListener {

        private static final String TEST_COMMAND = "test";
        private static final String CLEAR_COMMAND = "clear";
        private JTextField ipText = new JTextField();
        private JTextField portText = new JTextField();
        private JTextField rPassText = new JTextField();

        public RconSection() {
            super(new BorderLayout());
            JLabel ip = new JLabel("IP");
            JLabel port = new JLabel("Port");
            JLabel rPass = new JLabel("Rcon Password");
            JButton testButton = new JButton("Test");
            testButton.setActionCommand(TEST_COMMAND);
            testButton.addActionListener(this);
            JButton clearButton = new JButton("Clear");
            clearButton.setActionCommand(CLEAR_COMMAND);
            clearButton.addActionListener(this);
            JPanel panel = new JPanel(new GridLayout(3, 2));
            panel.add(ip);
            panel.add(ipText);
            panel.add(port);
            panel.add(portText);
            panel.add(rPass);
            panel.add(rPassText);
            JPanel buttons = new JPanel(); // default FlowLayout
            buttons.add(testButton);
            buttons.add(clearButton);
            add(panel, BorderLayout.NORTH);
            add(buttons, BorderLayout.SOUTH);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(e);
        }
    }
}

Size of applet viewer does not depend on your code.

JApplet is not window, so in java code you can't write japplet dimensions. You have to change run settings. I don't know where exactly are in other ide's, but in Eclipse you can change dimensions in Project Properties -> Run/Debug settings -> click on your launch configurations file (for me there were only 1 - main class) -> edit -> Parameters. There you can choose width and height for your applet. save changes and you are good to go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!