java按钮改变窗口大小_布局似乎有问题,JButton在调整窗口大小时显示出意外的行为。...

很好的例子的问题可能与平台有关,但我可以提供一些观察:您没有添加或删除组件,所以您不需要revalidate().

由于背景色是按钮的绑定属性,因此不需要后续调用repaint().

你,你们做需要repaint()在你的习惯里DrawingArea,但您可能需要尝试添加属性更改支持,如建议的那样。这里.

Color.white不可能brighter()和Color.black不可能darker(); Color.darkGray.darker()是Color.black().

下面的变化使用Queue以简化更改颜色。

import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;

import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;

import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.util.Arrays;

import java.util.LinkedList;import java.util.Queue;import javax.swing.BorderFactory;import javax.swing.JButton;

import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;

import javax.swing.Timer;/** @see https://stackoverflow.com/q/9849950/230513 */public class BallAnimation {

private int x;

private int y;

private boolean positiveX;

private boolean positiveY;

private boolean isTimerRunning;

private int speedValue;

private int diameter;

private DrawingArea drawingArea;

private Timer timer;

private Queue clut = new LinkedList(Arrays.asList(

Color.BLUE.darker(),

Color.MAGENTA.darker(),

Color.BLACK,

Color.RED.darker(),

Color.PINK,

Color.CYAN.darker(),

Color.DARK_GRAY,

Color.YELLOW.darker(),

Color.GREEN.darker()));

private Color backgroundColour;

private Color foregroundColour;

private ActionListener timerAction = new ActionListener() {

@Override

public void actionPerformed(ActionEvent ae) {

x = getX();

y = getY();

drawingArea.setXYColourValues(x, y, backgroundColour, foregroundColour);

}

};

private JPanel buttonPanel;

private JButton startStopButton;

private JButton speedIncButton;

private JButton speedDecButton;

private JButton resetButton;

private JButton colourButton;

private JButton exitButton;

private ComponentAdapter componentAdapter = new ComponentAdapter() {

@Override

public void componentResized(ComponentEvent ce) {

timer.restart();

startStopButton.setText("Stop");

isTimerRunning = true;

}

};

public BallAnimation() {

x = y = 0;

positiveX = positiveY = true;

speedValue = 1;

isTimerRunning = false;

diameter = 50;

backgroundColour = Color.white;

foregroundColour = clut.peek();

timer = new Timer(10, timerAction);

}

private void createAndDisplayGUI() {

JFrame frame = new JFrame("Ball Animation");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationByPlatform(true);

drawingArea = new DrawingArea(x, y, backgroundColour, foregroundColour, diameter);

drawingArea.addComponentListener(componentAdapter);

frame.add(makeButtonPanel(), BorderLayout.LINE_END);

frame.add(drawingArea, BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

private JPanel makeButtonPanel() {

buttonPanel = new JPanel(new GridLayout(0, 1));

buttonPanel.setBorder(BorderFactory.createLineBorder(Color.darkGray, 5));

startStopButton = new JButton("Start");

startStopButton.setOpaque(true);

startStopButton.setForeground(Color.white);

startStopButton.setBackground(Color.green.darker());

startStopButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent ae) {

if (!isTimerRunning) {

startStopButton.setText("Stop");

timer.start();

isTimerRunning = true;

} else if (isTimerRunning) {

startStopButton.setText("Start");

timer.stop();

isTimerRunning = false;

}

}

});

startStopButton.setBorder(BorderFactory.createLineBorder(Color.gray, 4));

buttonPanel.add(startStopButton);

colourButton = new JButton("Change Color");

colourButton.setOpaque(true);

colourButton.setForeground(Color.white);

colourButton.setBackground(clut.peek());

colourButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent ae) {

//timer.restart();

clut.add(clut.remove());

foregroundColour = clut.peek();

drawingArea.setXYColourValues(x, y, backgroundColour, foregroundColour);

colourButton.setBackground(foregroundColour);

}

});

colourButton.setBorder(BorderFactory.createLineBorder(Color.gray, 4));

buttonPanel.add(colourButton);

exitButton = new JButton("Exit");

exitButton.setBackground(Color.red);

exitButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent ae) {

timer.stop();

System.exit(0);

}

});

exitButton.setBorder(BorderFactory.createLineBorder(Color.red.darker(), 4));

buttonPanel.add(exitButton);

return buttonPanel;

}

