我做了一个简单的程序,几乎所有的东西都可以工作,除了向后切换到第一个面板。 因此,如果我单击“德鲁克”按钮,它将把钱从一个银行帐户转移到另一个银行帐户,然后切换到第二个面板,该面板显示两个帐户的余额,第二个面板上的按钮,应注意切换回第一个面板,但是单击该按钮后什么也没有发生。还有另外两个类,“银行”和“客户”,但是我没有粘贴代码,因为我认为这不是必需的。我的英语不是最好的,所以如果我不清楚,请告诉我! 预先感谢!
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");
}
}}
答案 0 :(得分:0)
我重新创建了GUI,并解决了CardLayout
的问题。
这是我要强调的要点。
您必须始终通过调用SwingUtilities
invokeLater
方法来启动Swing应用程序。此方法可确保在Event Dispatch Thread(EDT)上创建并执行Swing组件。
我将JFrame
,CardLayout
,FirstPanel
和SecondPanel
的创建分为各自的类。这样,我可以一次专注于GUI的一部分。
当我创建FirstPanel
类时,我以行,列顺序创建了Swing组件。我还将与一个Swing组件相关的所有方法调用放在一起。这使测试和调试变得非常容易。
无论如何,这是我运行的代码。您需要在操作侦听器中添加不重要的Bank
和Customer
信息。
您可以通过调用JTextField
类和FirstPanel
类中的适当方法来从CardLayoutPanel
访问FirstPanel
。这样,我只需要将一个实例传递给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();
}
}
}
}