帮忙改程序!!!在线等待,急用!!!

cmlazio 2003-02-18 11:37:15
这是我的程序,运行没有问题,但是本人脑子里没有OO的概念,所以只写了2个class,从设计上说是太失败了:(
那位大侠能够按照OO的标准帮我改一下,把程序多分出几个class,比如save, load和add.(因为还需要写测试程序和UML,尤其是UML,要使这个结构,就死掉了:()多谢多谢了!!JDK1.3
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;



public class GymLogProgram implements ActionListener {

//declare public variables
JFrame frame;
JTextArea editor; //a textarea to display a log ,and is editable
JTextField day;
JTextField repititions;
JTextField sets;
JTextField program;
JTextField intensity;
JComboBox jc; //all kinds of exercise
String strExercise = new String ("Legs");
boolean isfirst = true;
Font editorfont = new Font("Monospaced",Font.PLAIN,12);
int tabsize = 5;


/**the main function
*/
public static void main(String[] args){

GymLogProgram gymlogprogram = new GymLogProgram();
gymlogprogram.init();

}


public void init(){

//initialise the frame
frame = new JFrame("GymLogProgram");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//button controls
/* for Load and Save button*/
JPanel controlpanel = new JPanel();
JButton openbutton = new JButton("Load Log");
JButton writebutton = new JButton("Save Log");
JButton exitbutton = new JButton("Exit");
exitbutton.addActionListener(this);
openbutton.addActionListener(this);
writebutton.addActionListener(this);
controlpanel.add(writebutton);
controlpanel.add(openbutton);
controlpanel.add(exitbutton);
frame.getContentPane().add(controlpanel,"North");

//set the part for inserting information into a log
//set the layout
JPanel insertpanel = new JPanel();
JPanel insertpanel1 = new JPanel();
JPanel insertpanel2 = new JPanel();
//Jpanel insertpanel3 = new Jpanel();

insertpanel.add(insertpanel1, "North");
//insertpanel.add(insertpanel2, "Center");
insertpanel.add(insertpanel2, "South");

insertpanel1.setLayout( new GridLayout(3,6));

//set the section to insert the number of days of a log
JLabel daylabel = new JLabel("Day");
insertpanel1.add(daylabel);
day = new JTextField("",1);
insertpanel1.add(day);

//set the section to insert a exercise type
JLabel exerciselabel = new JLabel("Exercise");
insertpanel1.add(exerciselabel);
jc = new JComboBox();
jc.addItem("Legs");
jc.addItem("Biceps Superset");
jc.addItem("Triceps Superset");
jc.addItem("Stomach");
jc.addItem("Chest & Back");
jc.addItem("Shoulders Triset");
jc.addItem("Arms");
/** Provides actions to the list of exercises.
*/
jc.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(ItemEvent ie) {
strExercise = (String)ie.getItem();
}
});

insertpanel1.add(jc);

//set the section to insert the name of exercise
JLabel programlable = new JLabel("Program");
insertpanel1.add(programlable);
program = new JTextField("",1);
insertpanel1.add(program);

//set the section to insert the numbers of repititions
JLabel repititionslabel = new JLabel("Repititions");
insertpanel1.add(repititionslabel);
repititions = new JTextField("",1);
insertpanel1.add(repititions);

//set the section to insert the numbers of sets
JLabel setslabel = new JLabel("Sets");
insertpanel1.add(setslabel);
sets = new JTextField("",1);
insertpanel1.add(sets);

//set the section to insert the intensity of exercise
JLabel intensitylabel = new JLabel("Intensity");
insertpanel1.add(intensitylabel);
intensity = new JTextField("",1);
insertpanel1.add(intensity);

//set the ADD button
JButton ADD = new JButton("Add");
insertpanel2.add(ADD);
ADD.addActionListener(this);

frame.getContentPane().add(insertpanel,"South");

//editor
editor = new JTextArea(new PlainDocument(),"",20,60); //initializes a JTextArea with width 60 and height 20
editor.setFont(editorfont);
editor.setTabSize(tabsize);
JScrollPane scrollpane = new JScrollPane(editor);
frame.getContentPane().add(scrollpane,"Center");
frame.pack();
frame.show();

//centre the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);


/////////////////////////////
/*create a user login window
*/
LoginDialog loginDlg = new LoginDialog( frame );

Dimension dlgSize = loginDlg.getSize();
if (dlgSize.height > screenSize.height) {
dlgSize.height = screenSize.height;
}
if (dlgSize.width > screenSize.width) {
dlgSize.width = screenSize.width;
}

loginDlg.setLocation((screenSize.width - dlgSize.width) / 2, (screenSize.height - dlgSize.height) / 2);
loginDlg.setVisible(true);
}

/////////////////
/** Brings up as file save dialog and save the file into a directry selected by the user.
*/
public File getAFileForSave(){
File file = null;
File currentdirectory = new File(".");
JFileChooser filechooser = new JFileChooser(currentdirectory);
int replycode = filechooser.showSaveDialog(frame);
if (replycode == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
}
return file;

}

/** Brings up as imple file open dialog and returns the file selected by the user.
*/
public File getAFileToOpen(){
File file = null;
File currentdirectory = new File(".");
JFileChooser filechooser = new JFileChooser(currentdirectory);
int replycode = filechooser.showOpenDialog(frame);
if (replycode == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
}
...全文
23 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cmlazio 2003-02-18
  • 打赏
  • 举报
回复
/*the class of login dialog window
*/
class LoginDialog extends JDialog {

JPanel panel = new JPanel();

//set the username and password
String username = new String("zongyi");
String password = new String("zongyi");

JTextField userText;
JPasswordField pwText;

int loginCount = 0;
JFrame parentFrame;

LoginDialog(JFrame parent) {

super(parent, "Login Dialog", true);
setSize(300, 150);
getContentPane().add(panel);
parentFrame = parent;

init();
}

public void init() {
//set the layout
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel0 = new JPanel();

panel1.setLayout( new GridLayout(3,2) );

panel.add(panel0, "North");
panel.add(panel1, "Center");
panel.add(panel2, "South");

JLabel userLabel = new JLabel("Username");
panel1.add( userLabel );
userText = new JTextField("", 10);
panel1.add( userText );

JLabel pwLabel = new JLabel("Password");
panel1.add( pwLabel );
pwText = new JPasswordField("", 10);
panel1.add( pwText );

JButton ok = new JButton("OK");
panel2.add(ok);

//check the usernaem and password to login
ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {

if ( userText.getText().compareTo(username) == 0 &&
pwText.getText().compareTo(password) == 0 ) {

dispose();
}
else{

JOptionPane.showMessageDialog( parentFrame, "Invaild Login, please try again!" );

userText.setText("");
pwText.setText("");

loginCount ++;
if ( loginCount > 3 ) exit();

}
}
});

JButton cancel = new JButton("CANCEL");
panel2.add(cancel);

cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
exit();
}
});
}

