【问题标题】:Java Applet Using Radio Buttons to Switch Between ClassesJava Applet 使用单选按钮在类之间切换
【发布时间】:2017-04-21 01:25:58
【问题描述】:

背景信息: 这是我学校的本科生研究 Java 项目。我将在 2017 年 4 月 23 日星期日介绍这个节目。我被困在程序的特定部分。该程序不在任何教科书中,而是从头开始制作的。 在 Windows 10 上使用 Eclipse Neon.2 进行编程。

说明: 这个程序是一个使用 JApplet 的动画窗口。显示的将是一个使用边框布局来组织我的面板的窗口。我有 5 个面板(东西方两个滑块,南面 1 个单选按钮,北面一个标题,中间是动画)。

问题区域是中心面板,也就是动画窗口所在的位置。我有三个类,每个类创建一个 16 个球的动画。一类创建垂直弹跳的球,另一类创建水平弹跳的球,最后球将沿随机方向弹跳。

我正在使用单选按钮面板来完全更改中心面板,以便在单击时显示每个不同的类。默认情况下,它从垂直类开始。我可以通过注释掉一个类并与另一个类一起尝试来让每个类单独显示。

问题:

我的单选按钮动作监听器与被点击的单选按钮正确通信。 但是课程没有改变,面板也没有更新。按照我现在设置的方式,中心面板是空的。 最后,我希望我的中心面板能够通过单击单选按钮来显示三个动画类中的任何一个。

免责声明: 我没有正确编程水平或随机类的逻辑,但垂直类有效。滑块不起作用。

代码如下:

package NEWbounceBallPackage;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class NEWbounceBallClass extends JApplet{

private DrawingPanelRandom ballBounce3; //Center panel, if random bounce
private DrawingPanelHorizontal ballBounce2; //Center panel, if horizontal 
//bounce
private DrawingPanelVertical ballBounce;  //Center panel, if vertical bounce
private JPanel title; //North
private JPanel selectionButtons; //South
private JPanel ballSize; //West
private JPanel numberOfBalls; //East
private JSlider ballSizeSlider; //Component of ballSize panel
private JSlider numberOfBallsSlider; //Componenet of 
private JRadioButton vertical; //button for DrawingPanelVertical
private JRadioButton horizontal; //button for DrawingPanelHorizontal
private JRadioButton random; //button for DrawingPanelRandom

public void init()
{
    resize(1150,600); //resize main window
    setLayout(new BorderLayout());

        buildTitlePanel();  //builds north panel
        buildBallBouncePanel(); //calls buildSelectionButtonsPanel()(radio 
//buttons panel)  
        //buildSelectionButtonsPanel(); //being called in 
//buildBallBouncePanel()
        buildBallSizeSlider(); //builds west panel
        buildNumberOfBallsSlider(); //builds east panel

    add(title, BorderLayout.NORTH);                 //adds north panel to 
//main window
    //add(ballBounce, BorderLayout.CENTER);         //will be called in 
//buildSelectionButtonsPanel() inside of action listener
    add(selectionButtons, BorderLayout.SOUTH);      //adds south panel to 
//main window
    add(ballSize, BorderLayout.WEST);               //adds west panel to 
//main window
    add(numberOfBalls, BorderLayout.EAST);          //adds east panel to 
//main window
}

private void buildTitlePanel() //creates north panel
{
    title = new JPanel();

    Font titleFont = new Font("Serrif", Font.BOLD, 17);
    JLabel titleText = new JLabel("Bounce Ball Window");

    titleText.setFont(titleFont);
    title.add(titleText);
}

private void buildBallBouncePanel() //creates center panel by calling radio 
//buttons
{
    buildSelectionButtonsPanel();
}

public void buildSelectionButtonsPanel() //creates south panel (called by 
buildBallBouncePanel()
{
    selectionButtons = new JPanel();

    vertical = new JRadioButton("Vertical Bounce");
    horizontal = new JRadioButton("Horizontal Bounce");
    random = new JRadioButton("Random Bounce");

        ButtonGroup group = new ButtonGroup(); //groups buttons allowing 
//only one to be selected
            group.add(vertical);
            group.add(horizontal);
            group.add(random);

    vertical.setSelected(true);  //vertical button selected by default

    selectionButtons.add(vertical);
    selectionButtons.add(horizontal);
    selectionButtons.add(random);

    //ballBounce = new DrawingPanelVertical();  //(TEST) if you want to see 
//animation work, uncomment line 76 and 77,
    //add(ballBounce, BorderLayout.CENTER);     //(TEST) this will only show 
//the vertical bounce

    vertical.addActionListener(new ActionListener() //action listener for 
//vertical class
    {
        public void actionPerformed(ActionEvent event)
        {

                ballBounce = new DrawingPanelVertical(); //calls vertical 
//class then adds to center panel
                add(ballBounce, BorderLayout.CENTER);

                System.out.print("Vertical Test");
        }
    }
    );

    horizontal.addActionListener(new ActionListener() //action listener for 
//horizontal class
    {
        public void actionPerformed(ActionEvent event)
        {
            ballBounce2 = new DrawingPanelHorizontal(); //calls horizontal 
//class then adds to center panel
            add(ballBounce2, BorderLayout.CENTER);

            System.out.print("Horizontal Test");
        }
    }
    );

    random.addActionListener(new ActionListener() //action listener for 
//random class
    {
        public void actionPerformed(ActionEvent event)
        {
            ballBounce3 = new DrawingPanelRandom(); //calls random class 
//then adds to center panel
            add(ballBounce3, BorderLayout.CENTER);

            System.out.print("Random Test");
        }
    }
    );

}

private void buildBallSizeSlider() //creates west slider panel
{
    ballSize = new JPanel();
    ballSize.setLayout(new BorderLayout());

    ballSizeSlider = new JSlider(JSlider.VERTICAL, 1, 8, 1);
    JLabel title = new JLabel("Change Size of Ball");

        ballSizeSlider.setPreferredSize(new Dimension(50,500));
        ballSizeSlider.setMajorTickSpacing(1);
        ballSizeSlider.setMinorTickSpacing(1);
        ballSizeSlider.setPaintTicks(true);
        ballSizeSlider.setPaintLabels(true);
        //ballSizeSlider.addChangeListener(new SliderListener());

    ballSize.add(title, BorderLayout.NORTH);
    ballSize.add(ballSizeSlider, BorderLayout.CENTER);
}

//private class SliderListener implements ChangeListener
//{
//  public void stateChanged(ChangeEvent e)
//  {
    //  int sliderChoice;

    //  sliderChoice = ballSizeSlider.getValue();

    //  if(sliderChoice = )

    //}
//}

private void buildNumberOfBallsSlider() //creates east slider panel
{
    numberOfBalls = new JPanel();
    numberOfBalls.setLayout(new BorderLayout());

    numberOfBallsSlider = new JSlider(JSlider.VERTICAL, 0, 16, 8);
    JLabel title = new JLabel("Adjust Number of Balls");

        numberOfBallsSlider.setPreferredSize(new Dimension(50,500));
        numberOfBallsSlider.setMajorTickSpacing(1);
        numberOfBallsSlider.setMinorTickSpacing(1);
        numberOfBallsSlider.setPaintTicks(true);
        numberOfBallsSlider.setPaintLabels(true);

    numberOfBalls.add(title, BorderLayout.NORTH); 
    numberOfBalls.add(numberOfBallsSlider, BorderLayout.CENTER);
}
}

