我目前正在開發 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中繪制超級擁擠的點