private int getX() {

if (x 

positiveX = true;

} else if (x >= drawingArea.getWidth() - diameter) {

positiveX = false;

}

return (calculateX());

}

private int calculateX() {

if (positiveX) {

return (x += speedValue);

} else {

return (x -= speedValue);

}

}

private int getY() {

if (y 

positiveY = true;

} else if (y >= drawingArea.getHeight() - diameter) {

positiveY = false;

}

return (calculateY());

}

private int calculateY() {

if (positiveY) {

return (y += speedValue);

} else {

return (y -= speedValue);

}

}

public static void main(String... args) {

Runnable runnable = new Runnable() {

@Override

public void run() {

new BallAnimation().createAndDisplayGUI();

}

};

SwingUtilities.invokeLater(runnable);

}}class DrawingArea extends JComponent {

private int x;

private int y;

private int ballDiameter;

private Color backgroundColor;

private Color foregroundColor;

public DrawingArea(int x, int y, Color bColor, Color fColor, int dia) {

this.x = x;

this.y = y;

ballDiameter = dia;

backgroundColor = bColor;

foregroundColor = fColor;

setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 5));

}

public void setXYColourValues(int x, int y, Color bColor, Color fColor) {

this.x = x;

this.y = y;

backgroundColor = bColor;

foregroundColor = fColor;

repaint();

}

@Override

public Dimension getPreferredSize() {

return (new Dimension(500, 400));

}

@Override

public void paintComponent(Graphics g) {

g.setColor(backgroundColor);

g.fillRect(0, 0, getWidth(), getHeight());

g.setColor(foregroundColor);

g.fillOval(x, y, ballDiameter, ballDiameter);

}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/340503.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JUnit 5和Selenium –使用Selenium内置的`PageFactory`实现页面对象模式

Selenium是一组支持浏览器自动化的工具和库,主要用于Web应用程序测试。 Selenium的组件之一是Selenium WebDriver,它提供客户端库,JSON有线协议(与浏览器驱动程序进行通信的协议)和浏览器驱动程序。 Selenium WebDrive…

python 配置文件中密码不能是明文_配置文件中明文密码改为密文密码的方法

我们用java链接数据库,不管是web项目还是小程序,都需要把数据库密码写在配置文件中(当然你要写死在程序里也没有办法),或者数据库中,通常源代码漏洞扫描都会告诉你不能有明文密码,那么有什么办法可以变为密文呢&#x…

java.lang 源码剖析_java.lang.Void类源码解析

