Creating The String* p-6ren">
gpt4 book ai didi

java - 如何将文本字段添加到 java swing 中按钮的 Action 监听器?我也有关于字符串的疑问

转载 作者:行者123 更新时间:2023-12-01 15:16:06 25 4
gpt4 key购买 nike

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

最佳答案

我希望我正确理解了你的问题。请参阅下面的示例,其中添加了文本“Hello world!”当按下 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
}
}
}

}

如果这不是您的意思,请澄清您的问题

关于java - 如何将文本字段添加到 java swing 中按钮的 Action 监听器?我也有关于字符串的疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11590137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com