有没有一种方法可以隐藏JFrame中的所有元素



我目前正在开发Java应用程序,我正在使用JFrame,在JFrame内部我有一些按钮、标签和文本字段。我需要隐藏JFrame中的所有元素。我不需要隐藏JFrame本身,只需要隐藏其中的元素,这样我就可以显示其他元素。有没有一种方法可以在不手动编写元素的情况下将它们全部隐藏起来。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窗口的布局,需要提高它的效率,这样我就不会写数百次相同的代码了。

将要控制的组件添加到一个通用容器中,然后简单地隐藏/显示

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来描述高级功能概念。例如;学生详细信息";view并不关心学生信息是如何构建、获取或管理的,它只关心呈现interface(或合同(维护的一些信息。这将使对底层实现进行核心更改变得更容易,而不会对想要使用它的系统的所有部分产生不利影响,因为它们只依赖于";合同;由interface定义

示例

现在,我通常使用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);
}
});
}
}
}

这是一个不完整的实现,只是为了提供一个起点。

我强烈建议你看看在集装箱中布置组件

最新更新