如何构建 JTextfield,以及如何使用方法 selectAll()
How to construct a JTextfield, and how to use the method selectAll()
我想构建一个Swing组件JTextField,这是我的代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JTextFieldGui{
JTextField textField;
JLabel labelInput;
JLabel labelOutput;
public static void main(String[] args) {
JTextFieldGui gui = new JTextFieldGui();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
JPanel panelInput = new JPanel();
JPanel panelOutput = new JPanel();
labelInput = new JLabel("Your first name: ");
labelOutput = new JLabel("Enter your name, and you will see it here.");
textField = new JTextField(20);
JButton enter = new JButton("Enter");
JButton selectAll = new JButton("Select all text");
frame.setSize(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelInput.setLayout(new BoxLayout(panelInput, BoxLayout.X_AXIS));
textField.addActionListener(new LabelActionListener());
enter.addActionListener(new LabelActionListener());
selectAll.addActionListener(new TextFieldActionlistener());
frame.getContentPane().add(BorderLayout.NORTH, panelInput);
panelInput.add(labelInput);
panelInput.add(textField);
panelInput.add(enter);
panelInput.add(selectAll);
frame.getContentPane().add(BorderLayout.CENTER, panelOutput);
panelOutput.add(labelOutput);
}
class LabelActionListener implements ActionListener{
public void actionPerformed(ActionEvent event){
labelOutput.setText(textField.getText());
}
}
class TextFieldActionlistener implements ActionListener{
public void actionPerformed(ActionEvent event){
textField.selectAll();
}
}
}
问题1:我把文本域的宽度定义为20列,但是总是占一行,比如图片:
问题2:如何使用selectAll()方法,我在按钮selectAll的监听器中使用,但是点击按钮没有任何反应,为什么
I define the width of the text field in 20 columns, but it always take up a row,
这是BoxLayout的规则。调整组件大小以填充 space 可用空间。 JTextField 没有最大大小,因此它会增长。按钮和标签确实有最大尺寸,因此它们不会变大。
不要使用 BoxLayout,只使用 FlowLayout
。它会自动在每个组件之间留出 space,这是一个更好的布局。
I use it in a listener of the button selectAll, but when I click the button, nothing happens, why
焦点仍在按钮上。所选文本仅在文本字段具有焦点时显示。
所以在他的监听器代码中你需要添加:
textField.requestFocusInWindow();
以下代码是旧的:
frame.getContentPane().add(BorderLayout.NORTH, panelInput);
您不需要获取内容窗格。您可以将组件添加到框架中。
约束应该是第二个参数
有新的约束使名称更有意义
所以代码应该是:
frame.add(panelInput, BorderLayout.PAGE_START, panelInput);
有关详细信息,请参阅 How to Use BorderLayout 上的 Swing 教程部分。
我想构建一个Swing组件JTextField,这是我的代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JTextFieldGui{
JTextField textField;
JLabel labelInput;
JLabel labelOutput;
public static void main(String[] args) {
JTextFieldGui gui = new JTextFieldGui();
gui.go();
}
public void go(){
JFrame frame = new JFrame();
JPanel panelInput = new JPanel();
JPanel panelOutput = new JPanel();
labelInput = new JLabel("Your first name: ");
labelOutput = new JLabel("Enter your name, and you will see it here.");
textField = new JTextField(20);
JButton enter = new JButton("Enter");
JButton selectAll = new JButton("Select all text");
frame.setSize(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelInput.setLayout(new BoxLayout(panelInput, BoxLayout.X_AXIS));
textField.addActionListener(new LabelActionListener());
enter.addActionListener(new LabelActionListener());
selectAll.addActionListener(new TextFieldActionlistener());
frame.getContentPane().add(BorderLayout.NORTH, panelInput);
panelInput.add(labelInput);
panelInput.add(textField);
panelInput.add(enter);
panelInput.add(selectAll);
frame.getContentPane().add(BorderLayout.CENTER, panelOutput);
panelOutput.add(labelOutput);
}
class LabelActionListener implements ActionListener{
public void actionPerformed(ActionEvent event){
labelOutput.setText(textField.getText());
}
}
class TextFieldActionlistener implements ActionListener{
public void actionPerformed(ActionEvent event){
textField.selectAll();
}
}
}
问题1:我把文本域的宽度定义为20列,但是总是占一行,比如图片:
问题2:如何使用selectAll()方法,我在按钮selectAll的监听器中使用,但是点击按钮没有任何反应,为什么
I define the width of the text field in 20 columns, but it always take up a row,
这是BoxLayout的规则。调整组件大小以填充 space 可用空间。 JTextField 没有最大大小,因此它会增长。按钮和标签确实有最大尺寸,因此它们不会变大。
不要使用 BoxLayout,只使用 FlowLayout
。它会自动在每个组件之间留出 space,这是一个更好的布局。
I use it in a listener of the button selectAll, but when I click the button, nothing happens, why
焦点仍在按钮上。所选文本仅在文本字段具有焦点时显示。
所以在他的监听器代码中你需要添加:
textField.requestFocusInWindow();
以下代码是旧的:
frame.getContentPane().add(BorderLayout.NORTH, panelInput);
您不需要获取内容窗格。您可以将组件添加到框架中。
约束应该是第二个参数
有新的约束使名称更有意义
所以代码应该是:
frame.add(panelInput, BorderLayout.PAGE_START, panelInput);
有关详细信息,请参阅 How to Use BorderLayout 上的 Swing 教程部分。