//action of exit
public void exit() {

dispose();
System.exit(0);
}
}

cmlazio 2003-02-18
  • 打赏
  • 举报
回复
/** Writes the string to the input file.
*/
public void writeStringToFile(File file, String s){

try{
FileWriter filewriter = new FileWriter(file);
StringReader stringreader = new StringReader(s);
BufferedReader bufferedreader = new BufferedReader(stringreader);
String lineread = "";
while ((lineread = bufferedreader.readLine()) != null){
filewriter.write(lineread + "\r\n");
}
filewriter.close();
}catch (FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
}catch (IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
showExceptionErrorMessage("IOException: " + ioe.getMessage());
}


}

/** Appends the contents of the input file to the text editor.
*/
public void writeFileToEditor(File file){

try{
FileReader filereader = new FileReader(file);
BufferedReader bufferedreader = new BufferedReader(filereader);
String lineread = "";
while ((lineread = bufferedreader.readLine()) != null){
editor.append(lineread + "\n");
}
filereader.close();
}catch (FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
}catch (IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
showExceptionErrorMessage("IOException: " + ioe.getMessage());
}

}

/*write input on the textarea as the information of a log
*/
public void writeToTextArea() {

final int nday = 1;
final int nexercise = 9;
final int nrepititions = 46;
final int nsets = 63;
final int nprogram = 28;
final int nintensity = 72;
final int ndone = 86;

try{
//set the first line of a log
StringBuffer str = new StringBuffer(" Day Exercise Program Repititions Sets Intensity Done(%)");

if ( isfirst ) {
editor.append( str.toString() );
editor.append("\n");
isfirst = false;
}

for ( int i=0; i<str.length(); i++ ) {
str.replace(i, i+1, " ");
}

int t = str.length();
//get the info from input
str.insert(nday, day.getText().trim());
str.insert(nexercise, strExercise);
str.insert(nprogram, program.getText().trim());
str.insert(nrepititions, repititions.getText().trim());
str.insert(nsets, sets.getText().trim());
str.insert(nintensity, intensity.getText().trim());

if ( t < str.length() ) {
editor.append( str.toString() );
editor.append("\n");
}
//error message
}catch (IllegalArgumentException e){
System.err.println("IllegalArgumentException: " + e.getMessage());
showExceptionErrorMessage("IllegalArgumentException: " + e.getMessage());
}
}

/** Provides actions to Log(user input).
*/
public void actionPerformed(ActionEvent actionevent){
String actioncommand = actionevent.getActionCommand();
if (actioncommand.compareTo("Exit") == 0) {
System.exit(0);
}else if (actioncommand.compareTo("Load Log") == 0) {
File f = getAFileToOpen();
writeFileToEditor(f);
}else if (actioncommand.compareTo("Save Log") == 0) {
String s = editor.getText();
File f = getAFileForSave();
writeStringToFile(f,s);
showMessage("Contents of the Text Editor saved to file " + f.getName());
}else if (actioncommand.compareTo("Add") == 0) {
writeToTextArea();
}
}

/** Conveniently displays a message to the user and waits for
* confirmation.
*/
public void showMessage(String s){
JOptionPane.showMessageDialog(frame,s);
}

/** Conveniently displays an exception error message to the user
* and waits for confirmation.
*/
public void showExceptionErrorMessage(String s){
JOptionPane.showMessageDialog(frame,s,s,JOptionPane.ERROR_MESSAGE);
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