簡體   English   中英

我的組件不會顯示在JFrame上,以及如何隱藏另一個框架

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

因此,我正在嘗試復制此網站的布局。

網站 Pinterest登錄

這是我已經做的一些事情。 我在布局中使用“空”。 我還在按鈕上放了一個動作監聽器,該按鈕顯示了另一幀。

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中運行它時,它只會顯示我的框架。 然后,我必須最大化它才能查看組件,但是在最大化然后最小化之后,它不再隱藏了。

之后,我最大化框架。

我的代碼有什么問題? 我的代碼能否在您的代碼上順利運行? 顯示嗎? 單擊按鈕后如何隱藏第一幀?

我也很難在框架上放置圖標。

謝謝您的幫助。

有一些基本錯誤

  • null布局。 避免使用null布局,像素完美布局是現代ui設計中的一種幻覺。 有太多因素會影響組件的單個大小,您無法控制。 Swing旨在與布局經理為核心一起工作,舍棄這些問題不會導致問題和問題的終結,您將花費越來越多的時間來嘗試糾正
  • 在完成更新UI之前,使框架可見。 在大多數情況下,可以使用revalidate ,但是由於這會導致布局管理器重新計算其布局,因此在使用null布局時毫無意義。

簡單的答案是,使用布局管理器。 答案越長越復雜。

您有三個不同的區域,“登錄方式”組,“字段”組和(“我想說”的)“動作”組。 每個都有自己的要求和功能,如果可以的話,最好單獨嘗試。

這將允許將功能應用於該組/類所獨有的每個組或類,並減少很多管理人員的頭痛

以下示例着重於布局,而不着重於如何連接功能,這可以通過簡單地使用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;
        }

    }

}

看一下在容器布置組件以及如何使用GridBagLayout LoginPane也可以使用GridLayout ,有關更多詳細信息,請參見

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM