创建九个滑块,移动每个滑块在对应的表格里显示相应的数据。
现在的问题是怎么分别监视它们?
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
public class SliperController {
JFrame mainWin = new JFrame("滑动条示范");
JPanel JP = new JPanel();
JTable table = new JTable(3, 3);
JSlider slider;
public void init() {
for (int i = 0; i < 9; i++) {
slider = new JSlider();
slider.setPaintTicks(true);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintLabels(true);
slider.addChangeListener(new MyChangeListener());
JP.add(slider);
}
JP.setLayout(new GridLayout(3, 3));
mainWin.add(new JScrollPane(table), BorderLayout.EAST);
mainWin.add(JP);
mainWin.pack();
mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWin.setVisible(true);
}
public static void main(String[] args) {
new SliperController().init();
}
}
java?gui?滑块监视
------解决方案--------------------
楼主既然想分别监视他们,最好不要将之写到一个循环中啊
至于怎么“分别”监听这是什么说法啊
你的那个实现就已经是分别监听了哇
给你一个比较吧:下面的这种形式
package com.kiritor;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SliperController implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
}
JFrame mainWin = new JFrame("滑动条示范");
JPanel JP = new JPanel();
JTable table = new JTable(3, 3);
JSlider slider;
public void init() {
for (int i = 0; i < 9; i++) {
slider = new JSlider();
slider.setPaintTicks(true);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintLabels(true);
slider.addChangeListener(this);
JP.add(slider);
}
JP.setLayout(new GridLayout(3, 3));
mainWin.add(new JScrollPane(table), BorderLayout.EAST);
mainWin.add(JP);
mainWin.pack();
mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWin.setVisible(true);
}
public static void main(String[] args) {
new SliperController().init();
}
}