gpt4 book ai didi

java - CardLayout - 不会切换回第一个面板

转载 作者:行者123 更新时间:2023-12-01 16:52:02 30 4
gpt4 key购买 nike

我制作了一个简单的程序,几乎可以完成所有操作,除了返回到第一个面板的开关之外。因此,如果我单击“Druck”按钮,它将把钱从一个银行帐户转移到另一个银行帐户,然后切换到第二个面板,其中显示两个帐户的余额,并且在第二个面板上还显示一个按钮,应该注意切换回第一个面板,但单击它时什么也没有发生。还有另外两个类,“Bank”和“Customer”,但我没有粘贴代码,因为我认为没有必要。我的英语不是最好的,所以如果我不清楚,请告诉我!提前致谢!

public class View extends JFrame {
private Container c;
private JPanel oben, unten, firstPanel, secondPanel;
private JLabel kontoInhaber, empfanger, bildBehalter, secondJLabel;
private JTextField jtextKontoinhaber, jtextempfanger, menge;
private JButton button, backButton;
private ImageIcon image;
private CardLayout cl;

public View (Bank bank){
c = getContentPane();
cl = new CardLayout();
oben = new JPanel();
unten = new JPanel();
firstPanel = new JPanel();
secondPanel = new JPanel();

kontoInhaber = new JLabel("Sender");
empfanger = new JLabel("Empfanger");
secondJLabel = new JLabel();

image = new ImageIcon("/Users/jetser/Desktop/ProgramozasKepek/bank.jpg");
bildBehalter = new JLabel(image);

jtextKontoinhaber = new JTextField(10);
jtextempfanger = new JTextField(10);
menge = new JTextField(10);

button = new JButton("Druck");
button.setActionCommand("uberweisen");
backButton = new JButton("Back");
backButton.setActionCommand("back");

//------------HOZZAADNI A DOLGOKAT-------------//

oben.add(bildBehalter);
oben.setBackground(Color.PINK);
unten.setLayout(new GridLayout(3,2));
unten.add(kontoInhaber);
unten.add(jtextKontoinhaber);
unten.add(empfanger);
unten.add(jtextempfanger);
unten.add(button);
unten.add(menge);
unten.setBackground(Color.PINK);

firstPanel.setLayout(new BoxLayout(firstPanel,BoxLayout.Y_AXIS));
firstPanel.add(oben);
firstPanel.add(unten);

secondPanel.add(secondJLabel);
secondPanel.add(backButton);

c.setLayout(cl);
c.add(firstPanel,"1");
c.add(secondPanel,"2");
cl.show(c,"1");

ButtonListener listener = new ButtonListener(bank,jtextKontoinhaber,jtextempfanger,menge,secondJLabel,c);
button.addActionListener(listener);
}}
<小时/>
public class ButtonListener implements ActionListener {
private Bank bank;
private JTextField einhalter, empfanger, geldMenge;
private JLabel secondLabel;
private Container c;

public ButtonListener(Bank bank, JTextField einhalter, JTextField empfanger, JTextField geldMenge, JLabel secondLabel, Container c) {
this.bank = bank;
this.einhalter = einhalter;
this.empfanger = empfanger;
this.geldMenge = geldMenge;
this.secondLabel = secondLabel;
this.c = c;
}

@Override
public void actionPerformed(ActionEvent e) {

CardLayout cl = (CardLayout) (c.getLayout());
if(e.getActionCommand().equals("uberweisen")){
cl.show(c,"2");
for(int i=0; i<bank.getKunden().length; i++){
if(einhalter.getText().equals(bank.getKunden()[i].getName())){
for(int j=0; j<bank.getKunden().length; j++){
if(empfanger.getText().equals(bank.getKunden()[j].getName())){
String geld = geldMenge.getText();
double geldMenge = Double.valueOf(geld);

bank.getKunden()[i].geldUberweisen(geldMenge);
bank.getKunden()[j].geldErhalten(geldMenge);

String output = bank.getKunden()[i].getName() + ": " + bank.getKunden()[i].getKontostand() + "\n" +
bank.getKunden()[j].getName() + ": " + bank.getKunden()[j].getKontostand();
secondLabel.setText(output);
System.out.println(output);
}
}
}
}
}
else if(e.getActionCommand().equals("back")) {
cl.show(c,"1");
}

}}

最佳答案

我重新创建了您的 GUI 并修复了您的 CardLayout 的问题.

以下是我想强调的要点。

  1. 您必须始终通过调用 SwingUtilities 来启动 Swing 应用程序。 invokeLater方法。此方法确保在 Event Dispatch Thread 上创建并执行 Swing 组件。 (美国东部时间)。

  2. 我分离了 JFrame 的创建,CardLayoutFirstPanel ,以及 SecondPanel在他们自己的类(class)里。这样,我就可以一次专注于 GUI 的一部分。

  3. 当我创建FirstPanel时类中,我按行、列顺序创建了 Swing 组件。我还将与一个 Swing 组件相关的所有方法调用放在一起。这使得测试和调试变得更加容易。

