首先,我想说,就我的java知识而言,我是一个初学者。所以,我正在开发一个applet,使用Card Layout可以有不同的页面。我设法从在线资源中收集了一些代码,但是,我正在努力完成一些事情。我会把它分成几个问题。
如果有人能帮助我,我真的很感激。
主要课程
public class Main
{
private Menu menu;
private FirstPage page1;
private SecondPage page2;
private ThirdPage page3;
private FourthPage page4;
private void showGUI()
{
JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout());
contentPane.setPreferredSize(new Dimension(240,600));
menu = new Menu(contentPane);
page1 = new FirstPage(contentPane);
page2 = new SecondPage(contentPane);
page3 = new ThirdPage(contentPane);
page4 = new FourthPage(contentPane);
contentPane.add(menu, "Menu");
contentPane.add(page1, "First page");
contentPane.add(page2, "Second page");
contentPane.add(page3, "Third page");
contentPane.add(page4, "Fourth page");
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Main().showGUI();
}
});
}
}
菜单页面类
class Menu extends javax.swing.JPanel
{
private javax.swing.JLabel textLabel;
private JPanel contentPane;
private JButton pageOne;
private JButton pageTwo;
private JButton pageThree;
private JButton pageFour;
public Menu(JPanel cp)
{
this.contentPane = cp;
initComponents();
}
private void initComponents()
{
setBackground(Color.WHITE);
setLayout(null);
textLabel = new JLabel("This is the menu page", JLabel.CENTER);
textLabel.setForeground(Color.BLACK);
pageOne = new JButton("Flowers");
pageTwo = new JButton("Trees");
pageThree = new JButton("Rivers");
pageFour = new JButton("Seas");
pageOne.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { pageOneAction(ae); } });
pageTwo.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { pageTwoAction(ae); } });
pageThree.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { pageThreeAction(ae); } });
pageFour.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { pageFourAction(ae); } });
textLabel.setBounds(20, 20, 200, 60);
pageOne.setBounds(20, 100, 200, 60);
pageTwo.setBounds(20, 170, 200, 60);
pageThree.setBounds(20, 240, 200, 60);
pageFour.setBounds(20, 310, 200, 60);
add(pageOne);
add(pageTwo);
add(pageThree);
add(pageFour);
add(textLabel);
}
private void pageOneAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageTwoAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageThreeAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageFourAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
}
第一页课
class FirstPage extends javax.swing.JPanel
{
private javax.swing.JLabel textLabel;
private JPanel contentPane;
private JButton menu;
private JButton pageTwo;
private JButton pageThree;
private JButton pageFour;
public FirstPage(JPanel cp)
{
this.contentPane = cp;
this.setSize(500, 500);
initComponents();
}
private void initComponents()
{
setBackground(Color.WHITE);
setLayout(null);
textLabel = new JLabel("This is the flowers page", JLabel.CENTER);
textLabel.setForeground(Color.BLACK);
menu = new JButton("Back to Menu");
pageTwo = new JButton("Trees");
pageThree = new JButton("Rivers");
pageFour = new JButton("Seas");
menu.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { menuAction(ae); } });
pageTwo.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { pageTwoAction(ae); } });
pageThree.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { pageThreeAction(ae); } });
pageFour.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { pageFourAction(ae); } });
textLabel.setBounds(20, 20, 200, 60);
menu.setBounds(20, 100, 200, 60);
pageTwo.setBounds(20, 170, 200, 60);
pageThree.setBounds(20, 240, 200, 60);
pageFour.setBounds(20, 310, 200, 60);
add(menu);
add(pageTwo);
add(pageThree);
add(pageFour);
add(textLabel);
}
private void menuAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageTwoAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageThreeAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
private void pageFourAction(ActionEvent ae)
{
CardLayout layout = (CardLayout)contentPane.getLayout();
layout.next(contentPane);
}
}
其他页面与上面的页面相同。