主頁 > 軟體工程 > 有沒有辦法隱藏JFrame中的所有元素

有沒有辦法隱藏JFrame中的所有元素

2021-12-03 00:30:45 軟體工程

我目前正在開發 Java 應用程式,我正在使用 JFrame,在 JFrame 內部我有一些按鈕、標簽和文本欄位。我需要隱藏 JFrame 中的所有元素。我不需要隱藏 JFrame 本身,只需要隱藏其中的元素,這樣我就可以顯示其他一些元素。有沒有辦法在不手動撰寫 element.setVisibility(false); 的情況下隱藏它們?對于他們中的每一個?

import javax.swing.*;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
import java.util.ArrayList;
public class GraphicUserInterface implements ActionListener {
    /**
     * Constructor for objects of class GraphicUserInterface
     */
    private JFrame window;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
   
    private JLabel title;
    private JLabel username;
    private JLabel password;
    private JTextField textF1;
    private JPasswordField passF;
    private JTextArea title2;
    
    
    private int prevMenu;
    private int menu ;

    private Admin admin;

    public GraphicUserInterface() {
        // initialise instance variables
        this.admin = new Admin();
        this.window = new JFrame();
        this.button1 = new JButton();
        this.button2 = new JButton();
        this.button3 = new JButton();
        this.button4 = new JButton();
        this.button5 = new JButton();
        this.button6 = new JButton();
        
        this.title2 = new JTextArea();
        
        this.title = new JLabel();
        this.username = new JLabel();
        this.password = new JLabel();
        this.textF1 = new JTextField();
        this.passF = new JPasswordField();
        this.button1.addActionListener(this);
        this.button2.addActionListener(this);
        
    }

    public void start() {
        
        switch(this.menu) {
            case 0:
            this.window.setTitle("?tudentsky systém");
            this.button1.setText("Admin");
            this.button2.setText("?tudent");
            this.title.setText("Zvo? typ prihlásenia"); 

            this.title.setBounds(130,50,200,70);
            this.title.setFont(new Font("Calibri", Font.BOLD, 20));

            this.button1.setBounds(140,130,120,55);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,5));