无论如何,这是我运行的代码。您需要添加不重要的 BankCustomer Action 监听器中的信息。

您可以访问JTextField来自FirstPanel通过调用CardLayoutPanel中的适当方法类和 FirstPanel类(class)。这样,我只需将一个实例传递给 ButtonListener类。

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SimpleBankProgram
implements Runnable {

public static void main(String[] args) {
SwingUtilities.invokeLater(
new SimpleBankProgram());
}

@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);

CardLayoutPanel panel =
new CardLayoutPanel();
frame.add(panel.getPanel());

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

public class CardLayoutPanel {

private CardLayout cardLayout;

private FirstPanel firstPanel;

private JPanel panel;

private SecondPanel secondPanel;

public CardLayoutPanel() {
createPartControl();
}

private void createPartControl() {
panel = new JPanel();
cardLayout = new CardLayout();
panel.setLayout(cardLayout);

firstPanel = new FirstPanel(this);
panel.add(firstPanel.getPanel(), "1");

secondPanel = new SecondPanel(this);
panel.add(secondPanel.getPanel(), "2");

showFirstPanel();
}

public void showFirstPanel() {
cardLayout.show(panel, "1");
}

public void showSecondPanel() {
cardLayout.show(panel, "2");
}

public JPanel getPanel() {
return panel;
}

public FirstPanel getFirstPanel() {
return firstPanel;
}

public SecondPanel getSecondPanel() {
return secondPanel;
}

}

public class FirstPanel {

private JPanel panel;

private JTextField jtextKontoinhaber;
private JTextField jtextempfanger;
private JTextField menge;

public FirstPanel(CardLayoutPanel
cardLayoutPanel) {
createPartControl(cardLayoutPanel);
}

private void createPartControl(CardLayoutPanel
cardLayoutPanel) {
panel = new JPanel();
panel.setLayout(new BoxLayout(
panel, BoxLayout.Y_AXIS));

JPanel oben = new JPanel();
oben.setBackground(Color.PINK);

// An image goes here
JLabel bildBehalter = new JLabel(" ");
oben.add(bildBehalter);

JPanel unten = new JPanel();
unten.setBackground(Color.PINK);
unten.setLayout(new GridLayout(3,2));

JLabel kontoInhaber = new JLabel("Sender");
unten.add(kontoInhaber);

jtextKontoinhaber = new JTextField(10);
unten.add(jtextKontoinhaber);

JLabel empfanger = new JLabel("Empfanger");
unten.add(empfanger);

jtextempfanger = new JTextField(10);
unten.add(jtextempfanger);

ButtonListener listener =
new ButtonListener(cardLayoutPanel);

JButton button = new JButton("Druck");
button.setActionCommand("uberweisen");
button.addActionListener(listener);
unten.add(button);

menge = new JTextField(10);
unten.add(menge);

panel.add(oben);
panel.add(unten);
}

public JPanel getPanel() {
return panel;
}

public JTextField getJtextKontoinhaber() {
return jtextKontoinhaber;
}

public JTextField getJtextempfanger() {
return jtextempfanger;
}

public JTextField getMenge() {
return menge;
}

}

public class SecondPanel {

private JLabel secondJLabel;

private JPanel panel;

public SecondPanel(CardLayoutPanel
cardLayoutPanel) {
createPartControl(cardLayoutPanel);
}

private void createPartControl(CardLayoutPanel
cardLayoutPanel) {
panel = new JPanel();

secondJLabel = new JLabel(" ");
panel.add(secondJLabel);

ButtonListener listener =
new ButtonListener(cardLayoutPanel);

JButton backButton = new JButton("Back");
backButton.setActionCommand("back");
backButton.addActionListener(listener);
panel.add(backButton);

}

public JPanel getPanel() {
return panel;
}

public void setJLabel(String text) {
secondJLabel.setText(text);
}
}

public class ButtonListener implements ActionListener {

private CardLayoutPanel cardLayoutPanel;

public ButtonListener(CardLayoutPanel
cardLayoutPanel) {
this.cardLayoutPanel = cardLayoutPanel;
}

@Override
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand()
.equals("uberweisen")) {
String output = "Some bank text I "
+ "can't duplicate";
cardLayoutPanel.getSecondPanel()
.setJLabel(output);
cardLayoutPanel.showSecondPanel();
} else if (event.getActionCommand()
.equals("back")) {
cardLayoutPanel.showFirstPanel();
}
}

}

}

关于java - CardLayout - 不会切换回第一个面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61666871/

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