使用单选按钮在类之间切换的Java小程序
背景信息: 这是一个Java项目,在我的学校进行本科生研究。我将于2017年4月23日(星期日)介绍本节目。我被困在程序的某个特定部分。本课程不在任何教科书中,是从头开始编写的。 在Windows10上使用EclipseNeon.2编程 说明: 这个程序是一个使用JApplet的动画窗口。将显示一个窗口,该窗口使用边框布局来组织我的面板。我有5个面板(东西两侧有2个滑块,南部有1个单选按钮,北部有一个标题,中间是动画) 问题区域是中央面板,即动画窗口所在的位置。我有三个类,每个类创建16个球的动画。一个类创建垂直反弹的球,另一个类创建水平反弹的球,最后球将在随机方向反弹 我使用一个单选按钮面板来完全改变中心面板,在点击时显示每个不同的类。默认情况下,它从垂直类开始。我可以通过注释一个类并尝试另一个类来单独显示每个类 问题: 我的单选按钮操作侦听器正在与正在单击的单选按钮正确通信但是类没有改变,面板也没有更新。按照我现在设置的方式,中心面板是空的。 最后,我希望我的中心面板能够通过单击单选按钮显示三个动画类中的任意一个 免责声明: 我没有为水平类或随机类正确编程逻辑,但是垂直类可以工作。滑块不起作用 代码如下:使用单选按钮在类之间切换的Java小程序,java,eclipse,swing,japplet,Java,Eclipse,Swing,Japplet,背景信息: 这是一个Java项目,在我的学校进行本科生研究。我将于2017年4月23日(星期日)介绍本节目。我被困在程序的某个特定部分。本课程不在任何教科书中,是从头开始编写的。 在Windows10上使用EclipseNeon.2编程 说明: 这个程序是一个使用JApplet的动画窗口。将显示一个窗口,该窗口使用边框布局来组织我的面板。我有5个面板(东西两侧有2个滑块,南部有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);
}
}
}
package-newbounceball-package;
导入javax.swing.*;
导入javax.swing.event.*;
导入java.awt.*;
导入java.awt.event.*;
公共类NEWbounceBallClass扩展了JApplet{
private DrawingPanel random ballBounce3;//中间面板,如果为random bounce
专用绘图面板水平球弹跳2;//中心面板,如果水平
//弹跳
private DrawingPanel垂直弹跳;//中心面板,如果垂直弹跳
私有JPanel title;//北
私有JPanel selectionButtons;//南部
私人JPanel ballSize;//West
私人JPanel numberOfBalls;//东
私有JSlider ballSizeSlider;//ballSize面板的组件
私有JSlider numberOfBallsSlider;//的组件网
private JRadioButton vertical;//DrawingPanelVertical的按钮
private JRadioButton horizontal;//用于DrawingPanelHorizontal的按钮
private JRadioButton random;//DrawingPanelRandom的按钮
公共void init()
{
调整大小(1150600);//调整主窗口大小
setLayout(新的BorderLayout());
buildTitlePanel();//构建北面板
buildBallBouncePanel();//调用buildSelectionButtonsPanel()
//按钮(面板)
//buildSelectionButtonsPanel();//正在调用
//buildBallBouncePanel()
buildBallSizeSlider();//构建西面板
buildNumberOfBallsSlider();//构建东面板
添加(title,BorderLayout.NORTH);//将NORTH面板添加到
//主窗口
//add(ballbunce,BorderLayout.CENTER);//将被调用
//操作侦听器内部的buildSelectionButtonsPanel()
添加(selectionButtons,BorderLayout.SOUTH);//将南面板添加到
//主窗口
添加(ballSize,BorderLayout.WEST);//将西面板添加到
//主窗口
添加(numberOfBalls,BorderLayout.EAST);//将东面板添加到
//主窗口
}
私有void buildTitlePanel()//创建北面板
{
title=新JPanel();
字体标题字体=新字体(“Serrif”,Font.BOLD,17);
JLabel titleText=新的JLabel(“弹跳球窗口”);
titleText.setFont(titleFont);
标题.添加(标题文本);
}
私有void buildBallBouncePanel()//通过调用radio创建中心面板
//钮扣
{
buildSelectionButtonsPanel();
}
public void buildSelectionButtonsPanel()//创建南面板(由调用
buildBallBouncePanel()
{
selectionButtons=新建JPanel();
垂直=新的JRadioButton(“垂直反弹”);
水平=新的JRadioButton(“水平反弹”);
随机=新的JRadioButton(“随机反弹”);
ButtonGroup=新建ButtonGroup();//对按钮进行分组
//只能选择一个
组。添加(垂直);
组。添加(水平);
组。添加(随机);
vertical.setSelected(true);//默认情况下选择垂直按钮
选择按钮。添加(垂直);
选择按钮。添加(水平);
选择按钮。添加(随机);
//ballBounce=new DrawingPanelVertical();/(测试)如果您想查看
//动画作品,取消注释第76行和第77行,
//添加(ballBounce,BorderLayout.CENTER);/(测试)这将仅显示
//垂直弹跳
vertical.addActionListener(新ActionListener()//的操作侦听器
//垂直类
{
已执行的公共无效操作(操作事件)
{
ballBounce=new DrawingPanelVertical();//调用垂直
//类然后添加到中心面板
添加(ballBounce、BorderLayout.CENTER);
系统输出打印(“垂直测试”);
}
}
);
horizontal.addActionListener(新建ActionListener()//的操作侦听器
//水平班
{
已执行的公共无效操作(操作事件)
{
ballBounce2=新建DrawingPanelHorizontal();//调用horizontal
//类然后添加到中心面板
添加(ballBounce2,BorderLayout.CENTER);
系统输出打印(“水平测试”);
}
}
);
random.addActionListener(新建ActionListener()//的操作侦听器
//随机类
{
已执行的公共无效操作(操作事件)
{
ballBounce3=newDrawingPanelRandom();//调用random类
//然后添加到中心面板
panel.add(...);
panel.revalidate(); // invokes the layout manager
panel.repaint();