[英]Controlling multiple JPanels visibility in a JFrame
所以我想要做的是創建我需要的不同菜單的多個 jpanel。 起初,只有主菜單的可見性設置為 true。 然后根據我單擊的按鈕,我可以通過關閉上一個菜單的可見性並打開當前菜單的可見性來轉到不同的菜單。 但是它不起作用,我認為這與我添加 JPanels 的方式有關。 有什么快速解決辦法嗎?
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.opencsv.CSVWriter;
public class justawindow extends JFrame{
public static void main (String[] args){
justawindow w = new justawindow();
}
//JPanels
JPanel mainMenu = new JPanel();
JPanel createUserMenu = new JPanel();
JPanel loginUserMenu = new JPanel();
//mainMenu stuff
JButton createMenu = new JButton("Create new user");
JButton loginMenu = new JButton("Login");
//createUserMenu stuff
JTextField username = new JTextField(15);
JPasswordField password = new JPasswordField(15);
JTextField realname = new JTextField(15);
JButton backFromCreateUser = new JButton("Back");
JButton createUser = new JButton("Create new user");
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
JLabel realnameLabel = new JLabel("Real name:");
JButton login = new JButton("Login");
JButton backFromLoginUser = new JButton ("Back");
public justawindow(){
createMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
mainMenu.setVisible(false);
createUserMenu.setVisible(true);
}
});
loginMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
mainMenu.setVisible(false);
loginUserMenu.setVisible(true);
}
});
GridBagLayout g1 = new GridBagLayout();
GridBagConstraints c1 = new GridBagConstraints();
mainMenu.setLayout(g1);
c1.gridx = 0;
c1.gridy = 0;
mainMenu.add(createMenu,c1);
c1.gridy = 1;
mainMenu.add(loginMenu,c1);
mainMenu.setVisible(true);
GridBagLayout g2 = new GridBagLayout();
GridBagConstraints c2 = new GridBagConstraints();
createUserMenu.setLayout(g2);
c2.gridx = 0;
c2.gridy = 0;
createUserMenu.add(usernameLabel,c2);
c2.gridx = 1;
createUserMenu.add(username,c2);
c2.gridx = 0;
c2.gridy = 1;
createUserMenu.add(passwordLabel,c2);
c2.gridx = 1;
createUserMenu.add(password,c2);
c2.gridx = 0;
c2.gridy = 2;
createUserMenu.add(realnameLabel,c2);
c2.gridx = 1;
createUserMenu.add(realname,c2);
c2.gridx = 0;
c2.gridy = 3;
createUserMenu.add(createUser,c2);
c2.gridy = 4;
createUserMenu.add(backFromCreateUser,c2);
createUserMenu.setVisible(false);
GridBagLayout g3 = new GridBagLayout();
GridBagConstraints c3 = new GridBagConstraints();
loginUserMenu.setLayout(g3);
c3.gridx = 0;
c3.gridy = 0;
loginUserMenu.add(usernameLabel,c3);
c3.gridx = 1;
loginUserMenu.add(username,c3);
c3.gridx = 0;
c3.gridy = 1;
loginUserMenu.add(passwordLabel,c3);
c3.gridx = 1;
loginUserMenu.add(password,c3);
c3.gridx = 0;
c3.gridy = 2;
loginUserMenu.add(login,c3);
c3.gridy = 3;
loginUserMenu.add(backFromLoginUser,c3);
loginUserMenu.setVisible(false);
this.add(createUserMenu);
this.add(loginUserMenu);
this.add(mainMenu);
setSize(400,500);
setTitle("Bomberman");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
以下代碼可能對您有所幫助
JPanel mainMenu = new JPanel();
createUserMenu.setVisible(false);
JPanel createUserMenu = new JPanel();
createUserMenu.setVisible(false);
JPanel loginUserMenu = new JPanel();
loginUserMenu.setVisible(false);
已執行的操作
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
mainMenu.setVisible(true);
createUserMenu.setVisible(false);
loginUserMenu.setVisible(false);
}
else if(e.getSource()==btn2)
{
mainMenu.setVisible(false);
createUserMenu.setVisible(true);
loginUserMenu.setVisible(false);
}
else if(e.getSource()==btn3)
{
mainMenu.setVisible(false);
createUserMenu.setVisible(false);
loginUserMenu.setVisible(true);
}
}
這不是一個完整的解決方案,只是一個例子
使用數組列表
components1 = new ArrayList<Component>();
components1.add(label11);
components1.add(label12);
設置可見性
panel1.setVisible(true);
for (Component component1 : components1) {
component1.setVisible(true);
}
panel2.setVisible(false);
for (Component component2 : components2) {
component2.setVisible(false);
}
panel3.setVisible(false);
for (Component component : components3) {
component.setVisible(false);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.