Applet not appearing full

前端 未结 4 865
小蘑菇
小蘑菇 2020-11-30 15:16

I just created an applet

public class HomeApplet extends JApplet {

    private static final long serialVersionUID = -7650916407386219367L;

    //Called wh         


        
相关标签:
4条回答
  • 2020-11-30 15:40

    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

    0 讨论(0)
  • 2020-11-30 15:42

    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 FixedSizeLayout fixed at 200x130 200x150 FixedSizeLayout fixed at 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);
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-30 16:01

    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
    

    enter image description here

    // <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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 16:06

    Size of applet viewer does not depend on your code.

    0 讨论(0)
提交回复
热议问题