[英]How can i add textfields to an actionlistener of a button in java swing? i have a query regarding strings as well
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);
>}
>}
Here i want to add the text field only when this new Html button is clicked. 在这里,我只想在单击此新的HTML按钮时添加文本字段。 I tried with this code. 我尝试使用此代码。 But this is not working. 但这是行不通的。 How it can be done? 怎么做?
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) {}
}
}
I hope I did understood your question correctly. 我希望我确实正确理解了您的问题。 See below for an example which adds the text "Hello world!" 参见以下示例,该示例添加了文本“ Hello world!”。 to a JTextField
when the JButton
is pressed. 按下JButton
时,将其添加到JTextField
。
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 如果这不是您的意思,请澄清您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.