我想在按钮属性字段中使用text属性。这是我尝试过的代码,但它不起作用。
private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {
String btnOneText = btnOne.getText( );
txtDisplay.setText(btnOneText);
}
我建议您阅读oracle官方教程,那里有很好的示例。如何使用按钮。
我给你树立了一个榜样,让你知道你必须做什么。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TextFieldTest {
private JPanel panel;
public TextFieldTest(){
panel = new JPanel();
final JTextField textfield = new JTextField(10);
final JButton button = new JButton("Press me");
//here i add the action listener, that will listen the input event
button.addActionListener(new ActionListener(){
//this is anonymous class
@Override
public void actionPerformed(ActionEvent evt){
String text = button.getText();
textfield.setText(text);
}
});
panel.add(textfield);
panel.add(button);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Textfield example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(Boolean.TRUE);
frame.add(new TextFieldTest().panel);
//Display the window.
frame.pack();
frame.setVisible(Boolean.TRUE);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
看到它比使用gui编辑器更简单,然后你就知道你在做什么了。最好先这样做,然后在您了解要做什么之后使用netbeans gui编辑器。