gpt4 book ai didi

java - 可以动java的JFrame和Block的背景

转载 作者:行者123 更新时间:2023-12-01 11:56:38 24 4
gpt4 key购买 nike

我有 2 个问题。如何为 JFrame 制作背景颜色以及如何制作可以移动的 block 。

我正在摆弄按钮,我想为我的 Jframe 制作背景颜色,所以我尝试了 .setBackground() 和 .getContentPane().setBackground(),但似乎都不起作用

我还想在 JFrame 的左上角初始化一个小球,可以使用箭头键移动它,并在点击其中一个按钮时触发一个新窗口。我知道如何编写碰撞代码,只是不知道如何制作球并使其与 WASD 或箭头键关联移动。

这是我到目前为止所拥有的:

import java.awt.*;
import javax.swing.*;
import javax.swing.*;
public class idk
{
public static void main (String args[])
{
JFrame frame = new JFrame("Button test 1");
frame.setSize(1680, 1000);
//the next line won't do anything
frame.setBackground(Color.RED);

JPanel panel = new JPanel();

JButton buttonOne = new JButton("Button" + "" + "1");
buttonOne.setBounds( 100, 100 ,200,100);
buttonOne.setBackground(Color.BLACK);
buttonOne.setForeground(Color.WHITE);

JButton buttonTwo = new JButton("Button" + "" + "2");
buttonTwo.setBounds( 740, 100 ,200,100);
buttonTwo.setBackground(Color.BLACK);
buttonTwo.setForeground(Color.WHITE);

JButton buttonThree = new JButton("Button" + "" + "3");
buttonThree.setBounds( 1380, 100 ,200,100);
buttonThree.setBackground(Color.BLACK);
buttonThree.setForeground(Color.WHITE);

JButton buttonFour = new JButton("Button" + "" + "4");
buttonFour.setBounds( 100, 450 ,200,100);
buttonFour.setBackground(Color.BLACK);
buttonFour.setForeground(Color.WHITE);

JButton buttonFive = new JButton("Button" + "" + "5");
buttonFive.setBounds( 740, 450 ,200,100);
buttonFive.setBackground(Color.BLACK);
buttonFive.setForeground(Color.WHITE);

JButton buttonSix = new JButton("Button" + "" + "6");
buttonSix.setBounds( 1380, 450 ,200,100);
buttonSix.setBackground(Color.BLACK);
buttonSix.setForeground(Color.WHITE);

JButton buttonSeven = new JButton("Button" + "" + "7");
buttonSeven.setBounds( 100, 800 ,200,100);
buttonSeven.setBackground(Color.BLACK);
buttonSeven.setForeground(Color.WHITE);

JButton buttonEight = new JButton("Button" + "" + "8");
buttonEight.setBounds( 740, 800 ,200,100);
buttonEight.setBackground(Color.BLACK);
buttonEight.setForeground(Color.WHITE);

JButton buttonNine = new JButton("Button" + "" + "9");
buttonNine.setBounds( 1380, 800 ,200,100);
buttonNine.setBackground(Color.BLACK);
buttonNine.setForeground(Color.WHITE);

panel.setLayout(null);

panel.add(buttonOne);
panel.add(buttonTwo);
panel.add(buttonThree);
panel.add(buttonFour);
panel.add(buttonFive);
panel.add(buttonSix);
panel.add(buttonSeven);
panel.add(buttonEight);
panel.add(buttonNine);

frame.add(panel);

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

最佳答案

  • 避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计目的是与核心的布局管理器一起工作,放弃这些将导致无休止的问题和问题,您将花费越来越多的时间来尝试纠正
  • 您有一个位于框架顶部的 JPanel,遮挡了其后面的任何内容。在这种情况下,请考虑更改面板的背景颜色
  • 为了在屏幕上“移动”一个 block ,您可能需要深入研究自定义绘画,请参阅 Painting in AWT and Swing , Performing Custom Painting2D Graphics了解更多详情
  • 对于按键通知,您可以使用按键绑定(bind) API 获得最佳结果,与旧版 KeyListener API 相比,该 API 具有许多优势。请参阅How to Use Key Bindings了解更多详情
  • 如果没有背后的功能,您的按钮将毫无用处,请参阅 How to Write an Action Listeners , How to Use Buttons, Check Boxes, and Radio ButtonsHow to Use Actions了解更多详情

类似于 How to change images based on keystrokesI am trying to make ball gradually move例如

关于java - 可以动java的JFrame和Block的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28400737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com