java - 我的组件不会显示在 JFrame 上以及如何隐藏另一个框架

标签 java swing jframe

所以我正在尝试复制该网站的布局。

Website Pinterest 登录

这是我已经完成的一些工作。 我的布局使用“null”。 我还在按钮上放置了一个 Action 监听器,它显示另一个框架。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Frame {



public static void main (String [] args) {

    JFrame frame = new JFrame("Pinterest");
    frame.setVisible(true);
    frame.setSize(1300,750);


    JPanel panel = new JPanel();
    frame.add(panel);

    JLabel name = new JLabel("Log in to Pinterest");
    name.setBounds(500, 96, 300, 100);
    name.setFont(new Font("Tahoma", Font.PLAIN, 28));

    JTextField text1 = new JTextField(15);
    text1.setBounds(500, 450, 300, 40);

    JTextField text2 = new JTextField(15);
    text2.setBounds(500, 350, 300, 40);

    JButton button = new JButton("Log In");
    button.setBounds(560,550, 200,30 );

    panel.setLayout(null);
    panel.add(name);
    panel.add(text1);
    panel.add(text2);
    panel.add(button);
    button.addActionListener(new Action1());

}
static class Action1 implements ActionListener
    {
public void actionPerformed(ActionEvent e)
{


JFrame frame2=  new JFrame("Pinterest");
frame2.setVisible(true);
frame2.setSize(1300,750);



 }}

每次我在 JCreator 中运行它时,它只会显示我的框架。然后我必须最大化它才能查看组件,但是在我最大化然后最小化它之后,它就不再隐藏了。

After I maximize the frame.

我的代码有什么问题吗? 我的代码在你的代码上运行顺利吗?它显示吗? 如何隐藏点击按钮后的第一帧?

我也很难将图标放在框架上。

感谢您的帮助。

最佳答案

存在一些基本错误

  • null 布局。避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计目的是与核心的布局管理器一起工作,放弃这些将导致无休止的问题和问题,您将花费越来越多的时间来尝试纠正
  • 在完成 UI 更新之前使框架可见。在大多数情况下,这可以通过 revalidate 来修复,但由于这会导致布局管理器重新计算其布局,因此当您使用 null 布局时,这是毫无意义的

简单的答案是,使用布局管理器。答案越长越复杂。

您有三个不同的区域,“登录方式”组、“字段”组和(我喜欢这样称呼)“操作”组。每个都有自己的要求和功能,如果可以的话最好尝试单独使用它们。

这将允许将功能应用到每个组或类,这些功能对于该组/类是唯一的,并减少很多管理头痛

以下示例重点介绍布局,而不重点介绍如何连接功能,这只需通过使用 Observer Pattern 即可实现,也许像 ActionListener

Login layout

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new LoginPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class LoginPane extends JPanel {

        public LoginPane() {
            setBackground(Color.WHITE);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(4, 20, 4, 20);

            JLabel title = new JLabel("Log in to Pinterest");
            title.setFont(title.getFont().deriveFont(Font.BOLD, 18f));
            title.setBorder(new EmptyBorder(10, 0, 10, 0));

            add(title, gbc);
            add(new GroupPane(), gbc);
            gbc.insets = new Insets(4, 0, 4, 0);
            add(new JSeparator(JSeparator.HORIZONTAL), gbc);
            gbc.insets = new Insets(4, 20, 4, 20);
            add(new FieldPane(), gbc);
            gbc.insets = new Insets(4, 0, 0, 0);
            add(new ActionPane(), gbc);
        }

    }

    public class GroupPane extends JPanel {

        public GroupPane() {
            setOpaque(false);
            JPanel fbPane = new JPanel();
            JPanel goPane = new JPanel();
            JPanel twPane = new JPanel();

            fbPane.setBackground(Color.RED);
            goPane.setBackground(Color.BLUE);
            twPane.setBackground(Color.CYAN);

            fbPane.add(makeLabel("Log in with Facebook"));
            goPane.add(makeLabel("Log in with Google"));
            twPane.add(makeLabel("Log in with Twitter"));

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(4, 0, 4, 0);

            add(fbPane, gbc);
            add(goPane, gbc);
            add(twPane, gbc);
        }

        protected JLabel makeLabel(String text) {
            JLabel label = new JLabel(text);
            label.setForeground(Color.WHITE);
            label.setFont(label.getFont().deriveFont(Font.BOLD, 14f));
            return label;
        }

    }

    public class FieldPane extends JPanel {

        private JTextField email;
        private JPasswordField password;

        public FieldPane() {
            setOpaque(false);
            email = new JTextField(10);
            password = new JPasswordField(10);

            email.setBackground(new Color(225, 225, 225));
            password.setBackground(new Color(225, 225, 225));

            Font font = email.getFont().deriveFont(Font.PLAIN, 24f);
            email.setFont(font);
            password.setFont(font);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(4, 0, 4, 0);

            add(email, gbc);
            add(password, gbc);

            JLabel label = new JLabel("Are you a business? Get started here");
            label.setFont(label.getFont().deriveFont(Font.PLAIN, 10f));
            gbc.insets.left = 4;
            add(label, gbc);
        }

    }

    public class ActionPane extends JPanel {

        public ActionPane() {
            setBorder(new EmptyBorder(10, 20, 10, 20));
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.WEST;

            add(makeLabel("Forgot your password?"), gbc);
            gbc.gridy++;
            add(makeLabel("Sign up now"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.gridheight = 2;
            gbc.ipady = 10;
            gbc.anchor = GridBagConstraints.EAST;
            JButton login = new JButton("Log in");
            add(login, gbc);
        }

        protected JLabel makeLabel(String text) {
            JLabel label = new JLabel(text);
            label.setForeground(Color.DARK_GRAY);
            return label;
        }

    }

}

看看Laying Out Components Within a ContainerHow to Use GridBagLayoutLoginPane 还可以使用 GridLayout,请参阅 引用资料 获取更多详细信息

关于java - 我的组件不会显示在 JFrame 上以及如何隐藏另一个框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35905177/

相关文章:

java - Jframe中的计时器重新启动

java - Selenium 中的代理身份验证弹出窗口

java - 我如何在我的小程序中使用 JXMapViewer?

java - 如何在android中检查日期(从json文件获取)是之前还是之后?

java Swing : Focus lost on enter key event

java - Swing中如何设置默认打开窗口?

java - 如何更改 Mac 操作系统上按钮的颜色?

mysql - 如何使用java从Mysql中检索数据并将其显示在jframe中的jtable上

java - 在 Java 的 Arraylist 中创建 Arraylist

java - 如何将 JQPL 查询转换为 DTO?