java中切换卡怎样实现,Java使用jbutton在卡之间切换

我正在使用卡片布局,因此要使第一张卡片具有一个按钮,单击该按钮会将其带到卡片2,卡片2具有一个按钮将其带回到卡片1.这是我当前的代码,以及我曾尝试在actionPerformed方法中放一些东西,但在使它正常工作方面没有取得任何成功.另外,在button1.addActionListener(this);的行上,我在“ this”上收到语法错误.和button2.addActionListener(this);我认为这是因为我的actionPerformed方法未正确设置.任何对按钮设置的帮助将不胜感激.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Main implements ItemListener {

JPanel cards;

public void addComponentToPane(Container pane) {

//create cards

JPanel card1 = new JPanel();

JPanel card2 = new JPanel();

JButton button1 = new JButton("Button 1");

JButton button2 = new JButton("Button 2");

button1.addActionListener(this);

button2.addActionListener(this);

card1.add(button1);

card2.add(button2);

//create panel that contains cards

cards = new JPanel(new CardLayout());

cards.add(card1);

cards.add(card2);

pane.add(cards, BorderLayout.CENTER);

}

public void itemStateChanged(ItemEvent evt) {

CardLayout cl = (CardLayout)(cards.getLayout());

cl.show(cards, (String)evt.getItem());

}

public static void createAndShowGUI() {

//create and setup window

JFrame frame = new JFrame("Frame");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//create and setup content pane

Main main = new Main();

main.addComponentToPane(frame.getContentPane());

//display window

frame.pack();

frame.setVisible(true);

}

public void actionPerformed(ActionEvent ae) {

}

public static void main(String[] args) {

//set look and feel

try {

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

} catch (UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

} catch (IllegalAccessException ex) {

ex.printStackTrace();

} catch (InstantiationException ex) {

ex.printStackTrace();

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

}

//turn off metal's bold fonts

UIManager.put("swing.boldMetal", Boolean.FALSE);

//schedule job for the event dispatch thread creating and showing GUI

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

最佳答案

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CardTest implements ActionListener {

private JPanel cards;

private JButton button1;

private JButton button2;

public void addComponentToPane(Container pane) {

// create cards

JPanel card1 = new JPanel();

JPanel card2 = new JPanel();

button1 = new JButton("Button 1");

button2 = new JButton("Button 2");

button1.addActionListener(this);

button2.addActionListener(this);

card1.add(button1);

card2.add(button2);

// create panel that contains cards

cards = new JPanel(new CardLayout());

cards.add(card1, "Card 1");

cards.add(card2, "Card 2");

pane.add(cards, BorderLayout.CENTER);

}

public void itemStateChanged(ItemEvent evt) {

CardLayout cl = (CardLayout) (cards.getLayout());

cl.show(cards, (String) evt.getItem());

}

public static void createAndShowGUI() {

// create and setup window

JFrame frame = new JFrame("Frame");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// create and setup content pane

CardTest main = new CardTest();

main.addComponentToPane(frame.getContentPane());

// display window

frame.pack();

frame.setVisible(true);

}

public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == button1) {

CardLayout cl = (CardLayout) (cards.getLayout());

cl.show(cards, "Card 2");

} else if (ae.getSource() == button2) {

CardLayout cl = (CardLayout) (cards.getLayout());

cl.show(cards, "Card 1");

}

}

public static void main(String[] args) {

// set look and feel

try {

UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

} catch (UnsupportedLookAndFeelException ex) {

ex.printStackTrace();

} catch (IllegalAccessException ex) {

ex.printStackTrace();

} catch (InstantiationException ex) {

ex.printStackTrace();

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

}

// turn off metal's bold fonts

UIManager.put("swing.boldMetal", Boolean.FALSE);

// schedule job for the event dispatch thread creating and showing GUI

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值