            this.button2.setBounds(140,215,120,55);
            this.button2.setBackground(new Color(156,156,156));
            this.button2.setBorder(new LineBorder(Color.black,5));
            this.button2.setForeground(Color.white);

          
            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.title); 
            visOfUIElements(false);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(400,400);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            

            //this.button1.addActionListener(this);
                    
            //this.button2.addActionListener(this);
            break;
            case 1:
            
            this.window.setTitle("?tudentsky systém");
            this.button1.setText("Prihlási?");
            this.button2.setText("Sp??");
            this.title.setText("Prihlásenie Admin");
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);
            this.username.setText("Login:");
            this.password.setText("Heslo:");
            
            this.title.setBounds(100,50,200,70);
            this.title.setFont(new Font("Calibri", Font.BOLD, 20));
            
            this.username.setBounds(80,100,50,70);
            this.username.setFont(new Font("Calibri", Font.BOLD, 15));
       
            this.password.setBounds(80,130,50,70);      
            this.password.setFont(new Font("Calibri", Font.BOLD, 15));

            this.button1.setBounds(130,180,60,30);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,3));

            this.button2.setBounds(195,180,60,30);
            this.button2.setBackground(new Color(156,156,156));
            this.button2.setBorder(new LineBorder(Color.black,3));
            this.button2.setForeground(Color.white);
            
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);

          
            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.title); 
            this.window.add(this.textF1);
            this.window.add(this.passF);
            this.window.add(this.username);
            this.window.add(this.password);
            visOfUIElements(true);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(400,400);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            

            
            break;
            case 2:
            
            this.window.setTitle("?tudentsky systém");
            this.button1.setText("Prihlási?");
            this.button2.setText("Sp??");
            this.title.setText("Prihlásenie ?tudent");
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);
            this.username.setText("Login:");
            this.password.setText("Heslo:");
            
            this.title.setBounds(100,50,200,70);
            this.title.setFont(new Font("Calibri", Font.BOLD, 20));
            
            this.username.setBounds(80,100,50,70);
            this.username.setFont(new Font("Calibri", Font.BOLD, 15));
       
            this.password.setBounds(80,130,50,70);      
            this.password.setFont(new Font("Calibri", Font.BOLD, 15));

            this.button1.setBounds(130,180,60,30);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,3));

            this.button2.setBounds(195,180,60,30);
            this.button2.setBackground(new Color(156,156,156));
            this.button2.setBorder(new LineBorder(Color.black,3));
            this.button2.setForeground(Color.white);
            
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);
            
            this.textF1.setText("Marek");
          
            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.title); 
            this.window.add(this.textF1);
            this.window.add(this.passF);
            this.window.add(this.username);
            this.window.add(this.password);
            visOfUIElements(true);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(400,400);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            
            break;
            case 3:
            this.window.setTitle("?tudentsky systém");
            this.button1.setText("Prida? ?tudenta");
            this.button2.setText("Vyh?ada? ?tudenta");
            this.button3.setText("Prida? predmet");
            this.button4.setText("Prida? odbor");
            this.button5.setText("Prida? fakultu");
            this.button6.setText("Odhlási?");
            
            this.title.setText("Admin");
            this.textF1.setBounds(325,125,125,20);
            this.passF.setBounds(325,155,125,20);
            this.username.setText("Login:");
            this.password.setText("Heslo:");
            
            this.title2.setBounds(380,50,200,200);
            this.title2.setFont(new Font("Calibri", Font.BOLD, 20));
            
            this.username.setBounds(80,100,50,70);
            this.username.setFont(new Font("Calibri", Font.BOLD, 15));
       
            this.password.setBounds(80,130,50,70);      
            this.password.setFont(new Font("Calibri", Font.BOLD, 15));

            this.button1.setBounds(350,130,120,55);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,3));

            this.button2.setBounds(350,215,120,55);
            this.button2.setBackground(Color.white);
            this.button2.setBorder(new LineBorder(Color.black,3));
            this.button2.setForeground(Color.black);
            
            
            this.button3.setBounds(350,300,120,55);
            this.button3.setBackground(Color.white);
            this.button3.setBorder(new LineBorder(Color.black,3));

            this.button4.setBounds(350,385,120,55);
            this.button4.setBackground(Color.white);
            this.button4.setBorder(new LineBorder(Color.black,3));
            
            
            this.button5.setBounds(350,470,120,55);
            this.button5.setBackground(Color.white);
            this.button5.setBorder(new LineBorder(Color.black,3));

            this.button6.setBounds(350,555,120,55);
            this.button6.setBackground(new Color(156,156,156));
            this.button6.setBorder(new LineBorder(Color.black,3));
            this.button6.setForeground(Color.white);
            
            
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);

          
            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.button3);
            this.window.add(this.button4);
            this.window.add(this.button5);
            this.window.add(this.button6);
            
            this.window.add(this.title); 
            this.window.add(this.textF1);
            this.window.add(this.passF);
            this.window.add(this.username);
            this.window.add(this.password);
            visOfUIElements(false);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(820,800);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            break;
            case 4:
            this.window.setTitle("?tudentsky systém");
            this.button1.setText("Predmety");
            this.button2.setText("Volite?né predmety");
            this.button3.setText("Zmeni? heslo");
            this.button4.setText("Profilové údaje");
            this.button5.setText("Odhlási?");
            String a = "\nID: 560269 ";
            String b ="\nMeno: Marek Magula ";
            String c = "\n?tudijná skupina: 5ZYS11";
            
            this.title2.setEditable(false);
            this.title2.append(a);
            this.title2.append(b);
            this.title2.append(c);
            this.textF1.setBounds(325,125,125,20);
            this.passF.setBounds(325,155,125,20);
            this.username.setText("Login:");
            this.password.setText("Heslo:");
            
            this.title2.setBounds(350,50,200,200);
            this.title2.setFont(new Font("Calibri", Font.BOLD, 15));
            this.title2.setBackground(new Color(235, 235, 235));
            
            this.username.setBounds(80,100,50,70);
            this.username.setFont(new Font("Calibri", Font.BOLD, 15));
       
            this.password.setBounds(80,130,50,70);      
            this.password.setFont(new Font("Calibri", Font.BOLD, 15));

            this.button1.setBounds(350,130,120,55);
            this.button1.setBackground(Color.white);
            this.button1.setBorder(new LineBorder(Color.black,3));

            this.button2.setBounds(350,215,120,55);
            this.button2.setBackground(Color.white);
            this.button2.setBorder(new LineBorder(Color.black,3));
            this.button2.setForeground(Color.black);
            
            
            this.button3.setBounds(350,300,120,55);
            this.button3.setBackground(Color.white);
            this.button3.setBorder(new LineBorder(Color.black,3));

            this.button4.setBounds(350,385,120,55);
            this.button4.setBackground(Color.white);
            this.button4.setBorder(new LineBorder(Color.black,3));

            this.button5.setBounds(350,470,120,55);
            this.button5.setBackground(new Color(156,156,156));
            this.button5.setBorder(new LineBorder(Color.black,3));
            this.button5.setForeground(Color.white);
  
            this.textF1.setBounds(130,125,125,20);
            this.passF.setBounds(130,155,125,20);

            this.window.add(this.button1);
            this.window.add(this.button2);
            this.window.add(this.button3);
            this.window.add(this.button4);
            
            this.window.add(this.button5);
            
            this.window.add(this.title2); 
            this.window.remove(this.title); 
            this.window.add(this.textF1);
            this.window.add(this.passF);
            this.window.add(this.username);
            this.window.add(this.password);
            visOfUIElements(false);

            this.window.getContentPane().setBackground(new Color(235, 235, 235));
            this.window.setSize(820,800);  
            this.window.setLayout(null);  
            this.window.setVisible(true);
            
            break;
        
        }

    }
    public void actionPerformed(ActionEvent e) {
        switch (this.menu) {
            case 0:
            if (e.getSource() == this.button1) {
                this.menu = 1;            
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 2;           
                start();
            }
            break;
            case 1:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 2:
            if (e.getSource()== this.button1) {
                this.menu = 4;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 3:
            if (e.getSource() == this.button1) {
                this.menu = 5;            
                start();
            } else if (e.getSource() == this.button2) {
                this.menu = 6;           
                start();
            } else if (e.getSource() == this.button3) {
                this.menu = 7;           
                start();
            } else if (e.getSource() == this.button4) {
                this.menu = 8;           
                start();
            } else if (e.getSource() == this.button5) {
                this.menu = 9;           
                start();
            } else if (e.getSource() == this.button6) {
                this.menu = 0;           
                start();
            }
            break;
            case 4:
            if (e.getSource() == this.button1) {
                this.menu = 10;            
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 11;           
                start();
            } else if (e.getSource()== this.button3) {
                this.menu = 12;           
                start();
            } else if (e.getSource()== this.button4) {
                this.menu = 13;           
                start();
            } else if (e.getSource()== this.button5) {
                this.menu = 0;           
                start();
            } 
            break;
            case 5:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 6:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 7:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 8:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 9:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 10:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 11:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 12:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
            case 13:
            if (e.getSource()== this.button1) {
                this.menu = 3;
                start();
            } else if (e.getSource()== this.button2) {
                this.menu = 0;
                start();
            }
            break;
           
        }    
    }
    
    /*public void visOfUIElements(boolean visibility) {
        this.username.setVisible(visibility);
            this.password.setVisible(visibility);
            this.textF1.setVisible(visibility);
            this.passF.setVisible(visibility);
    }*/
}

這是我的代碼。我正在更改 UI 視窗的布局,需要使其更高效,因此我不會撰寫數百次相同的代碼。

uj5u.com熱心網友回復:

將要控制的組件添加到公共容器中,然后簡單地隱藏/顯示它

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
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() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBorder(new EmptyBorder(new Insets(8, 8, 8, 8)));
            setLayout(new BorderLayout());
            JPanel basePane = new JPanel(new GridBagLayout());
            basePane.setBorder(new EmptyBorder(new Insets(50, 50, 50, 50)));
            basePane.add(new JLabel("I can see you"));
            add(basePane);

            JButton btn = new JButton("Hide");
            add(btn, BorderLayout.SOUTH);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (basePane.isVisible()) {
                        basePane.setVisible(false);
                        btn.setText("Show");
                    } else {
                        basePane.setVisible(true);
                        btn.setText("Hide");
                    }
                }
            }); 
        }

    }
}

我認為這不會完成這項作業......

實際上,我認為它會,但是,您錯過了一些非常重要的核心概念。

解耦

您正在使用 OO 語言進行操作,您要關注的一件事是“解耦”您的功能對其他功能的依賴關系。問自己一個簡單的問題。如果我不得不修改系統的這一部分,它會有什么后果。如果您發現自己處于一連串的變化中,那么您的系統就是緊耦合的

單一職責

您希望盡可能地隔離功能,以便它只做一項作業。而不是使用“上帝”類(就像您擁有的那樣),它試圖做所有事情,將您的問題分解成小塊并隔離功能,以便它盡可能簡單地完成作業。

盡可能地,每個元素不應該依賴于了解系統的當前狀態,超出它被告知的內容,這是第一點和第三點的一部分。

依賴注入

將每個子系統需要的資訊傳遞給他們,而不是擁有全域可訪問的資源。這對于測驗來說真的很好,但它進一步減少了耦合

介面代碼

使用interface來描述功能的高級概念。例如,“學生詳細資訊”視圖并不關心學生資訊的結構、獲取或管理方式,它只關心呈現由interface(或合同)維護的一些資訊這將使對底層實作進行核心更改變得更容易,而不會對系統中想要使用它的所有部分產生不利影響,因為它們僅依賴于由interface

例子...

現在,通常,我使用 a CardLayout,但 aCardLayout不像這個問題可能需要的那樣動態。

因此,相反,我推出了自己的“導航”作業流程。這使我可以更好地控制需要注入的資訊并允許更動態的演示 - 您不想在學生注銷時顯示用學生憑據填充的學生登錄螢屏。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayDeque;
import java.util.Deque;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
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() {
                JFrame frame = new JFrame();
                frame.setTitle("?tudentsky systém");
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public interface Authenticator {

        public Student authenticateStudent(String userName, char[] password);

        public void logout(Student student);
    }

    // This is how you would return information
    public interface Student {
    }

    // You'll need to fill this out...
    public class DefaultAuthenticator implements Authenticator {

        @Override
        public Student authenticateStudent(String userName, char[] password) {
            return null;
        }

        @Override
        public void logout(Student student) {
        }

    }

    public class NavigationManager {

        private Deque<JPanel> viewHierarchy = new ArrayDeque<>(16);

        private JPanel parent;

        public NavigationManager(JPanel parent) {
            this.parent = parent;
        }

        protected void push(JPanel view) {
            viewHierarchy.add(view);
            present(view);
        }

        protected void present(JPanel view) {
            parent.removeAll();
            parent.add(view);
            parent.revalidate();
            Window window = SwingUtilities.windowForComponent(parent);
            if (window != null) {
                window.pack();
                window.setLocationRelativeTo(null);
            }
            parent.repaint();
        }

        protected void pop() {
            if (viewHierarchy.size() > 1) {
                viewHierarchy.removeLast();
            }
            present(viewHierarchy.getLast());
        }
    }

    public class MainPane extends JPanel {

        private MenuPane menuPane;

        private Authenticator authenticator = new DefaultAuthenticator();
        private NavigationManager navigationManager;

        public MainPane() {
            setLayout(new BorderLayout());
            navigationManager = new NavigationManager(this);

            push(getMenuPane());
        }

        protected void push(JPanel view) {
            navigationManager.push(view);
        }

        protected void pop() {
            navigationManager.pop();
        }

        protected void present(JPanel view) {
            navigationManager.present(view);
        }

        protected Authenticator getAuthenticator() {
            return authenticator;
        }

        protected MenuPane getMenuPane() {
            if (menuPane == null) {
                menuPane = new MenuPane(new MenuPane.NavigationController() {
                    @Override
                    public void presentAdmin() {
                    }

                    @Override
                    public void presentStudent() {
                        present(getStudentLoginPane());
                    }
                });
            }
            return menuPane;
        }

        protected StudentLoginPane getStudentLoginPane() {
            return new StudentLoginPane(getAuthenticator(), new StudentLoginPane.NavigationController() {
                @Override
                public void back() {
                    pop();
                }

                @Override
                public void studentAuthentictated(Student student) {
                    push(getStudentDetailsPane(student));
                }
            });
        }

        protected StudentPane getStudentDetailsPane(Student student) {
            return new StudentPane(student, new StudentPane.NavigationController() {
                @Override
                public void logout(Student student) {
                    getAuthenticator().logout(student);
                    pop();
                }
            });
        }

    }

    public class MenuPane extends JPanel {

        public static interface NavigationController {

            public void presentAdmin();

            public void presentStudent();
        }

        private JButton adminBtn;
        private JButton studentBtn;
        private JLabel title;

        public MenuPane(NavigationController navigationController) {
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(new Insets(25, 25, 25, 25)));
            adminBtn = new JButton("Admin");
            studentBtn = new JButton("?tudent");
            title = new JLabel("Zvo? typ prihlásenia");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(4, 4, 4, 4);

            add(title, gbc);
            add(adminBtn, gbc);
            add(studentBtn, gbc);

            adminBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigationController.presentAdmin();
                }
            });
            studentBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigationController.presentStudent();
                }
            });
        }
    }

    public class StudentLoginPane extends JPanel {

        public static interface NavigationController {

            public void back();

            public void studentAuthentictated(Student student);
        }

        private JLabel title;
        private JButton signInBtn;
        private JButton backBtn;

        private JLabel loginLabel;
        private JLabel passwordLabel;
        private JTextField userNameField;
        private JTextField passwordField;

        public StudentLoginPane(Authenticator authenticator, NavigationController navigationController) {
            title = new JLabel("?tudentsky systém");
            title.setHorizontalAlignment(JLabel.CENTER);
            title.setBorder(new EmptyBorder(16, 16, 16, 16));
            signInBtn = new JButton("Prihlási?");
            backBtn = new JButton("Sp??");
            loginLabel = new JLabel("Login:");
            passwordLabel = new JLabel("Heslo:");

            userNameField = new JTextField(10);
            passwordField = new JPasswordField(10);

            setLayout(new BorderLayout());
            add(title, BorderLayout.NORTH);

            JPanel contentPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.LINE_END;
            gbc.insets = new Insets(4, 4, 4, 4);

            contentPane.add(loginLabel, gbc);
            gbc.gridy  ;
            contentPane.add(passwordLabel, gbc);

            gbc.anchor = GridBagConstraints.LINE_START;
            gbc.gridx = 1;
            gbc.gridy = 0;
            contentPane.add(userNameField, gbc);
            gbc.gridy  ;
            contentPane.add(passwordField, gbc);

            add(contentPane);

            JPanel buttonPane = new JPanel(new GridBagLayout());
            buttonPane.setBorder(new EmptyBorder(16, 16, 16, 16));

            buttonPane.add(signInBtn);
            buttonPane.add(backBtn);

            add(buttonPane, BorderLayout.SOUTH);

            signInBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Perform the required authentication
                    // If you're wondering, yes, I'd probably pass in a reference
                    // to a "authenticator" which would do the actual work for
                    // me
                    // Passing null here is just for demonstration purposes
                    navigationController.studentAuthentictated(null);
                }
            });

            backBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigationController.back();
                }
            });
        }
    }

    public class StudentPane extends JPanel {

        public static interface NavigationController {

            public void logout(Student student);
        }

        private JLabel idLabel;
        private JLabel nameLabel;
        private JLabel groupLabel;

        // And here we have a demonstration of a decoupled workflow
        // The MainPane shouldn't care about anything other then logging
        // the student out, all other "sub flows" can become the responsbility
        // of the sub views themselves
        private NavigationManager navigationManager;

        public StudentPane(Student student, NavigationController navigationController) {
            navigationManager = new NavigationManager(this);

            idLabel = new JLabel("560269"); // Use the reference to student to get this
            nameLabel = new JLabel("Marek Magula"); // Use the reference to student to get this
            groupLabel = new JLabel("5ZYS11"); // Use the reference to student to get this

            setLayout(new BorderLayout());

            JPanel detailsPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.LINE_END;
            gbc.insets = new Insets(4, 4, 4, 4);

            detailsPane.add(new JLabel("ID:"), gbc);
            gbc.gridy  ;
            detailsPane.add(new JLabel("Meno:"), gbc);
            gbc.gridy  ;
            detailsPane.add(new JLabel("?tudijná skupina:"), gbc);

            add(detailsPane, BorderLayout.NORTH);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.LINE_START;
            detailsPane.add(idLabel, gbc);
            gbc.gridy  ;
            detailsPane.add(nameLabel, gbc);
            gbc.gridy  ;
            detailsPane.add(groupLabel, gbc);

            JButton subjectsBtn = new JButton("Predmety");
            JButton optioinalSubjectsBtn = new JButton("Volite?né predmety");
            JButton changePassword = new JButton("Zmeni? heslo");
            JButton profileBtn = new JButton("Profilové údaje");
            JButton logoutBtn = new JButton("Odhlási?");

            JPanel buttonPane = new JPanel(new GridBagLayout());
            gbc = new GridBagConstraints();
            gbc.gridwidth = gbc.REMAINDER;
            gbc.fill = gbc.HORIZONTAL;
            gbc.ipadx = 8;
            gbc.ipady = 8;
            gbc.insets = new Insets(4, 4, 4, 4);

            buttonPane.add(subjectsBtn, gbc);
            buttonPane.add(optioinalSubjectsBtn, gbc);
            buttonPane.add(changePassword, gbc);
            buttonPane.add(profileBtn, gbc);
            buttonPane.add(logoutBtn, gbc);

            add(buttonPane);

            subjectsBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Create a student's subject pane;
                    // Inject in the student reference
                    // Use the NavigationManager to push it onto the stack
                }
            });
            optioinalSubjectsBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Create a student's optional subject pane;
                    // Inject in the student reference
                    // Use the NavigationManager to push it onto the stack
                }
            });
            changePassword.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Okay, this is one of those "questionable" options
                    // Is it better to pass the a reference of the 
                    // "authenticator" service to this class, then use a 
                    // simple dialog to get the password from the user
                    // and then pass it to the "authenticator" service
                    // OR
                    // should we present another screen?!
                    // Personally, I prefer option 1

                    JOptionPane.showMessageDialog(StudentPane.this, "All your password belong to us");
                }
            });
            profileBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Create a student's profile pane;
                    // Inject in the student reference
                    // Use the NavigationManager to push it onto the stack
                }
            });
            logoutBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    navigationController.logout(student);
                }
            });
        }
    }
}

這是一個不完整的實作,只是為了提供一個起點。

我強烈建議您查看在容器內布置組件

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/371768.html

標籤:爪哇 摇摆 框架

上一篇:在Swing中繪制超級擁擠的點

下一篇:通過單擊更改繪圖的顏色AWTJAVA

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • Git本地庫既關聯GitHub又關聯Gitee

    創建代碼倉庫 使用gitee舉例(github和gitee差不多) 1.在gitee右上角點擊+,選擇新建倉庫 ? 2.選擇填寫倉庫資訊,然后進行創建 ? 3.服務端已經準備好了,本地開始作準備 (1)Git 全域設定 git config --global user.name "成鈺" git c ......

    uj5u.com 2020-09-10 05:04:14 more
  • CODING DevOps 代碼質量實戰系列第二課,相約周三

    隨著 ToB(企業服務)的興起和 ToC(消費互聯網)產品進入成熟期,線上故障帶來的損失越來越大,代碼質量越來越重要,而「質量內建」正是 DevOps 核心理念之一。**《DevOps 代碼質量實戰(PHP 版)》**為 CODING DevOps 代碼質量實戰系列的第二課,同時也是本系列的 PHP ......

    uj5u.com 2020-09-10 05:07:43 more
  • 推薦Scrum書籍

    推薦Scrum書籍 直接上干貨,推薦書籍清單如下(推薦有順序的哦) Scrum指南 Scrum精髓 Scrum敏捷軟體開發 Scrum捷徑 硝煙中的Scrum和XP : 我們如何實施Scrum 敏捷軟體開發:Scrum實戰指南 Scrum要素 大規模Scrum:大規模敏捷組織的設計 用戶故事地圖 用 ......

    uj5u.com 2020-09-10 05:07:45 more
  • CODING DevOps 代碼質量實戰系列最后一課,周四發車

    隨著 ToB(企業服務)的興起和 ToC(消費互聯網)產品進入成熟期,線上故障帶來的損失越來越大,代碼質量越來越重要,而「質量內建」正是 DevOps 核心理念之一。 **《DevOps 代碼質量實戰(Java 版)》**為 CODING DevOps 代碼質量實戰系列的最后一課,同時也是本系列的 ......

    uj5u.com 2020-09-10 05:07:52 more
  • 敏捷軟體工程實踐書籍

    Scrum轉型想要做好,第一步先了解并真正落實Scrum,那么我推薦的Scrum書籍是要看懂并實踐的。第二步是團隊的工程實踐要做扎實。 下面推薦工程實踐書單: 重構:改善既有代碼的設計 決議極限編程 : 擁抱變化 代碼整潔代碼 程式員的職業素養 修改代碼的藝術 撰寫可讀代碼的藝術 測驗驅動開發 : ......

    uj5u.com 2020-09-10 05:07:55 more
  • Jenkins+svn+nginx實作windows環境自動部署vue前端專案

    前面文章介紹了Jenkins+svn+tomcat實作自動化部署,現在終于有空抽時間出來寫下Jenkins+svn+nginx實作自動部署vue前端專案。 jenkins的安裝和配置已經在前面文章進行介紹,下面介紹實作vue前端專案需要進行的哪些額外的步驟。 注意:在安裝jenkins和nginx的 ......

    uj5u.com 2020-09-10 05:08:49 more
  • CODING DevOps 微服務專案實戰系列第一課,明天等你

    CODING DevOps 微服務專案實戰系列第一課**《DevOps 微服務專案實戰:DevOps 初體驗》**將由 CODING DevOps 開發工程師 王寬老師 向大家介紹 DevOps 的基本理念,并探討為什么現代開發活動需要 DevOps,同時將以 eShopOnContainers 項 ......

    uj5u.com 2020-09-10 05:09:14 more
  • CODING DevOps 微服務專案實戰系列第二課來啦!

    近年來,工程專案的結構越來越復雜,需要接入合適的持續集成流水線形式,才能滿足更多變的需求,那么如何優雅地使用 CI 能力提升生產效率呢?CODING DevOps 微服務專案實戰系列第二課 《DevOps 微服務專案實戰:CI 進階用法》 將由 CODING DevOps 全堆疊工程師 何晨哲老師 向 ......

    uj5u.com 2020-09-10 05:09:33 more
  • CODING DevOps 微服務專案實戰系列最后一課,周四開講!

    隨著軟體工程越來越復雜化,如何在 Kubernetes 集群進行灰度發布成為了生產部署的”必修課“,而如何實作安全可控、自動化的灰度發布也成為了持續部署重點關注的問題。CODING DevOps 微服務專案實戰系列最后一課:**《DevOps 微服務專案實戰:基于 Nginx-ingress 的自動 ......

    uj5u.com 2020-09-10 05:10:00 more
  • CODING 儀表盤功能正式推出,實作作業資料可視化!

    CODING 儀表盤功能現已正式推出!該功能旨在用一張張統計卡片的形式,統計并展示使用 CODING 中所產生的資料。這意味著無需額外的設定,就可以收集歸納寶貴的作業資料并予之量化分析。這些海量的資料皆會以圖表或串列的方式躍然紙上,方便團隊成員隨時查看各專案的進度、狀態和指標,云端協作迎來真正意義上 ......

    uj5u.com 2020-09-10 05:11:01 more
