按下按钮后,它应该打开一个JPanel

时间:2015-02-19 14:19:07

标签: java jpanel cardlayout

package garage;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Jela
 */
public class VehicleParts extends JPanel {

    public VehicleParts() {
     //super();
        //JPanel container = new JPanel();
        JPanel card1 = new JPanel();
        JPanel card2 = new JPanel();

        JButton buttonOne = new JButton("Parts");
        JButton buttonTwo = new JButton("Stock");
        JButton buttonThree = new JButton("Supplier");


        add(buttonOne);
        add(buttonTwo);
        add(buttonThree);


    }

}

当按下buttonOne时,它应该在同一帧上打开一个jpanel,并且应该可以返回到框架,但不知何故按钮没有显示出来。这不是我的主要课程。如果有人有一些提示,请帮忙。它应该像这样工作

股票|零件|供应商|

                         -
                         -
      PANEL  1           -
                         -
                         -
                         -
                         -
  "Previous" "NEXT"      -

========================== = 如果按下接下来的按钮,它应该转到面板2,在部件选项卡下

/ *  *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。  *要更改此模板文件,请选择“工具”|模板  *并在编辑器中打开模板。  * / 这是我的主要课程     包车库;

import java.awt.CardLayout;
import java.awt.Dimension;
import javax.swing.*;

/**
 *
 * @author Jela
 */
public class Garage extends JPanel {



  JFrame frame = new JFrame("Garage Management System");

  final static JTabbedPane tabbedPane = new JTabbedPane();
  JPanel panel = new JPanel();
  final static CustomerAccounts customerPanel = new CustomerAccounts();
  final static DiagnosisAndRepair diagnosisPanel = new DiagnosisAndRepair();
  final static ScheduledMaintenance maintenancePanel = new ScheduledMaintenance();
  final static VehicleParts partsPanel = new VehicleParts();
  final static VehicleRecords recordsPanel = new VehicleRecords();


  CardLayout cl = new CardLayout();

  public Garage(){
        tabbedPane.addTab("CustomerAccounts", customerPanel);
        tabbedPane.addTab("DiagnosisAndRepair", diagnosisPanel);
        tabbedPane.addTab("ScheduledMaintenance", maintenancePanel);
        tabbedPane.addTab("VehicleParts", partsPanel);
        tabbedPane.addTab("VehicleRecords", recordsPanel);
        //add(tabbedPane);



        frame.setSize(new Dimension(800,600));
    frame.add(tabbedPane);

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
    frame.setVisible(true);



    }





    public static void main(String[] args) {
        Garage g = new Garage();
    }

}

2 个答案:

答案 0 :(得分:1)

这是因为每次添加另一张卡时,您都会在当前卡的顶部堆叠新卡。

card1.add(buttonOne);
card1.add(buttonTwo);
card1.add(buttonThree);

执行此操作将显示添加的最后一张卡。

来自java docs:

http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html

  

它将容器中的每个组件视为卡片。一次只能看到一张卡片,而容器则充当一叠卡片。添加到CardLayout对象的第一个组件是首次显示容器时的可见组件。

这样的事情应该有效:

public class Cards() {

    private JPanel cardPanel;

    Cards() {
        cardPanel = makeCardPanel();
    }

private JPanel makeCardPanel() {
    JPanel card1 = new JPanel();

    JButton button1 = new JButton("button1");
    JButton button2 = new JButton("button1");
    JButton button3 = new JButton("button1");

    card1.add(button1);
    card1.add(button2);
    card1.add(button3);

    JPanel card2 = new JPanel();

    JButton buttonA = new JButton("buttonA");
    card2.add(buttonA);

    // Repeat the above for the cards and buttons

    JPanel cardPanel = new JPanel(new CardLayout());
    cardPanel.add(card1, "First Card");
    cardPanel.add(card2, "Second Card");

    // Add the rest of the cards.

    button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                ((CardLayout)cardPanel.getLayout()).show(cardPanel, "Second Card");


            }

        });

    buttonA.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                ((CardLayout)mainPanel.getLayout()).show(cardPanel, "First Card");


            }

        });

    return cardPanel;
}

public JPanel getCardPanel() { return cardPanel; }
}

然后在Garage()做:

public Garage(){
        Cards cards = new Cards();

        tabbedPane.addTab("CustomerAccounts", customerPanel);
        tabbedPane.addTab("DiagnosisAndRepair", diagnosisPanel);
        tabbedPane.addTab("ScheduledMaintenance", maintenancePanel);
        tabbedPane.addTab("VehicleParts", partsPanel);
        tabbedPane.addTab("VehicleRecords", recordsPanel);


        frame.setLayout(new FlowLayout());
        frame.setSize(new Dimension(800,600));
        frame.add(tabbedPane);
        frame.add(cards.getCardPanel());

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

答案 1 :(得分:0)

之后您将按钮添加到面板中,您需要执行以下操作:

frame.pack();
frame.setVisible(true);
相关问题