Java 小程序使用单选按钮在 类 之间切换
Java Applet Using Radio Buttons to Switch Between Classes
背景资料:
这是我学校本科生研究的 Java 项目。我将在 2017 年 4 月 23 日星期日介绍这个节目。我被困在程序的特定部分。该程序不在任何教科书中,是从头开始编写的。
在 Windows 10.
上使用 Eclipse Neon.2 编程
描述:
这个程序是一个使用 JApplet 的动画 window。将显示一个 window,它使用边框布局来组织我的面板。我有5个面板(东西2个滑块,南1个单选按钮,北1个标题,中间是动画)。
问题区域是中心面板,这是动画 window 所在的位置。我有三个 classes,每个都创建一个包含 16 个球的动画。一个 class 创建垂直弹跳的球,另一个水平弹跳的球,最后球将在随机方向弹跳。
我正在使用单选按钮面板来完全更改中心面板,以便在单击时显示每个不同的 class。它默认从垂直 class 开始。我可以通过注释掉一个 class 并尝试另一个来让每个 class 单独显示。
问题:
我的单选按钮动作侦听器与被单击的单选按钮正确通信。 但是 class 没有改变,面板也没有更新。 按照我现在的设置方式,中间面板是空的。
最后,我想让我的中心面板能够通过单击单选按钮显示三个动画中的任何一个 classes。
免责声明:
我没有正确编程水平或随机 class 的逻辑,但垂直 class 有效。滑块不起作用。
代码如下:
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);
}
}
}
But the class is not changing and the panel is not being updated.
默认情况下,Swing 组件的大小为 (0, 0),因此无需绘制任何内容。您需要调用布局管理器,以便根据布局管理器的规则为组件指定大小和位置。
因此,当您将组件添加到可见 GUI 时,基本逻辑是:
panel.add(...);
panel.revalidate(); // invokes the layout manager
panel.repaint();
In the end i want to have my center panel able to display any of the three animation classes by clicking a radio button.
更好的解决方案是使用 CardLayout
并让布局管理器管理可见面板。阅读有关 How to Use CardLayout 的 Swing 教程部分,了解更多信息和工作示例。
背景资料: 这是我学校本科生研究的 Java 项目。我将在 2017 年 4 月 23 日星期日介绍这个节目。我被困在程序的特定部分。该程序不在任何教科书中,是从头开始编写的。 在 Windows 10.
上使用 Eclipse Neon.2 编程描述: 这个程序是一个使用 JApplet 的动画 window。将显示一个 window,它使用边框布局来组织我的面板。我有5个面板(东西2个滑块,南1个单选按钮,北1个标题,中间是动画)。
问题区域是中心面板,这是动画 window 所在的位置。我有三个 classes,每个都创建一个包含 16 个球的动画。一个 class 创建垂直弹跳的球,另一个水平弹跳的球,最后球将在随机方向弹跳。
我正在使用单选按钮面板来完全更改中心面板,以便在单击时显示每个不同的 class。它默认从垂直 class 开始。我可以通过注释掉一个 class 并尝试另一个来让每个 class 单独显示。
问题:
我的单选按钮动作侦听器与被单击的单选按钮正确通信。 但是 class 没有改变,面板也没有更新。 按照我现在的设置方式,中间面板是空的。 最后,我想让我的中心面板能够通过单击单选按钮显示三个动画中的任何一个 classes。
免责声明: 我没有正确编程水平或随机 class 的逻辑,但垂直 class 有效。滑块不起作用。
代码如下:
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);
}
}
}
But the class is not changing and the panel is not being updated.
默认情况下,Swing 组件的大小为 (0, 0),因此无需绘制任何内容。您需要调用布局管理器,以便根据布局管理器的规则为组件指定大小和位置。
因此,当您将组件添加到可见 GUI 时,基本逻辑是:
panel.add(...);
panel.revalidate(); // invokes the layout manager
panel.repaint();
In the end i want to have my center panel able to display any of the three animation classes by clicking a radio button.
更好的解决方案是使用 CardLayout
并让布局管理器管理可见面板。阅读有关 How to Use CardLayout 的 Swing 教程部分,了解更多信息和工作示例。