简体   繁体   English

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

[英]My components won't show on JFrame and how to hide another frame

So I'm trying to copy the layout of this website. 因此,我正在尝试复制此网站的布局。

Website Pinterest Log In 网站 Pinterest登录

Here's some what I have already done. 这是我已经做的一些事情。 I'm using "null" for my layout. 我在布局中使用“空”。 I also put an actionlistener on my button which shows another frame. 我还在按钮上放了一个动作监听器,该按钮显示了另一帧。

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);



 }}

Every time I would run this in my JCreator it would only show my frame. 每次我在JCreator中运行它时,它只会显示我的框架。 Then I have to maximize it to view the components but after I maximize it then minimize it doesn't hide anymore. 然后,我必须最大化它才能查看组件,但是在最大化然后最小化之后,它不再隐藏了。

After I maximize the frame. 之后,我最大化框架。

What is wrong with my code? 我的代码有什么问题? Does my code works on yours smoothly? 我的代码能否在您的代码上顺利运行? does it shows? 显示吗? How can I hide the first frame after clicking the button? 单击按钮后如何隐藏第一帧?

I'm having a hard time putting icon on the frame too. 我也很难在框架上放置图标。

Thanks for the help. 谢谢您的帮助。

There are a number of basic mistakes 有一些基本错误

  • null layouts. null布局。 Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. 避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 There are too many factors which affect the individual size of components, none of which you can control. 有太多因素会影响组件的单个大小,您无法控制。 Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正
  • Making the frame visible before you've finished updating the UI. 在完成更新UI之前,使框架可见。 In most cases this can be fixed with revalidate , but since that causes the layout managers to recalculate their layouts, it's pointless when you're using null layouts 在大多数情况下,可以使用revalidate ,但是由于这会导致布局管理器重新计算其布局,因此在使用null布局时毫无意义。

The simple answer is, use layout managers. 简单的答案是,使用布局管理器。 The longer answer is more complicated. 答案越长越复杂。

You have three distinct areas, the "login with" group, the "field" group and (what I like to term) the "action" group. 您有三个不同的区域,“登录方式”组,“字段”组和(“我想说”的)“动作”组。 Each of these have there own requirements and functionality, it's best to try a seperate them if you can. 每个都有自己的要求和功能,如果可以的话,最好单独尝试。

This will allow to apply functionality to each group or class which is unique to that group/class and reduce a lot of management head aches 这将允许将功能应用于该组/类所独有的每个组或类,并减少很多管理人员的头痛

The following examples focus on the layout, it does not focus on how you would then connect the functionality, this would be achieved simply through the use of an Observer Pattern , perhaps like ActionListener 以下示例着重于布局,而不着重于如何连接功能,这可以通过简单地使用Observer Pattern来实现,也许就像ActionListener

登录布局

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;
        }

    }

}

Take a look at Laying Out Components Within a Container and How to Use GridBagLayout . 看一下在容器布置组件以及如何使用GridBagLayout The LoginPane could also make use of a GridLayout , see for more details LoginPane也可以使用GridLayout ,有关更多详细信息,请参见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM