有没有办法隐藏JFrame中的所有元素
Is there a way to hide all elements in JFrame
我目前正在开发 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("Študentský 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("Študentský 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("Študentský 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("Študentský 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("Študentský 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 window 的布局并需要使其更有效率,因此我不会将相同的代码编写数百次。
将你想要控制的组件添加到一个公共容器中,然后简单地hide/show它
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");
}
}
});
}
}
}
I think this will not do the job ...
实际上,我认为会,但是,您遗漏了一些非常重要的核心概念。
解耦
您正在使用 OO 语言进行操作,您需要关注的事情之一是“解耦”您的功能对其他功能的依赖性。问自己一个简单的问题。如果我必须修改系统的这一部分,它会产生什么后果。如果您发现自己处于一连串的变化中,那么您的系统就是紧密耦合的
单一职责
您想尽可能地隔离功能,以便它只有一项工作要做。而不是使用试图做所有事情的“上帝”class(就像你所做的那样),将你的问题分解成小块并隔离功能,这样它就可以尽可能简单地完成工作。
每个元素都应该尽可能不依赖于了解系统的当前状态,而不是被告知的,这是第一点和第三点的一部分。
依赖注入
将每个子系统需要的信息传递给它们,而不是拥有全局可达的资源。这对于测试来说确实很好,但它进一步降低了耦合度
接口代码
利用 interface
来描述高级功能概念。例如,“学生详细信息”视图不关心学生信息的结构、获取或管理方式,它只关心呈现由interface
(或合同)维护的一些信息。这将使对底层实现进行核心更改变得更容易,而不会对系统中所有想要使用它的部分产生不利影响,因为它们只依赖于 interface
[=19= 定义的“合同” ]
示例...
现在,我通常使用 CardLayout
,但是 CardLayout
不像这个问题可能需要的那样动态。
因此,我推出了自己的“导航”工作流程。这使我可以更好地控制需要注入的信息,并允许进行更动态的演示 - 您不希望在学生注销时显示填写了学生凭据的学生登录屏幕。
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("Študentský 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("Študentský 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);
}
});
}
}
}
这是一个不完整的实现,只是为了提供一个起点。
我目前正在开发 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("Študentský 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("Študentský 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("Študentský 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("Študentský 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("Študentský 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 window 的布局并需要使其更有效率,因此我不会将相同的代码编写数百次。
将你想要控制的组件添加到一个公共容器中,然后简单地hide/show它
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");
}
}
});
}
}
}
I think this will not do the job ...
实际上,我认为会,但是,您遗漏了一些非常重要的核心概念。
解耦
您正在使用 OO 语言进行操作,您需要关注的事情之一是“解耦”您的功能对其他功能的依赖性。问自己一个简单的问题。如果我必须修改系统的这一部分,它会产生什么后果。如果您发现自己处于一连串的变化中,那么您的系统就是紧密耦合的
单一职责
您想尽可能地隔离功能,以便它只有一项工作要做。而不是使用试图做所有事情的“上帝”class(就像你所做的那样),将你的问题分解成小块并隔离功能,这样它就可以尽可能简单地完成工作。
每个元素都应该尽可能不依赖于了解系统的当前状态,而不是被告知的,这是第一点和第三点的一部分。
依赖注入
将每个子系统需要的信息传递给它们,而不是拥有全局可达的资源。这对于测试来说确实很好,但它进一步降低了耦合度
接口代码
利用 interface
来描述高级功能概念。例如,“学生详细信息”视图不关心学生信息的结构、获取或管理方式,它只关心呈现由interface
(或合同)维护的一些信息。这将使对底层实现进行核心更改变得更容易,而不会对系统中所有想要使用它的部分产生不利影响,因为它们只依赖于 interface
[=19= 定义的“合同” ]
示例...
现在,我通常使用 CardLayout
,但是 CardLayout
不像这个问题可能需要的那样动态。
因此,我推出了自己的“导航”工作流程。这使我可以更好地控制需要注入的信息,并允许进行更动态的演示 - 您不希望在学生注销时显示填写了学生凭据的学生登录屏幕。
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("Študentský 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("Študentský 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);
}
});
}
}
}
这是一个不完整的实现,只是为了提供一个起点。