小程序未显示完整

发布于 2024-12-04 19:49:27 字数 4867 浏览 4 评论 0原文

我刚刚创建了一个小程序

public class HomeApplet extends JApplet {

    private static final long serialVersionUID = -7650916407386219367L;

    //Called when this applet is loaded into the browser.
    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
//      setSize(400, 400);
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) { 
            System.err.println("createGUI didn't complete successfully");
        }
    }

    private void createGUI() {
        RconSection rconSection = new RconSection();
        rconSection.setOpaque(true); 

//      CommandArea commandArea = new CommandArea();
//      commandArea.setOpaque(true); 

        JTabbedPane tabbedPane = new JTabbedPane();
//      tabbedPane.setSize(400, 400);
        tabbedPane.addTab("Rcon Details", rconSection);
//      tabbedPane.addTab("Commad Area", commandArea); 


        setContentPane(tabbedPane);       
    }
}

,其中第一个选项卡是:

package com.rcon;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.Bean.RconBean;
import com.util.Utility;

public class RconSection extends JPanel implements ActionListener{
    /**
     * 
     */
    private static final long serialVersionUID = -9021500288377975786L;
    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();
    //      private DynamicTree treePanel;

    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 panel1 = new JPanel(new GridLayout(1,3));
        panel1.add(testButton);
        panel1.add(clearButton);

        add(panel);
        add(panel1);
//      add(panel, BorderLayout.NORTH);
//      add(panel1, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.getActionCommand().equals(TEST_COMMAND)){
            String ip = ipText.getText().trim();
            if(!Utility.checkIp(ip)){
                ipText.requestFocusInWindow();
                ipText.selectAll();
                JOptionPane.showMessageDialog(this,"Invalid Ip!!!");
                return;
            }
            String port = portText.getText().trim();
            if(port.equals("") ||  !Utility.isIntNumber(port)){
                portText.requestFocusInWindow();
                portText.selectAll();
                JOptionPane.showMessageDialog(this,"Invalid Port!!!");
                return;
            }
            String pass = rPassText.getText().trim();
            if(pass.equals("")){
                rPassText.requestFocusInWindow();
                rPassText.selectAll();
                JOptionPane.showMessageDialog(this,"Enter Rcon Password!!!");
                return;
            }
            RconBean rBean = RconBean.getBean();
            rBean.setIp(ip);
            rBean.setPassword(pass);
            rBean.setPort(Integer.parseInt(port));
            if(!Utility.testConnection()){
                rPassText.requestFocusInWindow();
                rPassText.selectAll();
                JOptionPane.showMessageDialog(this,"Invalid Rcon!!!");
                return;
            }else{
                JOptionPane.showMessageDialog(this,"Correct Rcon!!!");
                return;
            }
        }
        else if(arg0.getActionCommand().equals(CLEAR_COMMAND)){
            ipText.setText("");
            portText.setText("");
            rPassText.setText("");
        }
    }
}

它显示为

applet Screenshot

已裁剪了一些数据如何完整显示它并使小程序也不可调整大小。我尝试了 setSize(400, 400); 但它没有帮助内部区域保持不变而外部边界增加

I just created an applet

public class HomeApplet extends JApplet {

    private static final long serialVersionUID = -7650916407386219367L;

    //Called when this applet is loaded into the browser.
    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
//      setSize(400, 400);
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) { 
            System.err.println("createGUI didn't complete successfully");
        }
    }

    private void createGUI() {
        RconSection rconSection = new RconSection();
        rconSection.setOpaque(true); 

//      CommandArea commandArea = new CommandArea();
//      commandArea.setOpaque(true); 

        JTabbedPane tabbedPane = new JTabbedPane();
//      tabbedPane.setSize(400, 400);
        tabbedPane.addTab("Rcon Details", rconSection);
//      tabbedPane.addTab("Commad Area", commandArea); 


        setContentPane(tabbedPane);       
    }
}

where the fisrt tab is:

package com.rcon;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.Bean.RconBean;
import com.util.Utility;

public class RconSection extends JPanel implements ActionListener{
    /**
     * 
     */
    private static final long serialVersionUID = -9021500288377975786L;
    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();
    //      private DynamicTree treePanel;

    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 panel1 = new JPanel(new GridLayout(1,3));
        panel1.add(testButton);
        panel1.add(clearButton);

        add(panel);
        add(panel1);
//      add(panel, BorderLayout.NORTH);
//      add(panel1, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.getActionCommand().equals(TEST_COMMAND)){
            String ip = ipText.getText().trim();
            if(!Utility.checkIp(ip)){
                ipText.requestFocusInWindow();
                ipText.selectAll();
                JOptionPane.showMessageDialog(this,"Invalid Ip!!!");
                return;
            }
            String port = portText.getText().trim();
            if(port.equals("") ||  !Utility.isIntNumber(port)){
                portText.requestFocusInWindow();
                portText.selectAll();
                JOptionPane.showMessageDialog(this,"Invalid Port!!!");
                return;
            }
            String pass = rPassText.getText().trim();
            if(pass.equals("")){
                rPassText.requestFocusInWindow();
                rPassText.selectAll();
                JOptionPane.showMessageDialog(this,"Enter Rcon Password!!!");
                return;
            }
            RconBean rBean = RconBean.getBean();
            rBean.setIp(ip);
            rBean.setPassword(pass);
            rBean.setPort(Integer.parseInt(port));
            if(!Utility.testConnection()){
                rPassText.requestFocusInWindow();
                rPassText.selectAll();
                JOptionPane.showMessageDialog(this,"Invalid Rcon!!!");
                return;
            }else{
                JOptionPane.showMessageDialog(this,"Correct Rcon!!!");
                return;
            }
        }
        else if(arg0.getActionCommand().equals(CLEAR_COMMAND)){
            ipText.setText("");
            portText.setText("");
            rPassText.setText("");
        }
    }
}

it appears as

applet screenshot

is has cropped some data how to display it full and make the applet non resizable as well. i tried setSize(400, 400); but it didnt helped the inner area remains the same and outer boundaries increases

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾忌 2024-12-11 19:49:27

这是布局的另一种变体。使用@Andrew的tag-in-source方法,很容易从命令行进行测试:

$ /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);
        }
    }
}

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);
        }
    }
}
千寻… 2024-12-11 19:49:27

正如我在评论中提到的,这个问题实际上是关于如何在容器中布局组件。此示例假设您希望向文本字段和标签添加额外的空间。小程序的大小在 HTML 中设置。

200x130 FixedSizeLayout 固定为 200x130 200x150 FixedSizeLayout 固定为 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);
            }
        });
    }
}

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);
            }
        });
    }
}
当爱已成负担 2024-12-11 19:49:27

小程序查看器的大小不取决于您的代码。

Size of applet viewer does not depend on your code.

南…巷孤猫 2024-12-11 19:49:27

JApplet不是窗口,所以在java代码中你不能写japplet尺寸。您必须更改运行设置。我不知道其他 ide 中的具体位置,但在 Eclipse 中,您可以在“项目属性”->“项目属性”中更改尺寸。运行/调试设置->单击您的启动配置文件(对我来说只有 1 个 - 主类)->编辑->参数。在那里您可以选择小程序的宽度和高度。保存更改即可开始

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文