0
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) {}  
    }  
    }  
4

1 回答 1

1

I hope I did understood your question correctly. See below for an example which adds the text "Hello world!" to a JTextField when the JButton is pressed.

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AppendTextDemo {

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame testFrame = new JFrame( "AppendTextDemo" );

        JPanel content = new JPanel( new BorderLayout(  ) );

        JTextField textField = new JTextField( 20 );
        content.add( textField, BorderLayout.CENTER );

        JButton appendButton = new JButton( "Append text" );
        content.add( appendButton, BorderLayout.SOUTH );
        appendButton.addActionListener( new AppendTextActionListener( "Hello world!", textField ) );

        testFrame.setContentPane( content );
        testFrame.pack();
        testFrame.setVisible( true );
        testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
    } );
  }

  private static class AppendTextActionListener implements ActionListener{
    private final String textToAppend;
    private final JTextComponent textComponent;

    private AppendTextActionListener( String textToAppend, JTextComponent textComponent ) {
      this.textToAppend = textToAppend;
      this.textComponent = textComponent;
    }

    @Override
    public void actionPerformed( ActionEvent e ) {
      Document document = textComponent.getDocument();
      try {
        document.insertString( document.getLength(), textToAppend, null );
      } catch ( BadLocationException e1 ) {
        e1.printStackTrace();//do something useful with the exception
      }
    }
  }

}

If this was not what you meant, please clarify your question

于 2012-07-21T07:46:40.117 回答