如何将 JButtons 与 cardlayout 一起使用
How to use JButtons with cardlayout
我一直在尝试 UI 的乐趣,因为我对 java 还很陌生。我已经了解了卡片布局的工作原理,但我遇到了一个问题,即 JButtons 没有在我的卡片布局中显示正确的卡片。我将 ItemListeners 与 Actionlisteners 一起使用来更改卡片布局。这是我的代码...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginCards implements ItemListener, ActionListener
{
JPanel cards; //a panel that uses CardLayout
JButton login1;
JButton login2;
JButton login3;
final static String STUDENTPANEL = "Student";
final static String INSTRUCTORPANEL = "Instructor";
final static String DEPTSTAFFPANEL = "Department Staff";
final static String DEPTSTAFFOPTIONS = "";
final static String INSTRUCTOROPTIONS = "";
final static String STUDENTOPTIONS ="";
public void addComponentToPane(Container pane)
{
login1 = new JButton("Login");
login2 = new JButton("Login");
login3 = new JButton("Login");
login1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, STUDENTOPTIONS);
}
});
login2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, INSTRUCTOROPTIONS);
}
});
login3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, DEPTSTAFFOPTIONS);
}
});
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { STUDENTPANEL, INSTRUCTORPANEL, DEPTSTAFFPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JLabel("UserName: "));
card1.add(new JTextField("Student UserName", 20));
card1.add(new JLabel("Password: "));
card1.add(new JPasswordField(20));
card1.add(login1);
JPanel card2 = new JPanel();
card2.add(new JLabel("UserName: "));
card2.add(new JTextField("Instructor UserName", 20));
card2.add(new JLabel("Password: "));
card2.add(new JPasswordField(20));
card2.add(login2);
JPanel card3 = new JPanel();
card3.add(new JLabel("UserName: "));
card3.add(new JTextField("Dept. Staff UserName", 20));
card3.add(new JLabel("Password: "));
card3.add(new JPasswordField(20));
card3.add(login3);
JPanel instructorViewCard = new JPanel();
instructorViewCard.add(new JLabel("instructorViewCard"));
JPanel deptStaffViewCard = new JPanel();
deptStaffViewCard.add(new JLabel("deptStaffViewCard"));
JPanel studentViewCard = new JPanel();
studentViewCard.add(new JLabel("studentViewCard"));
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, STUDENTPANEL);
cards.add(card2, INSTRUCTORPANEL);
cards.add(card3, DEPTSTAFFPANEL);
cards.add(deptStaffViewCard, DEPTSTAFFOPTIONS);
cards.add(studentViewCard, STUDENTOPTIONS);
cards.add(instructorViewCard, INSTRUCTOROPTIONS);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent evt)
{
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("OUR System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
LoginCards demo = new LoginCards();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
/* Use an appropriate Look and Feel */
try
{
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch (UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
}
catch (IllegalAccessException ex)
{
ex.printStackTrace();
}
catch (InstantiationException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
}
如果您注意到无论您在哪个按钮(1、2 或 3)上,您总是会得到带有标签 "instructorViewCard" 的相同卡片布局。我希望能够使用 login1 按钮等查看 studentViewCard。我只是不确定这样做我缺少什么。有人看到答案了吗?有没有更好的办法?
您没有为部门员工、教师和学生选项的卡片标识符指定唯一名称...
final static String DEPTSTAFFOPTIONS = "";
final static String INSTRUCTOROPTIONS = "";
final static String STUDENTOPTIONS = "";
这基本上意味着它使用 instructorViewCard
作为每个元素(最后添加的元素)的相同视图,因为卡片由 Map
内部管理...
我一直在尝试 UI 的乐趣,因为我对 java 还很陌生。我已经了解了卡片布局的工作原理,但我遇到了一个问题,即 JButtons 没有在我的卡片布局中显示正确的卡片。我将 ItemListeners 与 Actionlisteners 一起使用来更改卡片布局。这是我的代码...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginCards implements ItemListener, ActionListener
{
JPanel cards; //a panel that uses CardLayout
JButton login1;
JButton login2;
JButton login3;
final static String STUDENTPANEL = "Student";
final static String INSTRUCTORPANEL = "Instructor";
final static String DEPTSTAFFPANEL = "Department Staff";
final static String DEPTSTAFFOPTIONS = "";
final static String INSTRUCTOROPTIONS = "";
final static String STUDENTOPTIONS ="";
public void addComponentToPane(Container pane)
{
login1 = new JButton("Login");
login2 = new JButton("Login");
login3 = new JButton("Login");
login1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, STUDENTOPTIONS);
}
});
login2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, INSTRUCTOROPTIONS);
}
});
login3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, DEPTSTAFFOPTIONS);
}
});
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { STUDENTPANEL, INSTRUCTORPANEL, DEPTSTAFFPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JLabel("UserName: "));
card1.add(new JTextField("Student UserName", 20));
card1.add(new JLabel("Password: "));
card1.add(new JPasswordField(20));
card1.add(login1);
JPanel card2 = new JPanel();
card2.add(new JLabel("UserName: "));
card2.add(new JTextField("Instructor UserName", 20));
card2.add(new JLabel("Password: "));
card2.add(new JPasswordField(20));
card2.add(login2);
JPanel card3 = new JPanel();
card3.add(new JLabel("UserName: "));
card3.add(new JTextField("Dept. Staff UserName", 20));
card3.add(new JLabel("Password: "));
card3.add(new JPasswordField(20));
card3.add(login3);
JPanel instructorViewCard = new JPanel();
instructorViewCard.add(new JLabel("instructorViewCard"));
JPanel deptStaffViewCard = new JPanel();
deptStaffViewCard.add(new JLabel("deptStaffViewCard"));
JPanel studentViewCard = new JPanel();
studentViewCard.add(new JLabel("studentViewCard"));
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, STUDENTPANEL);
cards.add(card2, INSTRUCTORPANEL);
cards.add(card3, DEPTSTAFFPANEL);
cards.add(deptStaffViewCard, DEPTSTAFFOPTIONS);
cards.add(studentViewCard, STUDENTOPTIONS);
cards.add(instructorViewCard, INSTRUCTOROPTIONS);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent evt)
{
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("OUR System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
LoginCards demo = new LoginCards();
demo.addComponentToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
/* Use an appropriate Look and Feel */
try
{
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch (UnsupportedLookAndFeelException ex)
{
ex.printStackTrace();
}
catch (IllegalAccessException ex)
{
ex.printStackTrace();
}
catch (InstantiationException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
}
如果您注意到无论您在哪个按钮(1、2 或 3)上,您总是会得到带有标签 "instructorViewCard" 的相同卡片布局。我希望能够使用 login1 按钮等查看 studentViewCard。我只是不确定这样做我缺少什么。有人看到答案了吗?有没有更好的办法?
您没有为部门员工、教师和学生选项的卡片标识符指定唯一名称...
final static String DEPTSTAFFOPTIONS = "";
final static String INSTRUCTOROPTIONS = "";
final static String STUDENTOPTIONS = "";
这基本上意味着它使用 instructorViewCard
作为每个元素(最后添加的元素)的相同视图,因为卡片由 Map
内部管理...