gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:53:33 25 4
gpt4 key购买 nike

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

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

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

}

最佳答案

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

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

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

来自java文档:

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

It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.

这样的事情应该有效:

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

关于java - 按下按钮后,它应该打开一个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28608699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com