所以我想做的是创建我需要的不同菜单的多个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);
}
}
答案 0 :(得分:0)
以下代码可以帮助您
JPanel mainMenu = new JPanel();
createUserMenu.setVisible(false);
JPanel createUserMenu = new JPanel();
createUserMenu.setVisible(false);
JPanel loginUserMenu = new JPanel();
loginUserMenu.setVisible(false);
的actionPerformed
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);
}
}
这不是一个完整的解决方案,只是一个例子
答案 1 :(得分:0)
使用数组列表
futures_url
设置可见性
$ sudo reboot