【发布时间】:2015-02-12 08:14:29
【问题描述】:
我正在使用 eclipse 和 windowbuilder 制作一个绘图仪,它从用户那里获取一个数学表达式(例如 ADD(MUL(X,Y),Z) ),并在某一时刻要求用户指定其中一个变量随着时间的 。 然后将要求用户选择该变量的起点和范围。然后程序将显示一个框架(和一个面板),其中包含一个 jslider 和一个播放按钮。当用户单击按钮时,程序应该开始绘制表达式。 它应该看起来像 Gapminder chart 。
我已经检查了有关更新 jslider 的其他问题,但它们通常是关于音乐播放器滑块或随时间变化的滑块(没有图形部分)。
我的问题是关于滑块的更新(间隔可以是 1 ) 但是,如果您还可以指导我如何制作可以用于绘图部分的可播放滑块,那就太好了。
这是到目前为止的代码:
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);
}
}
【问题讨论】:
-
可玩是什么意思?
-
我的意思是它会更新,所以当您点击播放按钮时,它会从例如零开始并添加到滑块旋钮的值(每秒 1 个)查看我正在尝试编写的 gapminder 链接
-
允许
JSlider控制在图中绘制的内容,使用SwingTimer定期更新JSlider -
感谢您的解决方案,但我怎样才能保持该值用于绘图?每次滑块更改其值时,都应绘制一个点,并将这些点连接起来形成一个图形
标签: java swing windowbuilder jslider