JTextField 的不当使用(可能)帮助创建一个 GUI
Improper use of JTextField (maybe) Help creating a GUI
我是 Java 的新手,我正在尝试创建一个反转文本的小程序(那部分我已经弄明白了)。
我卡住的地方是我的 GUI,我对 gui 的设想是 window 有一个用于用户输入的居中文本字段,然后在它下面 window 一个按钮,将上面文本框中的文本反转并输出到按钮下方的文本框中。
现在我正在使用 JTextField 框,在尝试使它们看起来像我想要的方式后,我感觉有一种更简单的方法可以做到这一点,但我不知道。
这是我的 GUI class:
public class ReverseTextGUI extends ReverseRun implements ActionListener {
public static JFrame frame;
private JPanel northFlowLayoutPanel;
private JPanel centerFlowLayoutPanel;
private JPanel southFlowLayoutPanel;
private final JButton reverse = new JButton("Reverse");
private final JTextField userInput = new JTextField(50);
private final JTextField reverseOutput = new JTextField(50);
public void actionPerformed(ActionEvent e) {
reverse.addActionListener((ActionListener) reverse);
reverse.setActionCommand("Reverse");
if ("algorithm".equals(e.getActionCommand())) {
System.out.println("test");
}
}
public void initUI() {
northFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
northFlowLayoutPanel.add(userInput);
userInput.setPreferredSize(new Dimension(150,100));
centerFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
centerFlowLayoutPanel.add(reverse);
southFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
southFlowLayoutPanel.setBorder(BorderFactory.createTitledBorder("Output text"));
southFlowLayoutPanel.add(reverseOutput);
reverseOutput.setPreferredSize(new Dimension(150,100));
JFrame frame = new JFrame("Backwardizer");
frame.setLayout(new BorderLayout()); // This is the default layout
frame.add(northFlowLayoutPanel, BorderLayout.PAGE_START);
frame.add(centerFlowLayoutPanel, BorderLayout.CENTER);
frame.add(southFlowLayoutPanel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setSize(750, 500);
}
关于如何将光标移动到框的开头(它现在显示在中间)或完成我正在尝试做的事情的更好方法有什么想法吗?
对于反转方面,您可以将第一个框中的文本添加到字符串构建器
StringBuilder rev = new StringBuilder(firstBox.getText());
String reversedText = rev.reverse().toString();
secondBox.setText(reversedText);
如果您将这些内容嵌套在按钮操作中,那么应该会得到想要的结果。
Any ideas how to either move the cursor to the start of the box (it shows up in the middle as of now) or a better way to accomplish what I'm trying to do?
JTextField#setCaretPosition
,请在更新字段文本后调用它
- 将字段设置为只读,
JTextField#setEditable
并传递它 false
此外,如果您想存储多个 rows/lines 文本
,您可以使用 JList
或 JTextArea
您还应避免使用 setPreferredSize
,请参阅 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? 了解更多详情
我是 Java 的新手,我正在尝试创建一个反转文本的小程序(那部分我已经弄明白了)。
我卡住的地方是我的 GUI,我对 gui 的设想是 window 有一个用于用户输入的居中文本字段,然后在它下面 window 一个按钮,将上面文本框中的文本反转并输出到按钮下方的文本框中。
现在我正在使用 JTextField 框,在尝试使它们看起来像我想要的方式后,我感觉有一种更简单的方法可以做到这一点,但我不知道。
这是我的 GUI class:
public class ReverseTextGUI extends ReverseRun implements ActionListener {
public static JFrame frame;
private JPanel northFlowLayoutPanel;
private JPanel centerFlowLayoutPanel;
private JPanel southFlowLayoutPanel;
private final JButton reverse = new JButton("Reverse");
private final JTextField userInput = new JTextField(50);
private final JTextField reverseOutput = new JTextField(50);
public void actionPerformed(ActionEvent e) {
reverse.addActionListener((ActionListener) reverse);
reverse.setActionCommand("Reverse");
if ("algorithm".equals(e.getActionCommand())) {
System.out.println("test");
}
}
public void initUI() {
northFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
northFlowLayoutPanel.add(userInput);
userInput.setPreferredSize(new Dimension(150,100));
centerFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
centerFlowLayoutPanel.add(reverse);
southFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
southFlowLayoutPanel.setBorder(BorderFactory.createTitledBorder("Output text"));
southFlowLayoutPanel.add(reverseOutput);
reverseOutput.setPreferredSize(new Dimension(150,100));
JFrame frame = new JFrame("Backwardizer");
frame.setLayout(new BorderLayout()); // This is the default layout
frame.add(northFlowLayoutPanel, BorderLayout.PAGE_START);
frame.add(centerFlowLayoutPanel, BorderLayout.CENTER);
frame.add(southFlowLayoutPanel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setSize(750, 500);
}
关于如何将光标移动到框的开头(它现在显示在中间)或完成我正在尝试做的事情的更好方法有什么想法吗?
对于反转方面,您可以将第一个框中的文本添加到字符串构建器
StringBuilder rev = new StringBuilder(firstBox.getText());
String reversedText = rev.reverse().toString();
secondBox.setText(reversedText);
如果您将这些内容嵌套在按钮操作中,那么应该会得到想要的结果。
Any ideas how to either move the cursor to the start of the box (it shows up in the middle as of now) or a better way to accomplish what I'm trying to do?
JTextField#setCaretPosition
,请在更新字段文本后调用它- 将字段设置为只读,
JTextField#setEditable
并传递它false
此外,如果您想存储多个 rows/lines 文本
,您可以使用JList
或 JTextArea
您还应避免使用 setPreferredSize
,请参阅 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? 了解更多详情