1
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

    public class Proyecto_Frame extends JFrame {
    private ImageIcon Aston;
    private ImageIcon Ferrari;
    private ImageIcon Lambo;
    private ImageIcon McLaren;
/**
 * 
 */
    private static final long serialVersionUID = 1L;



        public Proyecto_Frame () {
            super ("Carrera");
            this.setLayout(null);

            Aston = new ImageIcon (getClass().getResource("Aston1.png"));
            Ferrari = new ImageIcon (getClass().getResource("Ferrari1.png"));
            Lambo = new ImageIcon (getClass().getResource("Lambo1.png"));
            McLaren = new ImageIcon (getClass().getResource("McLaren1.png"));

            final JLabel Aston1 = new JLabel(Aston);
            final JLabel Ferrari1 = new JLabel(Ferrari);
            final JLabel Lambo1 = new JLabel(Lambo);
            final JLabel McLaren1 = new JLabel(McLaren);

            final JLabel Aston2 = new JLabel(Aston);
            final JLabel Ferrari2 = new JLabel(Ferrari);
            final JLabel Lambo2 = new JLabel(Lambo);
            final JLabel McLaren2 = new JLabel(McLaren);

            JButton B_Aston = new JButton (Aston);
            JButton B_Ferrari = new JButton (Ferrari);
            JButton B_Lambo = new JButton (Lambo);
            JButton B_McLaren = new JButton (McLaren);

            B_Aston.setBounds(new java.awt.Rectangle(30,30,230,75));
            this.getContentPane().add(B_Aston);

            B_Ferrari.setBounds(new java.awt.Rectangle(270,30,230,75));
            this.getContentPane().add(B_Ferrari);

            B_Lambo.setBounds(new java.awt.Rectangle(30,110,230,75));
            this.getContentPane().add(B_Lambo);

            B_McLaren.setBounds(new java.awt.Rectangle(270,110,230,75));
            this.getContentPane().add(B_McLaren);

            add(B_Aston);
            add(B_Ferrari);
            add(B_Lambo);
            add(B_McLaren);

            Aston1.setBounds(30, 250, 230, 75);
            Aston1.setVisible(false);
            Ferrari1.setBounds(30, 250, 230, 75);
            Ferrari1.setVisible(false);
            Lambo1.setBounds(30, 250, 230, 75);
            Lambo1.setVisible(false);
            McLaren1.setBounds(30, 250, 230, 75);
            McLaren1.setVisible(false);

            Aston2.setBounds(30, 350, 230, 75);
            Aston2.setVisible(false);
            Ferrari2.setBounds(30, 350, 230, 75);
            Ferrari2.setVisible(false);
            Lambo2.setBounds(30, 350, 230, 75);
            Lambo2.setVisible(false);
            McLaren2.setBounds(30, 350, 230, 75);
            McLaren2.setVisible(false);

            add(Aston1);
            add(Ferrari1);
            add(Lambo1);
            add(McLaren1);

            add(Aston2);
            add(Ferrari2);
            add(Lambo2);
            add(McLaren2);

            B_Aston.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    Aston1.setVisible(true);
                    Ferrari1.setVisible(false);
                    Lambo1.setVisible(false);
                    McLaren1.setVisible(false);
                }
            });
            B_Ferrari.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
            Aston1.setVisible(false);
            Ferrari1.setVisible(true);
            Lambo1.setVisible(false);
            McLaren1.setVisible(false);
            }
            });
            B_Lambo.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    Aston1.setVisible(false);
                    Ferrari1.setVisible(false);
                    Lambo1.setVisible(true);
                    McLaren1.setVisible(false);
                }
            });
            B_McLaren.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    Aston1.setVisible(false);
                    Ferrari1.setVisible(false);
                    Lambo1.setVisible(false);
                    McLaren1.setVisible(true);

                }

            });

        }

    }

嘿,我是 Java 新手,得到了这段代码。所以,这里的问题是,我想选择其中两个图像并将它们绘制在按钮下方以模拟比赛。当我单击任何 Jbutton 时,该图像可见,而其他 3 个不可见,但我不知道如何离开该选择并使用相同的 JButton 做出下一个选择。谁能帮我?我已经阅读了许多主题和许多其他页面,但似乎找不到答案。

谢谢,对不起格式,不习惯论坛格式。

4

3 回答 3

2

我认为最简单的方法是检查是否已经做出了第一个选择,然后进行第二个选择,例如

if (playerA == null) {
    playerA = buttonSelection;
} else {
    playerB = buttonSelection;
}

此外,您还需要一些方法来指导用户使用什么按钮选择什么。

于 2012-04-21T17:52:54.620 回答
2

一种可能的解决方案(我不是特别喜欢的一种)是使用布尔值或其他变量来保存您的程序状态,例如称为 boolean autoSelected = false;。选择自动后,将此变量更改为 true。在 JButton 的 ActionListener 中,使用 if 块根据此变量的状态确定代码应该执行的操作。换句话说,您可以根据程序的状态更改 JButton 执行的操作。

但在我看来,更好的是简单地使用 CardLayout 交换 JPanel 并显示一个 JPanel 来保存您的轨道和新的 JButton。这是因为您正在从程序的一个主要状态(设置游戏)更改为另一种状态(显示比赛),而这些可能需要在完全不同的 JPanel 中显示。

于 2012-04-21T17:53:10.080 回答
1

I would tend to use a JList with a disabled JButton Start Race below it, for this.

Put the images into the list and add a list selection listener to detect selections. When the selection changes, check the list for the selected values array (1.6) or selected values list (1.7+) and count the entries.

  1. If the number equals 1, encourage the user to pick another (using a message JLabel in the main GUI, a pop-up window or a JOptionPane).
  2. If the number equals 2, enable the button.
  3. If 3 or more are selected, disable the button and show a JOptionPane based error message to the effect "This track is not big enough for 3 cars!".
于 2012-04-22T05:08:57.120 回答