package NEWbounceBallPackage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DrawingPanelVertical extends JPanel{

private final int X = 135; //starting position of first ball (all other ball 
//starting positions are based from first ball)
private final int WIDTH = 40; //width of balls
private final int HEIGHT = 40; //height of balls
private final int TIME_DELAY = 40; //delays animation (increase to slow 
//down)
private final int MOVE = 20; //dictates how quickly the ball moves during 
//animation
private final int MINIMUM_Y = 50; //bottom limit of bounce
private final int MAXIMUM_Y = 400; //top limit of bounce
private int y = 400; //starting position of first ball (all other ball 
//starting positions are based from first ball)
private boolean goingUp = true;
private Timer timer;

public DrawingPanelVertical()
{
    setPreferredSize(new Dimension(800,500));
    timer = new Timer(TIME_DELAY, new TimerListener());
    timer.start();
}

public void paint(Graphics g)
{
    super.paint(g);
    g.drawRect(80, 0, 750, 500);
    g.setColor(getBackground());

        g.setColor(Color.black);
        g.fillOval(X,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.blue);
        g.fillOval(X+50,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.cyan);
        g.fillOval(X+100,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.darkGray);
        g.fillOval(X+150,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.gray);
        g.fillOval(X+200,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.green);
        g.fillOval(X+250,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.lightGray);
        g.fillOval(X+300,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.magenta);
        g.fillOval(X+350,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.orange);
        g.fillOval(X+400,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.pink);
        g.fillOval(X+450,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.red);
        g.fillOval(X+500,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.white);
        g.fillOval(X+550,  y,  WIDTH,  HEIGHT);

        g.setColor(Color.yellow);
        g.fillOval(X+600,  y,  WIDTH,  HEIGHT);
}

private class TimerListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(goingUp)
        {
            if(y > MINIMUM_Y)
                y = y - MOVE;
            else
                goingUp = false;
        }

        else
        {
            if(y < MAXIMUM_Y)
                y = y + MOVE;
            else
                goingUp = true;
        }
        //resize(1300,600);
        repaint();
        //resize(1302,600);
    }
    }
}

【问题讨论】:

  • 抱歉,我在发帖时遇到了很多麻烦。它一直说格式不正确,现在看起来很乱。附上主要和垂直面板类。任何帮助,将不胜感激。如果您希望我发布横向和随机类,请询问。
  • Err 对不起,你为什么要研究小程序?

标签: java eclipse swing japplet


【解决方案1】:

但是类没有改变,面板也没有更新。

默认情况下,Swing 组件的大小为 (0, 0),因此无需绘制任何内容。您需要调用布局管理器,以便可以根据布局管理器的规则为组件指定大小和位置。

因此,当您将组件添加到可见 GUI 时,基本逻辑是:

panel.add(...);
panel.revalidate(); // invokes the layout manager
panel.repaint();

最后,我希望我的中心面板能够通过单击单选按钮来显示三个动画类中的任何一个。

更好的解决方案是使用CardLayout 并让布局管理器管理可见面板。阅读 How to Use CardLayout 上的 Swing 教程部分,了解更多信息和工作示例。

【讨论】:

  • 哇,谢谢,效果很好。现在单击单选按钮会显示不同的类。但他们出现在彼此之上。当我开始下一节课时,我将如何结束上一节课?
  • 我也会看看卡片布局
  • How would I... - 正如我所说,更好(正确)的解决方案是使用CardLayout。除了管理每个面板的大小/位置之外,CardLayout 还管理每个面板的可见性,以便一次只有一个有效。
  • 我明白了,我感谢您的所有帮助。我会给你一个积极的投票,但我显然没有足够的声望点来投票。
  • @RyanVeitenheimer,不用担心投票,但不要忘记点击复选标记“接受”答案,这样人们就知道问题已经解决了。
猜你喜欢
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多