Java 当我按下";时,知道所有选中的项目;“好的”;按钮摆动
当我点击ok按钮时,它应该打印所有选中的项目。这是我试过的 我尝试了以下代码,但它不起作用Java 当我按下";时,知道所有选中的项目;“好的”;按钮摆动,java,jquery,swing,jbutton,jcheckbox,Java,Jquery,Swing,Jbutton,Jcheckbox,当我点击ok按钮时,它应该打印所有选中的项目。这是我试过的 我尝试了以下代码,但它不起作用 public class FilePermission extends JPanel implements ItemListener { static String[] videoAvailable = new String[25]; static int j = 0; JCheckBox[] file_boxes = new JCheckBox[25]; JButton button; StringB
public class FilePermission extends JPanel implements ItemListener {
static String[] videoAvailable = new String[25];
static int j = 0;
JCheckBox[] file_boxes = new JCheckBox[25];
JButton button;
StringBuffer choices;
JLabel pictureLabel;
//static int j = 0;
public FilePermission() {
super(new BorderLayout());
JPanel checkPanel = new JPanel(new GridLayout(0, 1));
//Create the check boxes.
for (int i = 0; i < videoAvailable.length; i++) {
if (videoAvailable[i] != null) {
file_boxes[i] = new JCheckBox(videoAvailable[i]);
file_boxes[i].addItemListener(this);
checkPanel.add(file_boxes[i]);
}
}
button = new JButton("ok");
//Register a listener for the check boxes.
button.addItemListener(this);
//Indicates what's on the geek.
//Set up the picture label
pictureLabel = new JLabel();
pictureLabel.setFont(pictureLabel.getFont().deriveFont(Font.ITALIC));
//Put the check boxes in a column in a panel
checkPanel.add(button);
add(checkPanel, BorderLayout.LINE_START);
add(pictureLabel, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
/** Listens to the check boxes. */
public void itemStateChanged(ItemEvent e) {
for (int i = 0; i < videoAvailable.length; i++) {
if (videoAvailable[i] != null) {
if (file_boxes[i].isSelected()) {
System.out.println(file_boxes[i]);
}
}
}
if (button.isSelected()) {
System.out.println(button);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("CheckBoxDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new FilePermission();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws IOException {
File directory = new File("D:\\ims\\");
parseDir(directory);
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
公共类文件权限扩展JPanel实现ItemListener{
静态字符串[]videoAvailable=新字符串[25];
静态int j=0;
JCheckBox[]file_box=新的JCheckBox[25];
按钮;
缓冲区选择;
JLabel图片标签;
//静态int j=0;
公共文件权限(){
超级(新边框布局());
JPanel checkPanel=newjpanel(newgridlayout(0,1));
//创建复选框。
对于(int i=0;i
这些方法是从java类的主方法调用的。如果您不理解,我想回答。请评论“button.addItemListener(this);”。
或者使用ActionListener for OK按钮。我能够找到答案。答案如下:
public class FilePermission extends JPanel implements ItemListener {
static String[] videoAvailable = new String[25];
static int j = 0;
JCheckBox[] file_boxes = new JCheckBox[25];
JButton button;
StringBuffer choices;
JLabel pictureLabel;
static JFrame frame = new JFrame("CheckBoxDemo");
//static int j = 0;
public FilePermission() {
super(new BorderLayout());
JPanel checkPanel = new JPanel(new GridLayout(0, 1));
//Create the check boxes.
for (int i = 0; i < videoAvailable.length; i++) {
if (videoAvailable[i] != null) {
file_boxes[i] = new JCheckBox(videoAvailable[i]);
file_boxes[i].addItemListener(this);
checkPanel.add(file_boxes[i]);
}
}
button = new JButton("ok");
//Register a listener for the check boxes.
button.addItemListener(this);
//Indicates what's on the geek.
//Set up the picture label
pictureLabel = new JLabel();
pictureLabel.setFont(pictureLabel.getFont().deriveFont(Font.ITALIC));
//Put the check boxes in a column in a panel
checkPanel.add(button);
add(checkPanel, BorderLayout.LINE_START);
add(pictureLabel, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < videoAvailable.length; i++) {
if (videoAvailable[i] != null) {
if (file_boxes[i].isSelected()) {
System.out.println(videoAvailable[i]);
}
}
}
frame.dispose();
}
});
}
/** Listens to the check boxes. */
public void itemStateChanged(ItemEvent e) {
for (int i = 0; i < videoAvailable.length; i++) {
if (videoAvailable[i] != null) {
if (file_boxes[i].isSelected()) {
System.out.println(file_boxes[i]);
}
}
}
if (button.isSelected()) {
System.out.println(button);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new FilePermission();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws IOException {
File directory = new File("D:\\ims\\");
parseDir(directory);
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void parseDir(File dirPath) {
try {
File files[] = null;
if (dirPath.isDirectory()) {
files = dirPath.listFiles();
for (File dirFiles : files) {
if (dirFiles.isDirectory()) {
parseDir(dirFiles);
} else {
if (dirFiles.getName().endsWith(".mkv")) {
videoAvailable[j] = dirFiles.getName();
j++;
}
}
}
} else {
if (dirPath.getName().endsWith(".mkv")) {
videoAvailable[j] = dirPath.getName();
j++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
公共类文件权限扩展JPanel实现ItemListener{
静态字符串[]videoAvailable=新字符串[25];
静态int j=0;
JCheckBox[]file_box=新的JCheckBox[25];
按钮;
缓冲区选择;
JLabel图片标签;
静态JFrame=新JFrame(“CheckBoxDemo”);
//静态int j=0;
公共文件权限(){
超级(新边框布局());
JPanel checkPanel=newjpanel(newgridlayout(0,1));
//创建复选框。
对于(int i=0;i