gpt4 book ai didi

java - 在java中制作一个可播放的jslider

转载 作者:行者123 更新时间:2023-11-29 07:42:54 31 4
gpt4 key购买 nike

我正在使用从用户那里获取数学表达式(例如 ADD(MUL(X,Y),Z) )的 eclipse 和 windowbuilder 制作图形器,并在某一时刻要求用户指定其变量之一随着时间的 。然后将要求用户选择该变量的起点和范围。然后程序将显示一个框架(和一个面板),其中有一个 jslider 和一个播放按钮。当用户单击按钮时,程序应该开始绘制表达式。它应该看起来像 Gapminder chart .

我检查了有关更新 jsliders 的其他问题,但它们通常是关于音乐播放器 slider 或随时间变化的 slider (没有图形部分)。

我的问题具体是关于 slider 的更新(间隔可以是 1 )但如果您还可以指导我如何制作可播放的 slider ,它也可以用于图形部分,那就太好了。

enter image description here这是到目前为止的代码:

package progGUI;

import java.awt.BorderLayout;

public class CanvasFrame extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {

CanvasFrame frame = new CanvasFrame();
frame.setVisible(true);

} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public CanvasFrame() {
setTitle("Graph");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 700, 542);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

JSlider slider = new JSlider();
slider.setPaintLabels(true);
slider.setMinorTickSpacing(1);
slider.setMajorTickSpacing(5);
slider.setPaintTicks(true);


JLabel lblEnteredExpression = new JLabel("Entered Expression");

JLabel lblVaraibles = new JLabel("Variables");

JFormattedTextField formattedTextField = new JFormattedTextField();

JFormattedTextField formattedTextField_1 = new JFormattedTextField();

JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);

JButton btnNewButton = new JButton("Play");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 453, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblEnteredExpression, Alignment.TRAILING)
.addComponent(lblVaraibles)
.addComponent(formattedTextField, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
.addComponent(formattedTextField_1, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE))
.addGap(57))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(slider, GroupLayout.PREFERRED_SIZE, 454, GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblEnteredExpression)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(formattedTextField_1, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(lblVaraibles)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(formattedTextField, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 350, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(31)
.addComponent(btnNewButton))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(18)
.addComponent(slider, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(402))
);
contentPane.setLayout(gl_contentPane);
}


}

最佳答案

使用 javax.swing.Timer 安排定期回调,更新 JSlider 的值。使用附加到 JSliderChangeListener 来驱动图形,例如...

Slider

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private Timer timer;
private JSlider slider;
private JButton button;
private JTextField field;

public TestPane() {
slider = new JSlider();
field = new JTextField(4);
button = new JButton(">");
add(slider);
add(button);
add(field);

slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
field.setText(Integer.toString(slider.getValue()));
}
});
slider.setValue(0);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (timer.isRunning()) {
stopTheClock();
} else {
startTheClock();
}
}
});
timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int value = slider.getValue() + 1;
if (value >= slider.getMaximum()) {
stopTheClock();
} else {
slider.setValue(value);
}
}
});
}

protected void startTheClock() {
slider.setValue(0);
timer.start();
button.setText("[]");
}

protected void stopTheClock() {
timer.stop();
button.setText(">");
}

}

}

参见 How to use Swing TimersHow to Use Sliders了解更多详情

关于java - 在java中制作一个可播放的jslider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28472409/

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