2014-06-11 111 views
0

我正在尝试编写一个简短的程序,其中有一个带有标题和4个按钮的主页,其中3个将离开主屏幕并转到可以相应地输入信息的新页面。我通过使用拖放编辑器开始,但通过论坛发现我应该使用cardlayout,而不是我已经查看了oracle教程,但我想用按钮来更换卡片而不是组合框。那可能吗。CardLayout更改卡的按钮

这是来自教程的代码我需要更改哪个部分以使按钮成为更改屏幕的操作按钮。

感谢,

package layout; 

/* 
* CardLayoutDemo.java 
* 
*/ 
import java.awt.*; 
    import java.awt.event.*; 
import javax.swing.*; 

public class CardLayoutDemo implements ItemListener { 
    JPanel cards; //a panel that uses CardLayout 
    final static String BUTTONPANEL = "Card with JButtons"; 
    final static String TEXTPANEL = "Card with JTextField"; 

public void addComponentToPane(Container pane) { 
    //Put the JComboBox in a JPanel to get a nicer look. 
    JPanel comboBoxPane = new JPanel(); //use FlowLayout 
    String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL }; 
    JComboBox cb = new JComboBox(comboBoxItems); 
    cb.setEditable(false); 
    cb.addItemListener(this); 
    comboBoxPane.add(cb); 

    //Create the "cards". 
    JPanel card1 = new JPanel(); 
    card1.add(new JButton("Button 1")); 
    card1.add(new JButton("Button 2")); 
    card1.add(new JButton("Button 3")); 

    JPanel card2 = new JPanel(); 
    card2.add(new JTextField("TextField", 20)); 

    //Create the panel that contains the "cards". 
    cards = new JPanel(new CardLayout()); 
    cards.add(card1, BUTTONPANEL); 
    cards.add(card2, TEXTPANEL); 

    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("CardLayoutDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Create and set up the content pane. 
    CardLayoutDemo demo = new CardLayoutDemo(); 
    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(); 
     } 
    }); 
} 
} 
+2

“那可能吗?” - 当然。对于限制使用CardLayout给这些人的组合框没有任何魔力。 ''这里是来自教程的代码,我需要更改一个按钮以更改屏幕的操作按钮。“ - 您需要在JButton的ActionListener中进行更改。但不是简单地将教程代码转储到此处,您应该向我们展示**您的代码尝试**来解决此问题。否则你来这里太早。 –

+0

编辑:完全像camickr状态。 1+ –

+0

我已经编码了一些,但scrdached它所有我会开始备份开始cardlayout和转发,如果出现问题,谢谢 – AndroidNovice21

回答

2

,但我想用按钮来更改卡不是一个组合框。那可能吗。

本教程使用ItemListener作为组合框。

如果你想使用按钮,那么你会使用一个ActionListener。

ActionListener中的代码基本上是一样的。那就是你需要识别你想改变的卡。

cl.show(cards, BUTTONPANEL); // for example 

虽然因为按钮可能会显示你想切换到你可以做类似的面板的名称:

cl.show(cards, event.getActionCommand()); 

,当你点击生成ActionEvent的动作指令该按钮包含按钮上显示的文本。