java - 小程序未显示完整

标签 java swing layout applet japplet

我刚刚创建了一个小程序

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); 但它并没有帮助内部区域保持不变而外部边界增加

最佳答案

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

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

关于java - 小程序未显示完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7454656/

相关文章:

使用计数器的 Java 数据竞争示例

Java - 从两个 java.nio.Path 创建相对的 java.nio.Path

java - 如果数据从 cellrenderer 更改,如何在列调整后计算 jTable 宽度和高度

java - 为什么我的组件没有包装在 Swing 的 FlowLayout 中?

vba - 使用 VBA 更改 Excel 图表的方向(纵向或横向)

layout - ExtJS 3 使南面板位于东西面板之间

html - 某些绝对定位的元素导致 Firefox 超出屏幕

java - 如何在 apk 中列出所有 3rd 方 jar?

java - 通用记录器接口(interface)引发语法错误

java - 在 TextField 中显示文本时出现循环问题