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

时间:2016-03-10 00:08:46

标签: java swing jframe

所以我试图复制这个网站的布局。

Website Pinterest登录

这是我已经做过的一些事情。 我的布局使用“null”。 我还在我的按钮上放了一个动作侦测器,显示另一个框架。

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.

我的代码出了什么问题? 我的代码能顺利运行吗?它显示了吗? 单击按钮后如何隐藏第一帧?

我也很难把图标放在画面上。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

存在许多基本错误

  • 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,有关详细信息,请参阅