如何在设置完我的JLabel位置后不重新绘制

huangapple go评论81阅读模式
英文:

How don't repaint after setLocation my JLabel

问题

I'm doing an animation for my Menu. I created an animation video: https://youtu.be/wG6AFMj1ZYI
As u can see the animation repaint my frame so hard :'(. I won't repaint after modifying my JLabel!

私正在为我的菜单做动画。我创建了一个动画视频:https://youtu.be/wG6AFMj1ZYI
如您所见,动画会导致我的框架重新绘制得非常困难 :'(。我在修改我的JLabel后不想重新绘制!

My fps calculator detect how many times method "paintComponent" is called.

我的fps计算器检测方法"paintComponent"被调用了多少次。

Thanks!

谢谢!

英文:

I'm doing an animation for my Menu. I created an animation vidéo : https://youtu.be/wG6AFMj1ZYI
As u can see the animation repaint my frame so hard :'(. I won't repaint after modifying my JLabel !

private synchronized void animationButtonExited(JLabelFirstPosition button) {
	if (button.getActual_thread_running().isAlive()) {
		button.getActual_thread_running().interrupt();
	}
	Thread t = new Thread(() -> {
		SwingUtilities.invokeLater(() -> {
			button.setFont(new Font(null, 0,
					(int) (this.racio_width < this.racio_height
							? (int) (button.getFirstFontSize() * this.racio_width)
							: (button.getFirstFontSize() * this.racio_height))));
			button.setSize((int) (button.getFirstSize().getWidth() * this.racio_width),
					(int) (button.getFirstSize().getHeight() * this.racio_height));
		});
		try {
			while (button.getLocation().getX() > button.getFirstX() * this.racio_width) {
				SwingUtilities.invokeLater(() -> {
					button.setLocation((int) ((button.getLocation().getX() - 1)),
							(int) (button.getFirstY() * this.racio_height));
				});
				Thread.sleep(6);
			}
		} catch (InterruptedException e1) {
		}
	});
	t.start();
	if (!button.getActual_thread_running().isAlive()) {
	} else {
		button.getActual_thread_running().interrupt();
	}
	button.setActual_thread_running(t);
}

private synchronized void animationButtonEntered(JLabelFirstPosition button) {
	if (button.getActual_thread_running().isAlive()) {
		button.getActual_thread_running().interrupt();
	}
	Thread t = new Thread(() -> {
		SwingUtilities.invokeLater(() -> {
			button.setFont(new Font("", 1,
					(int) (this.racio_width < this.racio_height
							? (int) (button.getFirstFontSize() * this.racio_width)
							: ((button.getFirstFontSize() + 5) * this.racio_height))));
			button.setSize((int) ((button.getFirstSize().getWidth() + 20) * this.racio_width),
					(int) (button.getFirstSize().getHeight() * this.racio_height));
		});
		try {
			while (button.getLocation().getX() <= (button.getFirstX() + 20) * this.racio_width) {
				SwingUtilities.invokeLater(() -> {
					button.setLocation((int) ((button.getLocation().getX() + 1)),
							(int) (button.getFirstY() * this.racio_height));
				});
				Thread.sleep(3);
			}
		} catch (InterruptedException e1) {
		}
	});
	t.start();
	if (!button.getActual_thread_running().isAlive()) {
	} else {
		button.getActual_thread_running().interrupt();
	}
	button.setActual_thread_running(t);
}

My fps calculator detect how many time method "paintComponent" is call.

Thanks !

答案1

得分: 0

这是创建一个菜单的一种方法,鼠标悬停时使选择项突出显示。

这是创建一个菜单的一种方法鼠标悬停时使选择项突出显示

MouseListener 类有三个方法我们将在此示例中使用 mouseEntered 方法检测鼠标进入 Swing 组件时的情况本例中为 JLabel mouseExited 方法检测鼠标退出 Swing 组件的情况

mousePressed 方法检测鼠标按钮按下的情况这允许我们使用 JLabel 来选择选项

设置 JFrame 有点棘手我们不希望我们创建的 JPanels 不断改变大小

所以我们经历以下过程

1. 使用较大的字体创建菜单 JPanel

2. 调整 JFrame

3. 获取菜单 JPanel 的大小并将其设置为首选大小

4. 在使 JFrame 可见之前将字体改回较小的字体

这是我使用的代码

