在 Java 中将颜色选择器添加到面板
Posted
技术标签:
【中文标题】在 Java 中将颜色选择器添加到面板【英文标题】:Add a color selector to a panel in Java 【发布时间】:2021-04-18 21:34:40 【问题描述】:我创建了一个乒乓球游戏,我想在其中实现一个颜色选择器,以便用户可以选择他们想要的背景颜色。但是,即使我能够实现颜色选择器按钮,我也无法更改背景颜色。此外,由于它是一个面板,我无法使用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
,因此在进入颜色选择器菜单后我无法返回游戏。我还尝试创建一个新的Frame
类,但它只是打开了一个单独的面板,并且不会改变已经存在的面板上的颜色。帮助将不胜感激!提前致谢!
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Main
public static void main(String[] args)
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
PongPanel pongPanel = new PongPanel();
frame.add(pongPanel, BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setVisible(true);
// new JPanelBackgroundColorTest();
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class PongPanel extends JPanel implements ActionListener, KeyListener
private boolean showTitleScreen = true;
private boolean playing = false;
private boolean gameOver = false;
private boolean upPressed = false;
private boolean downPressed = false;
private boolean rightPressed = false;
private boolean leftPressed = false;
private boolean wPressed = false;
private boolean sPressed = false;
private boolean aPressed = false;
private boolean dPressed = false;
private int ballX = 250;
private int ballY = 250;
private int diameter = 20;
private int ballDeltaX = -1;
private int ballDeltaY = 3;
private int playerOneX = 25;
private int playerOneY = 250;
private int playerOneWidth = 10;
private int playerOneHeight = 50;
private int playerTwoX = 465;
private int playerTwoY = 250;
private int playerTwoWidth = 10;
private int playerTwoHeight = 50;
private int paddleSpeed = 5;
private int playerOneScore = 0;
private int playerTwoScore = 0;
JButton button;
JLabel label;
public PongPanel()
this.setLayout(new FlowLayout());
button = new JButton("Pick a color");
button.addActionListener(this);
label = new JLabel();
label.setBackground(Color.white);
label.setOpaque(true);
this.add(button);
this.add(label);
this.setVisible(true);
setBackground(Color.BLACK);
setFocusable (true);
addKeyListener(this);
Timer timer = new Timer(1000/60, this);
timer.start();
public void actionPerformed(ActionEvent e)
step();
if(e.getSource()==button)
JColorChooser colorChooser = new JColorChooser();
Color color = JColorChooser.showDialog(null, "Pick a color", Color.black);
label.setBackground(color);
public void step()
if (playing)
if(upPressed)
if (playerOneY-paddleSpeed>0)
playerOneY -= paddleSpeed;
if(rightPressed)
if (playerOneX+paddleSpeed+playerOneWidth<getWidth())
playerOneX += paddleSpeed;
if (downPressed)
if (playerOneY+paddleSpeed+playerOneHeight<getHeight())
playerOneY += paddleSpeed;
if (leftPressed)
if (playerOneX-paddleSpeed>0)
playerOneX -= paddleSpeed;
if (wPressed)
if (playerTwoY-paddleSpeed>0)
playerTwoY -= paddleSpeed;
if (dPressed)
if (playerTwoX+paddleSpeed+playerTwoWidth<getWidth())
playerTwoX += paddleSpeed;
if (sPressed)
if (playerTwoY+paddleSpeed+playerTwoHeight<getHeight())
playerTwoY += paddleSpeed;
if (aPressed)
if (playerTwoX-paddleSpeed>0)
playerTwoX -= paddleSpeed;
int nextBallLeft = ballX + ballDeltaX;
int nextBallRight = ballX + diameter + ballDeltaX;
int nextBallTop = ballY + ballDeltaY;
int nextBallBottom = ballY + diameter + ballDeltaY;
int playerOneRight = playerOneX + playerOneWidth;
int playerOneTop = playerOneY;
int playerOneBottom = playerOneY + playerOneHeight;
float playerTwoLeft = playerTwoX;
float playerTwoTop = playerTwoY;
float playerTwoBottom = playerTwoX + playerTwoHeight;
if (nextBallTop < 0 || nextBallBottom > getHeight())
ballDeltaY *= -1;
if (nextBallLeft < playerOneRight)
if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop)
playerTwoScore ++;
if (playerTwoScore == 3)
playing = false;
gameOver = true;
ballX = 250;
ballY = 250;
else
ballDeltaX *= -1;
if (nextBallRight > playerTwoLeft)
if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop)
playerOneScore ++;
if (playerOneScore == 3)
playing = false;
gameOver = true;
ballX = 250;
ballY = 250;
else
ballDeltaX *= -1;
ballX += ballDeltaX;
ballY += ballDeltaY;
repaint();
public void paintComponent (Graphics g)
super.paintComponent (g);
g.setColor (Color.WHITE);
if (showTitleScreen)
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString("Pong", 165, 100);
g.setFont(new Font(Font.DIALOG, Font.BOLD, 18));
g.drawString("Press 'P' to play.", 175, 400);
g.drawString ("Press 'Q' to quit.", 175, 450);
else if (playing)
int playerOneRight = playerOneX + playerOneWidth;
int playerTwoLeft = playerTwoX;
for (int lineY = 0; lineY < getHeight(); lineY += 50)
g.drawLine(250, lineY, 250, lineY+25);
g.drawLine(playerOneRight, 0, playerOneRight, getHeight());
g.drawLine(playerTwoLeft, 0, playerTwoLeft, getHeight());
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString(String.valueOf(playerOneScore), 100, 100);
g.drawString(String.valueOf(playerTwoScore), 400, 100);
g.fillOval (ballX, ballY, diameter, diameter);
g.fillRect(playerOneX, playerOneY, playerOneWidth, playerOneHeight);
g.fillRect(playerTwoX, playerTwoY, playerTwoWidth, playerTwoHeight);
else if (gameOver)
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString(String.valueOf(playerOneScore), 100, 100);
g.drawString(String.valueOf(playerTwoScore), 400, 100);
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
if (playerOneScore > playerTwoScore)
g.drawString("Player 1 Wins!", 165, 200);
else
g.drawString("Player 2 Wins!", 165, 200);
g.setFont(new Font(Font.DIALOG, Font.BOLD, 18));
g.drawString("Press space to restart.", 150, 400);
public void keyTyped (KeyEvent e)
public void keyPressed (KeyEvent e)
if (showTitleScreen)
if (e.getKeyCode() == KeyEvent.VK_P)
showTitleScreen = false;
playing = true;
else if (playing)
if (e.getKeyCode() == KeyEvent.VK_UP)
upPressed = true;
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
downPressed = true;
else if (e.getKeyCode() == KeyEvent.VK_LEFT)
leftPressed = true;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
rightPressed = true;
else if (e.getKeyCode()==KeyEvent.VK_W)
wPressed = true;
else if (e.getKeyCode()==KeyEvent.VK_S)
sPressed = true;
else if (e.getKeyCode()==KeyEvent.VK_A)
aPressed = true;
else if (e.getKeyCode()==KeyEvent.VK_D)
dPressed = true;
else if (e.getKeyCode() == KeyEvent.VK_Q)
showTitleScreen = true;
playing = false;
else if (gameOver)
if(e.getKeyCode() == KeyEvent.VK_SPACE)
gameOver = false;
showTitleScreen = true;
playerOneY = 250;
playerTwoY = 250;
playerOneX = 25;
playerTwoX = 465;
ballX = 250;
ballY = 250;
playerOneScore = 0;
playerTwoScore = 0;
public void keyReleased (KeyEvent e)
if (e.getKeyCode()==KeyEvent.VK_UP)
upPressed = false;
else if (e.getKeyCode()==KeyEvent.VK_DOWN)
downPressed = false;
else if (e.getKeyCode()==KeyEvent.VK_LEFT)
leftPressed = false;
else if (e.getKeyCode()==KeyEvent.VK_RIGHT)
rightPressed = false;
else if (e.getKeyCode()==KeyEvent.VK_W)
wPressed = false;
else if (e.getKeyCode()==KeyEvent.VK_S)
sPressed = false;
else if (e.getKeyCode()==KeyEvent.VK_A)
aPressed = false;
else if (e.getKeyCode()==KeyEvent.VK_D)
dPressed = false;
【问题讨论】:
首先,我建议看看How to Use Color Choosers。让JavaDocs 也可用也非常有用。你会发现JColorChooser
有几个JColorChooser#showDialog
说服方法,它们会在对话框中为你显示选择器
我还建议您熟悉 How to Make Dialogs 和 How to Use CardLayout,具体取决于您希望 UI 的工作方式
这可能对你有用:sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/ex/…
【参考方案1】:
首先查看How to Use Color Choosers。
让JavaDocs 也可用也非常有用。您会发现JColorChooser
有几个JColorChooser#showDialog
说服方法,它们会在对话框中为您显示选择器。
例如...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Main
public static void main(String[] args)
new Main();
public Main()
EventQueue.invokeLater(new Runnable()
@Override
public void run()
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
);
public class TestPane extends JPanel
public TestPane()
setLayout(new BorderLayout());
setBorder(new EmptyBorder(16, 16, 16, 16));
JButton chooseColor = new JButton("Choose Color");
add(chooseColor);
chooseColor.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
Color color = JColorChooser.showDialog(TestPane.this, "Make a choice", Color.MAGENTA);
if (color != null)
setBackground(color);
);
我还建议您熟悉 How to Make Dialogs 和 How to Use CardLayout,具体取决于您希望 UI 的工作方式
【讨论】:
以上是关于在 Java 中将颜色选择器添加到面板的主要内容,如果未能解决你的问题,请参考以下文章
CircleImage 在选择器中选择一个选项并添加到列表后不断改变颜色
如何在grapesjs的默认富文本编辑器上添加颜色选择器来更改字体颜色?