gpt4 book ai didi

java - 在 java swing 中移动组件

转载 作者:行者123 更新时间:2023-11-29 10:19:54 24 4
gpt4 key购买 nike

我制作了一个 swing 应用程序,其中我在面板上有一个网格布局,现在我在该面板上有 8 个自定义按钮,左右两个按钮用于导航。

现在我的问题是,当我点击那个导航按钮时,我想以滑动方式移动按钮,为此我设置了每个按钮的位置,

但是 Jpanel 不会自行刷新,所以它是不可见的,如果我添加对话框并单击确定,那么它看起来就像按钮在移动一样。

我该怎么做,我使用了 revalidate() 和 repaint() 但它不起作用。

点击下一个按钮时执行的函数

java.net.URL imageURL = cldr.getResource("images/" +2 + ".png");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
button = new MyButton("ABC", aceOfDiamonds, color[3]);
Component buttons[] = jPanel1.getComponents();
ArrayList<MyButton> buttons1=new ArrayList<MyButton>();
for(int i=0;i<buttons.length;i++)
{
buttons1.add((MyButton) buttons[i]);
}
Point p=buttons[0].getLocation();
Point p1=buttons[1].getLocation();
int dis=p1.x-p.x;
System.out.println("Distance-->"+dis);
button.setLocation(buttons[buttons.length-1].getLocation().x+dis,buttons[0].getLocation().y);
jPanel1.add(button);
buttons1.add(button);
for(int i=0;i<dis;i++)
{
for(int btn=0;btn<buttons1.size();btn++)
{
int currX=buttons1.get(btn).getLocation().x;
currX--;
buttons1.get(btn).setLocation(currX, buttons1.get(btn).getLocation().y);
}
try
{
Thread.sleep(500);
}
catch (InterruptedException ex)
{
Logger.getLogger(TestPanel.class.getName()).log(Level.SEVERE, null, ex);
}
//JOptionPane.showMessageDialog(null,"fds");
jPanel1.validate();jPanel1.repaint();
}

![在此处输入图片描述][1]

最佳答案

不要设置位置。从容器中移除所有按钮并以新顺序重新添加它们,然后调用 revalidate()repaint()

编辑
如果你想动画滑动按钮,然后考虑将它们放在一个没有滚动条的 JScrollPane 中,然后以编程方式滚动按钮。我同意您不应在 EDT 上使用 Thread.sleep(...),而应使用 Swing Timer。

编辑2
例如:

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

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SlideButtons extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 200;
private static final int MAX_BUTTONS = 100;
private static final int SCROLL_TIMER_DELAY = 10;
public static final int SCROLL_DELTA = 3;
private JPanel btnPanel = new JPanel(new GridLayout(1, 0, 10, 0));
private JScrollPane scrollPane = new JScrollPane(btnPanel);
private JButton scrollLeftBtn = new JButton("<");
private JButton scrollRightBtn = new JButton(">");
private BoundedRangeModel horizontalModel = scrollPane.getHorizontalScrollBar().getModel();
private Timer scrollTimer = new Timer(SCROLL_TIMER_DELAY, new ScrollTimerListener());
public String btnText = "";

public SlideButtons() {
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
for (int i = 0; i < MAX_BUTTONS; i++) {
String text = String.format("Button %03d", (i + 1));
JButton btn = new JButton(text);
btnPanel.add(btn);
}
ScrollingBtnListener scrollingBtnListener = new ScrollingBtnListener();
scrollLeftBtn.addChangeListener(scrollingBtnListener);
scrollRightBtn.addChangeListener(scrollingBtnListener);

JPanel northPanel = new JPanel(new BorderLayout());
northPanel.add(scrollLeftBtn, BorderLayout.LINE_START);
northPanel.add(scrollPane, BorderLayout.CENTER);
northPanel.add(scrollRightBtn, BorderLayout.LINE_END);

setLayout(new BorderLayout());
add(northPanel, BorderLayout.PAGE_START);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

private class ScrollingBtnListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JButton btn = (JButton)e.getSource();
ButtonModel model = btn.getModel();
//actionCommand = model.getActionCommand();
btnText = btn.getText();
if (model.isPressed() && model.isEnabled()) {
scrollTimer.start();
} else {
scrollTimer.stop();
}
}
}

private class ScrollTimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (btnText == null) {
return;
}
int max = horizontalModel.getMaximum();
int min = horizontalModel.getMinimum();
int value = horizontalModel.getValue();

if (btnText.equals(">")) {
if (value <= max) {
value += SCROLL_DELTA;
} else {
value = max;
}
} else if (btnText.equals("<")) {
if (value >= min) {
value -= SCROLL_DELTA;
} else {
value = min;
}
}
horizontalModel.setValue(value);
}
}

private static void createAndShowGui() {
SlideButtons mainPanel = new SlideButtons();

JFrame frame = new JFrame("SlideButtons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

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

关于java - 在 java swing 中移动组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8365431/

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