```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MouseoverExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MouseoverExample());
    }

    private Font font;

    private JLabel optionLabel;
    private JLabel option1Label;
    private JLabel option2Label;
    private JLabel option3Label;

    private JPanel menuPanel;

    @Override
    public void run() {
        JFrame frame = new JFrame("Mouseover Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createMenuPanel(), BorderLayout.BEFORE_LINE_BEGINS);
        frame.add(createDummyPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);

        menuPanel.setPreferredSize(menuPanel.getSize());
        updateFont();

        frame.setVisible(true);
    }

    private JPanel createMenuPanel() {
        JPanel outerPanel = new JPanel();
        outerPanel.setLayout(new FlowLayout());

        menuPanel = new JPanel();
        menuPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        menuPanel.setLayout(new GridLayout(0, 1, 5, 5));

        font = menuPanel.getFont().deriveFont(18f);
        Font boldFont = font.deriveFont(Font.BOLD).deriveFont(24f);
        ExampleListener listener = new ExampleListener(font, boldFont);

        option1Label = new JLabel("Option 1");
        option1Label.setFont(boldFont);
        option1Label.addMouseListener(listener);
        menuPanel.add(option1Label);

        option2Label = new JLabel("Option 2");
        option2Label.setFont(boldFont);
        option2Label.addMouseListener(listener);
        menuPanel.add(option2Label);

        option3Label = new JLabel("Option 3");
        option3Label.setFont(boldFont);
        option3Label.addMouseListener(listener);
        menuPanel.add(option3Label);

        outerPanel.add(menuPanel);

        return outerPanel;
    }

    private void updateFont() {
        option1Label.setFont(font);
        option2Label.setFont(font);
        option3Label.setFont(font);
    }

    private JPanel createDummyPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setPreferredSize(new Dimension(400, 400));

        optionLabel = new JLabel(" ");
        panel.add(optionLabel);

        return panel;
    }

    public class ExampleListener extends MouseAdapter {

        private Font enterFont;
        private Font exitFont;

        public ExampleListener(Font exitFont, Font enterFont) {
            this.exitFont = exitFont;
            this.enterFont = enterFont;
        }

        @Override
        public void mouseEntered(MouseEvent event) {
            JLabel label = (JLabel) event.getSource();
            label.setFont(enterFont);
        }

        @Override
        public void mouseExited(MouseEvent event) {
            JLabel label = (JLabel) event.getSource();
            label.setFont(exitFont);
        }

        @Override
        public void mousePressed(MouseEvent event) {
            JLabel label = (JLabel) event.getSource();
            String optionText = label.getText();
            optionLabel.setText(optionText + " displayed");
        }

    }
}

这部分代码中包括了创建菜单的示例,使用了鼠标事件来实现悬停和选择效果。希望这能帮助您理解代码的工作原理。
<details>
<summary>英文:</summary>
Here&#39;s one way to create a menu where a mouse-over makes the selection stand out.
[![Mouseover Example GUI][1]][1]
The `MouseListener` class has three methods that we&#39;ll use in this example.  The `mouseEntered` method detects when the mouse enters a Swing component, in this case, a `JLabel`.  The `mouseExited` method detects when the mouse exits a Swing component.
The `mousePressed` method detects when a mouse button is pressed.  This allows us to use a `JLabel` to select an option.
Setting up the `JFrame` is a bit tricky.  We don&#39;t want the `JPanels` we&#39;ve created to constantly change size.  
So, we go through the following process:
1.  Create the menu `JPanel` with the larger font
2.  Pack the `JFrame`
3.  Get the size of the menu `JPanel` and make that the preferred size of the menu `JPanel`
4.  Change the font back to the smaller font before we make the `JFrame` visible.
Here&#39;s the code I used.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MouseoverExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new MouseoverExample());
}
private Font font;
private JLabel optionLabel;
private JLabel option1Label;
private JLabel option2Label;
private JLabel option3Label;	
private JPanel menuPanel;
@Override
public void run() {
JFrame frame = new JFrame(&quot;Mouseover Example&quot;);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMenuPanel(), BorderLayout.BEFORE_LINE_BEGINS);
frame.add(createDummyPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
menuPanel.setPreferredSize(menuPanel.getSize());
updateFont();
frame.setVisible(true);
}
private JPanel createMenuPanel() {
JPanel outerPanel = new JPanel();
outerPanel.setLayout(new FlowLayout());
menuPanel = new JPanel();
menuPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
menuPanel.setLayout(new GridLayout(0, 1, 5, 5));
font = menuPanel.getFont().deriveFont(18f);
Font boldFont = font.deriveFont(Font.BOLD).deriveFont(24f);
ExampleListener listener = new ExampleListener(font, boldFont);
option1Label = new JLabel(&quot;Option 1&quot;);
option1Label.setFont(boldFont);
option1Label.addMouseListener(listener);
menuPanel.add(option1Label);
option2Label = new JLabel(&quot;Option 2&quot;);
option2Label.setFont(boldFont);
option2Label.addMouseListener(listener);
menuPanel.add(option2Label);
option3Label = new JLabel(&quot;Option 3&quot;);
option3Label.setFont(boldFont);
option3Label.addMouseListener(listener);
menuPanel.add(option3Label);
outerPanel.add(menuPanel);
return outerPanel;
}
private void updateFont() {
option1Label.setFont(font);
option2Label.setFont(font);
option3Label.setFont(font);
}
private JPanel createDummyPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setPreferredSize(new Dimension(400, 400));
optionLabel = new JLabel(&quot; &quot;);
panel.add(optionLabel);
return panel;
}
public class ExampleListener extends MouseAdapter {
private Font enterFont;
private Font exitFont;
public ExampleListener(Font exitFont, Font enterFont) {
this.exitFont = exitFont;
this.enterFont = enterFont;
}
@Override
public void mouseEntered(MouseEvent event) {
JLabel label = (JLabel) event.getSource();
label.setFont(enterFont);
}
@Override
public void mouseExited(MouseEvent event) {
JLabel label = (JLabel) event.getSource();
label.setFont(exitFont);
}
@Override
public void mousePressed(MouseEvent event) {
JLabel label = (JLabel) event.getSource();
String optionText = label.getText();
optionLabel.setText(optionText + &quot; displayed&quot;);
}
}
}
[1]: https://i.stack.imgur.com/chOAk.png
</details>

huangapple
  • 本文由 发表于 2020年8月12日 16:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63372406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定