public class BasicGuiOnlyText extends JPanel{
static String outputHtml="<html>";*>Creating The String*
private JFrame f = new JFrame("Static Web Page Builder"); *>Creating the Frame*
private JMenuBar mb = new JMenuBar(); *>Creating Menu*
private JMenu mnuFile = new JMenu("File");
private JMenuItem mnuItemNew=new JMenuItem("New HTML");
private JMenuItem mnuItemSave=new JMenuItem("Save");
private JMenuItem mnuItemQuit = new JMenuItem("Quit");
private JMenu mnuCreate = new JMenu("Create");
private JMenuItem mnuItemFinish=new JMenuItem("Finish");
private JMenu mnuHelp = new JMenu("Help");
private JMenuItem mnuItemAbout = new JMenuItem("About");
public BasicGuiOnlyText(){
f.setJMenuBar(mb);
mnuFile.add(mnuItemNew);>Adding Menu Items
mnuItemNew.addActionListener(new TextFieldSet());*>Adding MenuItemListeners*
mnuFile.add(mnuItemSave);
mnuFile.add(mnuItemQuit);
mnuItemQuit.addActionListener(new ListenMenuQuit());
mnuHelp.add(mnuItemAbout);
ButtonHandler phandler = new ButtonHandler();
mnuItemAbout.addActionListener(phandler);
mnuCreate.add(mnuItemFinish);
ButtonHandler1 fhandler = new ButtonHandler1();
mnuItemFinish.addActionListener(fhandler);
mb.add(mnuFile);
mb.add(mnuCreate);
mb.add(mnuHelp);
f.getContentPane().setLayout(new BorderLayout());
f.addWindowListener(new ListenCloseWdw());
}
>public class TextFieldSet implements ActionListener{
>JTextField tf=new JTextField(20);
>JPanel tfPanel = new JPanel(new GridLayout(1,1));
>public void actionPerformed(ActionEvent e){
>JTextArea text1 = new JTextArea("Write Your Text", 15, 40);
>JPanel textAreaPanel = new JPanel(new BorderLayout());
>textAreaPanel.add(text1, BorderLayout.CENTER);
>tfPanel.setText("Write your text here:");
>tfPanel.add(tf);
>}
>}
在这里,我只想在单击此新 Html 按钮时添加文本字段。我尝试使用此代码。但这不起作用。怎么做?
class ButtonHandler1 implements ActionListener{ >For the Final HTML tag
public void actionPerformed(ActionEvent e){
outputHtml="</html>";
//Here i need help. i have to add </html> tag when the finish button is clicked. But this code is not working. What should i do?***
}
}
public class ListenMenuQuit implements ActionListener{ *>For the Exit*
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class ButtonHandler implements ActionListener{ *>For displaying the Help*
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "It supports GUI environment to enter your choices.", "HELP", JOptionPane. INFORMATION_MESSAGE);
}
}
public class ListenCloseWdw extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public void launchFrame(){ *>Launches the Frame*
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,500);
f.setVisible(true);
f.setLocationRelativeTo(null);
}
public static void main(String args[]){ *>Main Method*
BasicGuiOnlyText gui = new BasicGuiOnlyText();
gui.launchFrame();
try {
OutputStream htmlfile= new FileOutputStream(new File("test.html"));*>For >the creation of HTML file*
PrintStream printhtml = new PrintStream(htmlfile);
printhtml.println(outputHtml);
printhtml.close();
htmlfile.close();
}
catch (Exception e) {}
}
}