1

我想阻止文本字段脱离 GUI 或在新的“行”上打印文本字段。

这是代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Window extends JFrame {
    private JTextField TextField0;
    private JTextField TextField1;
    private JCheckBox CheckBox0;
    private JPanel panel;

    //CONSTRUCTOR
    public Window() {
        super("Checkbox");
        setLayout(new FlowLayout());

        panel = new JPanel();
        add(panel, BorderLayout.CENTER);

        TextField0 = new JTextField("Add field",15);
        panel.add(TextField0);
        TextField1 = new JTextField("Add field", 15);

        CheckBox0 = new JCheckBox("");

        HandlerClass handler = new HandlerClass();
        TextField0.addActionListener(handler);
    }

    public class HandlerClass implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if(event.getSource()==TextField0) {
                CheckBox0.setText(String.format("%s",event.getActionCommand()));
                panel.remove(TextField0);
                panel.add(CheckBox0);
                panel.add(TextField1);
                panel.revalidate();
                panel.repaint();
            }
        }
    }
}
4

2 回答 2

2

你真的没有什么可以做的...

JTextField将允许文本溢出文本字段的可视区域(修剪屏幕上的文本)

您可以尝试使用JTextArea支持多行文本的

您也可以尝试包装框架

在此处输入图像描述

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTextField {

    public static void main(String[] args) {
        new TestTextField();
    }

    public TestTextField() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                String text = "This is a long piece of text that seems to go on and on and on and on an on....and some more...";

                JTextField field = new JTextField(10);
                JTextArea ta1 = new JTextArea(10, 2);
                JTextArea ta2 = new JTextArea(10, 2);

                field.setText(text);
                ta1.setText(text);
                ta2.setText(text);

                configure(ta1);
                configure(ta2);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                frame.add(field, gbc);
                frame.add(ta1, gbc);
                frame.add(new JScrollPane(ta2), gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

            protected void configure(JTextArea ta) {
                ta.setWrapStyleWord(true);
                ta.setLineWrap(true);
            }

        });
    }    
}
于 2013-05-25T01:01:49.097 回答
0

将面板的布局设置为 Boxlayout 而不是 Borderlayout。请参阅以下链接了解不同的布局以及如何使用它们:

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

于 2013-05-25T00:59:41.150 回答