在一次源码查看ThreadGroup的时候,看到一段代码,为以下:/** throws NullPointerException if the parent argument is {code null}* throws SecurityException if the current thread cannot create a* thread in the specified thread group…

vant按需引入没样式_vue vant-ui样式出不来的问题

第一步:安装vantnpm i vant -S // 或 yarn add vant第二步:配置按需引入// 在 babel.config.js 中配置 module.exports {plugins: [[import, {libraryName: vant,libraryDirectory: es,style: true}, vant]] };第三步:配置vue.config.js&…

javaserver_如何在JavaServer Pages中使用Salesforce REST API

javaserver摘要:本教程提供了一个JSP示例以及如何将其与Salesforce REST API集成。 我们将逐步完成创建外部客户端以使用Force.com (同时使用HTTP(S)和JSON)管理数据的分步过程。 在此示例中,我将Mac OS X…

jmeter线程数并发数区别_如何确定Kafka的分区数、key和consumer线程数、以及不消费问题解决...

在Kafak中国社区的qq群中,这个问题被提及的比例是相当高的,这也是Kafka用户最常碰到的问题之一。本文结合Kafka源码试图对该问题相关的因素进行探讨。希望对大家有所帮助。怎么确定分区数?“我应该选择几个分区?”——如果你在Kaf…

简述java规范要注意哪些问题_JAVA学习:JAVA基础面试题(经典)

第一阶段题库基础知识部分:1. JDK是什么?JRE是什么?a) 答:JDK:java开发工具包。JRE:java运行时环境。2. 什么是java的平台无关性?a) 答:Java源文件被编译成字节码的形式,…

我可以/应该在事务上下文中使用并行流吗?

介绍 长话短说,您不应在并行流中使用事务。 这是因为并行流中的每个线程都有其自己的名称,因此它确实参与了事务。 Streams API旨在在某些准则下正常工作。 实际上,为了受益于并行性,不允许每个操作更改共享对象的状态&#xff0…

插入排序java_「Java」各类排序算法

排序大的分类可以分为两种:内排序和外排序。在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序。下面讲的排序都是属于内排序。内排序有可以分为以下几类:(1) 插入排…

java object... arguments_Java面试之基础题---对象Object

参数传递:Java支持两种数据类型:基本数据类型和引用数据类型。原始数据类型是一个简单的数据结构,它只有一个与之相关的值。引用数据类型是一个复杂的数据结构,它表示一个对象。原始数据类型的变量将该值直接存储在其存储器地址处…

华为光伏usb适配器_华为系列原装充电器拆解第三弹:比亚迪版华为10W充电器

在对华为18W充电器的比亚迪版和赛尔康版进行拆解之后,充电头网今天继续为大家带来华为10W充电器的比亚迪版和达宏版的拆解。这两种10W规格的华为充电器外观延续了华为原装充电器的风格,而且型号也是一样的。那么,我们先一起来看看比亚迪版华为…

JMetro版本11.5.10和8.5.10发布

在这里,我们再次使用JMetro的另一个版本。 此版本中的新增功能: 工具栏内控件的新样式 新的可编辑组合框样式 对其他样式的一些调整 一些修复 继续阅读以获取详细信息。 可编辑的ComboBox新样式 JMetro早期版本的可编辑ComboBox看起来非常糟糕&am…

1s后跳转 android_优雅保活方案,原来Android还可以这样保活

作者:NanBox保活现状我们知道,Android 系统会存在杀后台进程的情况,并且随着系统版本的更新,杀进程的力度还有越来越大的趋势。系统这种做法本身出发点是好的,因为可以节省内存,降低功耗,也避免…

java mongo api_MONGODB的javaAPI简单应用

1 建立连接要建立MongoDB的连接,你只要指定要连接到的数据库就可以。这个数据库不一定存在,如果不存在,MongoDB会先为你建立这个库。同时,在连接时你也可以具体指定要连接到的网络地址和端口。下面的是连接本机数据库的一些例子&a…

wordpress致命错误怎么解决_pppoe错误是什么意思 pppoe错误怎么解决

最近有网友反应无线路由器上设置PPPoE拨号上网后,发现PPPoE连接不上,显示pppoe错误是什么意思呢?pppoe错误怎么解决呢?接下来详细为大家介绍:pppoe错误怎么解决无线路由器设置PPPoE拨号后,PPPoE拨号连接不上,不能够上…

java ssm 多租户_(十一)java B2B2C 源码 多级分销springmvc mybatis多租户电子商城系统- SSO单点登录之OAuth2.0登录流程(2)...

上一篇是站在巨人的肩膀上去研究OAuth2.0,也是为了快速帮助大家认识OAuth2.0,闲话少说,我根据框架中OAuth2.0的使用总结,画了一个简单的流程图(根据用户名密码实现OAuth2.0的登录认证):上面的图很清楚的描述了当前登录…

hibernate 序列_Hibernate身份,序列和表(序列)生成器

hibernate 序列介绍 在上一篇文章中,我谈到了不同的数据库标识符策略。 这篇文章将比较最常见的替代主要关键策略: 身份 序列 表(序列) 身份 IDENTITY类型(包括在SQL:2003标准中)受以下支持…

java中date加1s_是否有一个java库将描述时间度量(例如“1d 1m 1s”)的字符串转换为毫秒?...

解析器不是太复杂&#xff1a;public static long parse(String input) {long result 0;String number "";for (int i 0; i < input.length(); i) {char c input.charAt(i);if (Character.isDigit(c)) {number c;} else if (Character.isLetter(c) &&…

几何画板200个经典课件_项目制学科联动 | 金芬娥首席工作室:灵动“画板”,研修创新,协同进步...

西湖区成立115个“项目制首席教师工作室”&#xff0c;建立中小学、幼儿园学科联动机制&#xff0c;以专业发展为目标&#xff0c;以教育问题为导向&#xff0c;整合发挥学科教研员、学科带头人和名师工作室领衔人的智力资源&#xff0c;助推教师的专业成长及区域的学科建设。西…

通过这些简单的步骤从头开始学习Java

Java是用于软件开发的最流行的编程语言之一。 无论您的最终目标或技能水平如何&#xff0c;学习和掌握Java都将为您作为开发人员打开大门。 今天&#xff0c;我们将讨论一些原因&#xff0c;我们认为您应该开始学习Java&#xff0c;然后提供有关入门的深入路线图。 为什么要学…