最新发布
  • windows系統git使用ssh方式和gitee/github進行同步

    使用git來clone專案有兩種方式:HTTPS和SSH:
    HTTPS:不管是誰,拿到url隨便clone,但是在push的時候需要驗證用戶名和密碼;
    SSH:clone的專案你必須是擁有者或者管理員,而且需要在clone前添加SSH Key。SSH 在push的時候,是不需要輸入用戶名的,如果配置... ......

    uj5u.com 2023-04-19 08:41:12 more
  • windows系統git使用ssh方式和gitee/github進行同步

    使用git來clone專案有兩種方式:HTTPS和SSH:
    HTTPS:不管是誰,拿到url隨便clone,但是在push的時候需要驗證用戶名和密碼;
    SSH:clone的專案你必須是擁有者或者管理員,而且需要在clone前添加SSH Key。SSH 在push的時候,是不需要輸入用戶名的,如果配置... ......

    uj5u.com 2023-04-19 08:35:34 more
  • 2023年農牧行業6大CRM系統、5大場景盤點

    在物聯網、大資料、云計算、人工智能、自動化技術等現代資訊技術蓬勃發展與逐步成熟的背景下,數字化正成為農牧行業供給側結構性變革與高質量發展的核心驅動因素。因此,改造和提升傳統農牧業、開拓創新現代智慧農牧業,加快推進農牧業的現代化、資訊化、數字化建設已成為農牧業發展的重要方向。 當下,企業數字化轉型已經 ......

    uj5u.com 2023-04-18 08:05:44 more
  • 2023年農牧行業6大CRM系統、5大場景盤點

    在物聯網、大資料、云計算、人工智能、自動化技術等現代資訊技術蓬勃發展與逐步成熟的背景下,數字化正成為農牧行業供給側結構性變革與高質量發展的核心驅動因素。因此,改造和提升傳統農牧業、開拓創新現代智慧農牧業,加快推進農牧業的現代化、資訊化、數字化建設已成為農牧業發展的重要方向。 當下,企業數字化轉型已經 ......

    uj5u.com 2023-04-18 08:00:18 more
  • 計算機組成原理—存盤器

    計算機組成原理—硬體結構 二、存盤器 1.概述 存盤器是計算機系統中的記憶設備,用來存放程式和資料 1.1存盤器的層次結構 快取-主存層次主要解決CPU和主存速度不匹配的問題,速度接近快取 主存-輔存層次主要解決存盤系統的容量問題,容量接近與價位接近于主存 2.主存盤器 2.1概述 主存與CPU的聯 ......

    uj5u.com 2023-04-17 08:20:31 more
  • 談一談我對協同開發的一些認識

    如今各互聯網公司普通都使用敏捷開發,采用小步快跑的形式來進行專案開發。如果是小專案或者小需求,那一個開發可能就搞定了。但對于電商等復雜的系統,其功能多,結構復雜,一個人肯定是搞不定的,所以都是很多人來共同開發維護。以我曾經待過的商城團隊為例,光是后端開發就有七十多人。 為了更好地開發這類大型系統,往 ......

    uj5u.com 2023-04-17 08:18:55 more
  • 專案管理PRINCE2核心知識點整理

    PRINCE2,即 PRoject IN Controlled Environment(受控環境中的專案)是一種結構化的專案管理方法論,由英國政府內閣商務部(OGC)推出,是英國專案管理標準。
    PRINCE2 作為一種開放的方法論,是一套結構化的專案管理流程,描述了如何以一種邏輯性的、有組織的方法,... ......

    uj5u.com 2023-04-17 08:18:51 more
  • 談一談我對協同開發的一些認識

    如今各互聯網公司普通都使用敏捷開發,采用小步快跑的形式來進行專案開發。如果是小專案或者小需求,那一個開發可能就搞定了。但對于電商等復雜的系統,其功能多,結構復雜,一個人肯定是搞不定的,所以都是很多人來共同開發維護。以我曾經待過的商城團隊為例,光是后端開發就有七十多人。 為了更好地開發這類大型系統,往 ......

    uj5u.com 2023-04-17 08:18:00 more
  • 專案管理PRINCE2核心知識點整理

    PRINCE2,即 PRoject IN Controlled Environment(受控環境中的專案)是一種結構化的專案管理方法論,由英國政府內閣商務部(OGC)推出,是英國專案管理標準。
    PRINCE2 作為一種開放的方法論,是一套結構化的專案管理流程,描述了如何以一種邏輯性的、有組織的方法,... ......

    uj5u.com 2023-04-17 08:17:55 more
  • 計算機組成原理—存盤器

    計算機組成原理—硬體結構 二、存盤器 1.概述 存盤器是計算機系統中的記憶設備,用來存放程式和資料 1.1存盤器的層次結構 快取-主存層次主要解決CPU和主存速度不匹配的問題,速度接近快取 主存-輔存層次主要解決存盤系統的容量問題,容量接近與價位接近于主存 2.主存盤器 2.1概述 主存與CPU的聯 ......

    uj5u.com 2023-04-17 08:12:06 more