简体   繁体   English

文本区域超出窗口区域

[英]Text area exceeds the window area

I have wrote program in java the program is to convert from KM to miles and miles to KM the program works fine but the problem is in the result in text area exceeded the area so does not appear the full text below the code attached.我用java编写了程序,该程序是将公里转换为英里,将英里转换为公里,该程序工作正常,但问题在于文本区域超出了该区域,因此未在所附代码下方显示全文。 so i want if it is reached at the end of line it goes to new line所以我想如果它在行尾到达它会转到新行

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

public class Converter extends JFrame implements ActionListener {
    JLabel label = new JLabel("Distance : ");
    JTextField input = new JTextField(10);
    JButton button = new JButton("Convert");
    JTextArea output = new JTextArea(10,15);
    CheckboxGroup cbg = new CheckboxGroup();
    Checkbox cb1 = new Checkbox("Convert MILES to KM", cbg, true);
    Checkbox cb2 = new Checkbox("Convert KM to MILES", cbg, false);

    public static void main(String args[]) {
        Converter s = new Converter();
        s.setVisible(true);
    }

    public Converter() {
        setLayout(null);
        setSize(300,400);  
        //left-down-width-hegiht
        cb1.setBounds(60,30,150,30);
        cb2.setBounds(60,60,150,30);
        label.setBounds(30,90,120,30);
        input.setBounds(90,95,170,20);
        button.setBounds(100,130,90,30);
        output.setBounds(45,168,200,165);   
        add(cb1);
        add(cb2);
        add(label);
        add(input);
        add(button);
        add(output);
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (cb1.getState() ) {
            if (e.getSource() == button) {
                double d = Double.parseDouble(input.getText());
                double d2 =  d / 0.62;
                String str2 = String.valueOf(d2); 
                output.setText(d + "miles equals to " + str2 + " kilometers");      
            }       
        }

        if (cb2.getState()) {
            if (e.getSource() == button){
                double d = Double.parseDouble(input.getText());
                double d2 =  d * 0.62;
                String str2 = String.valueOf(d2);
                output.setText(d + " kilometers equals to " + str2 + " miles ");
            }           
        }   
    }        
}

If your goal is purely to have line wrapping on in the text area then you can make use of JTextArea's built-in function named setLineWrap .如果您的目标纯粹是在文本区域中进行换行,那么您可以使用JTextArea 的名为setLineWrap内置函数。

Passing a true boolean value as a parameter to setLineWrap such as setLineWrap(true) will turn on line wrapping for the JTextArea component.将 true 布尔值作为参数传递给setLineWrap,例如setLineWrap(true)将打开 JTextArea 组件的换行。 Passing a false boolean value as a parameter will turn off line wrapping将 false 布尔值作为参数传递将关闭换行

In your code, it would be used as follows.在您的代码中,它将按如下方式使用。

output.setLineWrap(true);

The Converter constructor will then look as follows. Converter构造函数将如下所示。

    public Converter() {
    
    // Turn on line wrapping.
    output.setLineWrap(true);
    
    setLayout(null);
    setSize(300, 400);
    // left-down-width-hegiht
    cb1.setBounds(60, 30, 150, 30);
    cb2.setBounds(60, 60, 150, 30);
    label.setBounds(30, 90, 120, 30);
    input.setBounds(90, 95, 170, 20);
    button.setBounds(100, 130, 90, 30);
    output.setBounds(45, 168, 200, 165);
    add(cb1);
    add(cb2);
    add(label);
    add(input);
    add(button);
    add(output);
    button.addActionListener(this);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM