I'm new to Swing. As the user types text I want it to appear in a JLabel. I am unable to access the label variable because it's in the constructor.
Also, what should I put in the main
method?
public class One extends JPanel implements KeyListener {
private JTextField textField;
/**
* Create the panel.
*/
public One() {
setLayout(null);
textField = new JTextField();
textField.setBounds(88, 81, 258, 53);
add(textField);
textField.setColumns(10);
JLabel label = new JLabel("New label");
label.setBounds(182, 198, 61, 16);
add(label);
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
label.setText(KeyEvent.getKeyText(keyCode));
}
public void keyReleased(KeyEvent e){
}
public static void main(String[] args){
// ??
}
@Override
public void keyTyped(KeyEvent e) {
}
}
There are a number of issues with your approach.
First, you should avoid using KeyListener
generally, but especially with text components, it's simply an inappropriate approach to monitoring changes to a text field. Instead, you should be using a DocumentListener
, see Listening for Changes on a Document for more details.
Second, avoid using null
layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
See Laying Out Components Within a Container for more details
The "core" problem you're having is a scope issue, basically label
has local scope to the constructor only. The basic solution is to make label
an instance field of the class. This is basic Java 101 and has nothing to do with Swing. Have a look at Understanding Class Memebers for more details.
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class TestInput {
public static void main(String[] args) {
new TestInput();
}
public TestInput() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new InputPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class InputPane extends JPanel {
private JTextField textField;
private JLabel label;
public InputPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridwidth = GridBagConstraints.REMAINDER;
textField = new JTextField(20);
label = new JLabel(" ");
add(textField, gbc);
add(label, gbc);
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
updateLabel();
}
@Override
public void removeUpdate(DocumentEvent e) {
updateLabel();
}
@Override
public void changedUpdate(DocumentEvent e) {
updateLabel();
}
protected void updateLabel() {
label.setText(textField.getText());
}
});
}
}
}
创建类的label
成员变量,并在构造initialize
中initialize
,如下所示
label = new JLabel("New label");
public class One extends JPanel implements KeyListener {
private JTextField textField;
private JLabel jLabel;
/**
* Create the panel.
*/
public One() {
setLayout(null);
textField = new JTextField();
textField.setBounds(88, 81, 258, 53);
add(textField);
textField.setColumns(10);
jLabel = new JLabel("New label");
jLabel.setBounds(182, 198, 61, 16);
add(label);
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
label.setText(KeyEvent.getKeyText(keyCode));
}
public void keyReleased(KeyEvent e){
}
public static void main(String[] args){
SwingUtilities.invokeLater(() -> {
One one = new One();
JFrame jFrame = new JFrame();
jFrame.add(one);
jFrame.pack();
jFrame.setVisible(true);
});
}
@Override
public void keyTyped(KeyEvent e) {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.