【问题标题】:after pressing a button it should open a JPanel按下按钮后,它应该打开一个 JPanel
【发布时间】:2015-02-19 14:19:06
【问题描述】:
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();
    }

}

【问题讨论】:

    标签: java jpanel cardlayout


    【解决方案1】:

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

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

    这样做会显示最后添加的卡片。

    来自 java 文档:

    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);
        }
    

    【讨论】:

    • 如何避免覆盖它?
    • 我想要 card1 中的三个按钮
    • 试过你的方法,但运行时屏幕上似乎没有任何内容
    • 请记住,在卡片布局中,您正在添加卡片,并且必须明确显示您感兴趣的卡片。至于没有出现,您是否在框架中添加了mainPanel?跨度>
    • 如果我将 mainPanel 添加到我的框架中,那么 tabbedPane 将被覆盖
    【解决方案2】:

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

    frame.pack();
    frame.setVisible(true);
    

    【讨论】:

      猜你喜欢
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      • 2022-09-24
      • 2015-04-04
      相关资源
      最近更新 更多