Java 如何通过编写此代码使“保存”按钮工作?
如何通过编写此代码使“保存”按钮工作? 无论何时我运行它,滚动按钮都会工作,但保存按钮不工作? 这是一个掷骰子的程序,但我需要让保存按钮工作,有人能帮我吗?Java 如何通过编写此代码使“保存”按钮工作?,java,Java,如何通过编写此代码使“保存”按钮工作? 无论何时我运行它,滚动按钮都会工作,但保存按钮不工作? 这是一个掷骰子的程序,但我需要让保存按钮工作,有人能帮我吗? 如何将dice_score.txt文件链接到此程序?操作侦听器未添加到“保存”按钮 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class DiceFrame
如何将dice_score.txt文件链接到此程序?操作侦听器未添加到“保存”按钮
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class DiceFrame extends JFrame{
ImageIcon[] dice_im = new ImageIcon[7];
String score="start";
JPanel mainPanel= new JPanel();
JPanel scorePanel=new JPanel();
JPanel buttonPanel =new JPanel();
JLabel picLabel = new JLabel();
JTextArea scorefield = new JTextArea();
JButton roll= new JButton("roll the dice");
JButton save = new JButton("save");
ActionListener action;
ActionListener output;
public DiceFrame(){
super();
setSize(600, 600);
setTitle("Dice Program");
loadImage();
getContentPane().add(mainPanel, BorderLayout.CENTER);
getContentPane().add(scorePanel, BorderLayout.EAST);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(save);
buttonPanel.add(roll);
mainPanel.add(picLabel);
picLabel.setIcon(dice_im[0]);
scorePanel.add(scorefield);
scorefield.setText(score);
action = new DiceActionListener();
roll.addActionListener(action);
}
private void loadImage(){
dice_im [0]= new ImageIcon("1.jpg");
dice_im[1] = new ImageIcon("2.jpg");
dice_im[2] = new ImageIcon("3.JPG");
dice_im[3] = new ImageIcon("4.JPG");
dice_im[4] = new ImageIcon("5.JPG");
dice_im[5] = new ImageIcon("6.JPG");
}
public static void main(String args[]){
DiceFrame frame = new DiceFrame();
frame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class DiceActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Random rg = new Random();
int k = rg.nextInt(6) + 1;
picLabel.setIcon(dice_im[k]);
}
}
class SaveActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) { //NEW code
String out_file_name = "dice_score.txt";
try {
File outputfile = new File(out_file_name);
PrintStream out = new PrintStream(
new FileOutputStream(outputfile));
out.println(score);
out.flush();`
out.close();
} catch (IOException y) {
System.out.println("IO problem.");
}
}
}
}
操作侦听器未添加到“保存”按钮
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class DiceFrame extends JFrame{
ImageIcon[] dice_im = new ImageIcon[7];
String score="start";
JPanel mainPanel= new JPanel();
JPanel scorePanel=new JPanel();
JPanel buttonPanel =new JPanel();
JLabel picLabel = new JLabel();
JTextArea scorefield = new JTextArea();
JButton roll= new JButton("roll the dice");
JButton save = new JButton("save");
ActionListener action;
ActionListener output;
public DiceFrame(){
super();
setSize(600, 600);
setTitle("Dice Program");
loadImage();
getContentPane().add(mainPanel, BorderLayout.CENTER);
getContentPane().add(scorePanel, BorderLayout.EAST);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(save);
buttonPanel.add(roll);
mainPanel.add(picLabel);
picLabel.setIcon(dice_im[0]);
scorePanel.add(scorefield);
scorefield.setText(score);
action = new DiceActionListener();
roll.addActionListener(action);
}
private void loadImage(){
dice_im [0]= new ImageIcon("1.jpg");
dice_im[1] = new ImageIcon("2.jpg");
dice_im[2] = new ImageIcon("3.JPG");
dice_im[3] = new ImageIcon("4.JPG");
dice_im[4] = new ImageIcon("5.JPG");
dice_im[5] = new ImageIcon("6.JPG");
}
public static void main(String args[]){
DiceFrame frame = new DiceFrame();
frame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class DiceActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Random rg = new Random();
int k = rg.nextInt(6) + 1;
picLabel.setIcon(dice_im[k]);
}
}
class SaveActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) { //NEW code
String out_file_name = "dice_score.txt";
try {
File outputfile = new File(out_file_name);
PrintStream out = new PrintStream(
new FileOutputStream(outputfile));
out.println(score);
out.flush();`
out.close();
} catch (IOException y) {
System.out.println("IO problem.");
}
}
}
}
将此添加到
DiceFrame()的结尾处。
:
将此添加到
DiceFrame()的结尾处。
:
看
好吧,你必须按照这个模式去做
action = new DiceActionListener();
roll.addActionListener(action);
虽然在现实中不需要创建命名实例,但您可以简单地创建命名实例
SaveActionListener action2 = new SaveActionListener ();
save.addActionListener(action2);
看
好吧,你必须按照这个模式去做
action = new DiceActionListener();
roll.addActionListener(action);
虽然在现实中不需要创建命名实例,但您可以简单地创建命名实例
SaveActionListener action2 = new SaveActionListener ();
save.addActionListener(action2);
注意:Java与JavaScript没有任何关系。注意:Java与JavaScript没有任何关系。保存按钮仍然不起作用。单击保存按钮时没有发生任何事情?是否进入SaveActionListener?单击保存按钮时没有发生任何事?我不知道输入是什么意思?保存按钮仍然不起作用。单击保存按钮时没有发生任何事情?是否进入了SaveActionListener?单击保存按钮时没有发生任何事情?我不知道你进来是什么意思?