JFrame 中只显示一个组件 [英] Only one component shows up in JFrame

查看:31
本文介绍了JFrame 中只显示一个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了改进我的加密项目,我决定制作一个小图形用户界面.但是,当我运行程序时,屏幕上只显示顶部元素,而其他元素似乎被遮挡了,但我无法检查.有谁知道为什么?

除了 e()d() 之外,下面是我的完整代码,因为它们只是加密字符串,与 GUI 无关.我还想要一种在不编辑加密的情况下尽可能加快速度的方法,只是为了使它尽可能好.

As an improvement to my encryption project I decided to make a little GUI. However, when I run the program, only the top element shows up on the screen and it appears to obscure the others, though I have no way of checking. Does anyone know why?

Below is my code in its entirety besides e() and d() because those simply encrypt a string and have nothing to do with a GUI. I would also like a way to speed it up as much as possible without editing the encryption, just to make it as great as possbile.

@SuppressWarnings("serial")
public class EncDecExample extends JFrame implements ActionListener {
    final static JPanel top = new JPanel();
    final static JPanel mid = new JPanel();
    final static JPanel bot = new JPanel();
    final static JTextField in = new JTextField(10);
    final static JTextField out = new JTextField(10);
    final static JButton enc = new JButton("Encrypt");
    final static JButton dec = new JButton("Decrypt");
    final static JFrame f = new JFrame("Encryption/decryption");

    public static void main(String[] args) {
//        EncDec.exampleImplement();

        f.setSize(500, 500);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        out.setEditable(false);
        out.setText("Hello");
        in.setVisible(true);
        out.setVisible(true);
        enc.setVisible(true);
        dec.setVisible(true);
        top.add(in);
        mid.add(enc);
        mid.add(dec);
        bot.add(out);
        f.add(top);
        f.add(mid);
        f.add(bot);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == enc && !in.getText().equalsIgnoreCase("")) {
            out.setText(EncDec.e(in.getText(), 5));
        }
        else if(e.getSource() == dec && !in.getText().equalsIgnoreCase("")) {
            out.setText(EncDec.d(in.getText()));
        }
    }
}

推荐答案

JFrame 的内容面板有一个 BorderLayout.如果您将一个组件放置在没有约束的 BL 中,它最终会位于 CENTER 中.中心只能显示一个组件.

The content pane of a JFrame has a BorderLayout. If you place a component in a BL with no constraints it ends up in the CENTER. The center can only display one component.

为了立竿见影的效果,我建议:

For an immediate effect, I suggest:

f.add(top, BorderLayout.PAGE_START);
f.add(mid);
f.add(bot, BorderLayout.PAGE_END);

其他要点.

  1. 取出f.setSize(500, 500);并在setVisible(true)
  2. 之前立即调用pack()
  3. 为了更好地结束 GUI,请将 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 更改为 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  4. in.setVisible(true); 除了框架本身,把这些都去掉.当组件被添加到顶级容器并且该容器本身可见时,它会自动变为可见.

  5. public class EncDecExample extends JFrame
    更改为
    public class EncDecExample
    此代码保留对框架的引用,并且是正确的方法.
  1. Take out f.setSize(500, 500); and call pack() immediately before setVisible(true)
  2. For a better way to end the GUI, change f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); to f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  3. in.setVisible(true); Except for the frame itself, take these out. A component automatically becomes visible when it is added to a top level container and that container is itself made visible.
  4. Change
    public class EncDecExample extends JFrame
    to
    public class EncDecExample
    This code keeps a reference to a frame, and that is the right way to go.

这篇关于JFrame 中只显示一个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