- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
很抱歉,如果这个问题已经在某个时候被问过。我正在尝试用 Java 实现一个 JSlider,它将在 GUI 中增大和缩小图标。到目前为止,我已经开发了 GUI 并实现了 slider 等,但是当我尝试移动 slider 时程序返回“AWT-EventQueue-0”NullPointerExceptions(调用再次绘制对象。下面我复制了代码GUI 和其中使用的 CarIcon 类。感谢您的帮助!抱歉,如果我在这里犯了任何明显的错误,我是 GUI 编码的新手。
这是 slider GUI:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.Timer;
public SliderGui(){
}
public void initGUI() {
JFrame frame = new JFrame("Slider");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setPreferredSize(new Dimension(F_WIDTH, F_HEIGHT));
frame.setLayout(new BorderLayout());
CarIcon initI = new CarIcon(DEFAULT_SIZE);
this.imagePan = new JLabel(initI);
frame.add(imagePan, BorderLayout.CENTER);
this.setSlider(frame);
}
public void setSlider(JFrame frame) {
JSlider sizeSlider = new JSlider(JSlider.VERTICAL, MIN_SIZE,
MAX_SIZE, DEFAULT_SIZE);
sizeSlider.setMajorTickSpacing(10);
sizeSlider.setMinorTickSpacing(5);
sizeSlider.setPaintTicks(true);
sizeSlider.setPaintLabels(true);
sizeSlider.addChangeListener((ChangeEvent e) -> {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int level = (int)source.getValue();
this.icon.setWidth(level);
this.icon.paintIcon(imagePan, this.g, x, y);
}
});
frame.add(sizeSlider, BorderLayout.WEST);
}
public static void main(String[] args) {
SliderGui test = new SliderGui();
test.initGUI();
}
public CarIcon icon;
public Graphics2D g;
private JLabel imagePan;
static final int x = 350;
static final int y = 350;
static final int F_WIDTH = 700;
static final int F_HEIGHT = 700;
static final int MAX_SIZE = 200;
static final int MIN_SIZE = 5;
static final int DEFAULT_SIZE = 75;
}
这是 CarIcon 类:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class CarIcon implements Icon
{
public CarIcon(int aWidth)
{
width = aWidth;
}
public void setWidth(int aWidth) {
this.width = aWidth;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return width / 2;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
Graphics2D g2 = (Graphics2D) g;
Rectangle2D.Double body
= new Rectangle2D.Double(x, y + width / 6,
width - 1, width / 6);
Ellipse2D.Double frontTire
= new Ellipse2D.Double(x + width / 6, y + width / 3,
width / 6, width / 6);
Ellipse2D.Double rearTire
= new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
width / 6, width / 6);
// The bottom of the front windshield
Point2D.Double r1
= new Point2D.Double(x + width / 6, y + width / 6);
// The front of the roof
Point2D.Double r2
= new Point2D.Double(x + width / 3, y);
// The rear of the roof
Point2D.Double r3
= new Point2D.Double(x + width * 2 / 3, y);
// The bottom of the rear windshield
Point2D.Double r4
= new Point2D.Double(x + width * 5 / 6, y + width / 6);
Line2D.Double frontWindshield
= new Line2D.Double(r1, r2);
Line2D.Double roofTop
= new Line2D.Double(r2, r3);
Line2D.Double rearWindshield
= new Line2D.Double(r3, r4);
g2.fill(frontTire);
g2.fill(rearTire);
g2.setColor(Color.red);
g2.fill(body);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
}
private int width;
}
最佳答案
问题:
paintIcon(...)
直接绘制图标。让 Java 自己来做这件事。只需在更改图标宽度后在保存图标的 JLabel 上调用 repaint()
- 就是这样!我是这样测试的:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public class ResizeIcon extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private static final int MAX_ICON_WIDTH = 400;
private int iconWidth = MAX_ICON_WIDTH / 2;
private CarIcon carIcon = new CarIcon(iconWidth);
private JLabel carLabel = new JLabel(carIcon);
private JSlider slider = new JSlider(0, MAX_ICON_WIDTH, iconWidth);
public ResizeIcon() {
slider.setMajorTickSpacing(50);
slider.setMinorTickSpacing(10);
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setPaintTrack(true);
slider.setSnapToTicks(true);
slider.addChangeListener(new SliderListener());
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout());
add(slider, BorderLayout.PAGE_START);
add(carLabel, BorderLayout.CENTER);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class SliderListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
carIcon.setWidth(value);
carLabel.repaint();
}
}
private static void createAndShowGui() {
ResizeIcon mainPanel = new ResizeIcon();
JFrame frame = new JFrame("Resize Icon");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class CarIcon implements Icon {
public CarIcon(int aWidth) {
width = aWidth;
}
public void setWidth(int aWidth) {
this.width = aWidth;
}
public int getIconWidth() {
return width;
}
public int getIconHeight() {
return width / 2;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g;
Rectangle2D.Double body = new Rectangle2D.Double(x, y + width / 6, width - 1, width / 6);
Ellipse2D.Double frontTire = new Ellipse2D.Double(x + width / 6, y + width / 3, width / 6,
width / 6);
Ellipse2D.Double rearTire = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
width / 6, width / 6);
// The bottom of the front windshield
Point2D.Double r1 = new Point2D.Double(x + width / 6, y + width / 6);
// The front of the roof
Point2D.Double r2 = new Point2D.Double(x + width / 3, y);
// The rear of the roof
Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3, y);
// The bottom of the rear windshield
Point2D.Double r4 = new Point2D.Double(x + width * 5 / 6, y + width / 6);
Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
Line2D.Double roofTop = new Line2D.Double(r2, r3);
Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
g2.fill(frontTire);
g2.fill(rearTire);
g2.setColor(Color.red);
g2.fill(body);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
}
private int width;
}
关于java - Jslider 增长和收缩图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37233423/
我已从使用 JFrame 的项目中复制代码以显示文本字段和 JSlider 我的新项目使用了JInternalFrame,并且只绘制了 slider 。如果我注释掉代码,它将绘制文本字段,似乎它只会绘
我正在测试一个使用 JSlider 调整圆宽度的程序,并且 slider 的值正在工作,但它实际上并没有改变“宽度”变量。请帮忙!!!这是我到目前为止所拥有的: public class Slider
我的 JSlider 第一个值为 0,最后一个值为 8850。我想设置主要刻度间距,以便打印最后一个值 (8850)。例如,如果我将主刻度间距设置为 550,则最后打印的值将为 8800。 最佳答案
我想在单击按钮时动态添加文本字段,但要获取的值和按钮位于一个类中,而我想要添加与其相邻的文本框和 slider 的面板位于不同的类中。代码是- public class TipSplitting ex
我第一次尝试使用JSlider,并尝试咨询How to Use Sliders学习基础知识。但是当我尝试使刻度线显示为这样时: sliderInterior = new JSlider(JSlide
此 jSlider 代码导致应用程序挂起。 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //
我正在学习如何使用 oracle tutorials 中的 JSlider 。我想用它来更改代码中变量的值,而不是更改动画的速度/fps。我注意到我在电脑游戏中看到的“每秒帧数”并阅读了 here 。
我正在为模拟创建一个简单的“播放/倒带”界面,其中的 JSlider 既可用作进度条,也可用作 slider ,您可以将模拟移动到模拟范围内的任何时间点。滑动到时间点效果很好,但是当模拟“播放”时,我
这是我更新 jslider 的代码 try{ name=(String) jList1.getSelectedValue(); CanvasVideoSurface video
这个问题已经有答案了: Update JLabel repeatedly with results of long running task (2 个回答) 已关闭 8 年前。 我有一个 Java S
我有一个 wav 音频文件。我知道如何在java中使用AudioImputStream来播放它。我学会了创建 jslider。现在我要使用 slider 作为带有音频的进度条,比如音频播放,那么 sl
我刚刚开始使用 java :)。 我的问题是: 我想用 JSlider 制作一个 GUI,然后 slider 移动,我希望 JTextField 中的数字发生变化。我尝试了很多东西,但总是出错。 如果
是否可以在 JSlider 上显示两组具有相同刻度的标签,一组在顶部,一组在底部? 最佳答案 您可以实现自己的SliderUI,如图here ,并覆盖 paintTicks()。 或者,您可能试图表示
我在 Java 中使用 JSlider 时遇到问题 我画了一个圆 A,我想在第一个圆 A 内放置另一个圆 B。我想将第二个圆 B 的中心放在与圆心相同的坐标处第一个圆 A,然后我想使用 JSlider
在实现 JSlider 的 ChangeListener 时,只要值发生变化就会触发该事件。我需要获取 JSLider 的最后一个值:我不需要 slider 访问的每个值,只需要用户释放 slider
我有一个带有附加 ChangeListerner 的简单 JSlider。这是代码: JSlider slider = new JSlider(); slider.setMinorTickSpacin
我有一个非常简单的 JFrame,其北面板中只有一个 JSlider。我希望通过以下方法以最小尺寸显示 JSlider。 slider.putClientProperty("JComponent.si
我想为我 parent 的 Estate Agency 假期出租在线预订服务创建一个 Java 应用程序。 不幸的是,我还不能发布图片,但他们想要一种 slider 式的预订服务,用户可以在其中滑动条
我编写了一个小程序作为初学者练习,使用两个 JSlider 将摄氏度转换为华氏度,反之亦然。我让两个 JSliders 互相监听,目标是每当我移动一个 slider 时,Celcius slider
这是我的代码的一部分,但它不起作用,因为它一直说在我的 ActionListener 中找不到该符号。我不知道如何让它发挥作用。 所以基本上我想做的是让 1-8.png 中的图像根据 slider 着
我是一名优秀的程序员,十分优秀!