文本区域超出窗口区域

huangapple go评论65阅读模式
英文:

Text area exceeds the window area

问题

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 ");
            }
        }
    }
}
英文:

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. 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 ");
}			
}	
}		 
}

答案1

得分: 1

如果您的目标仅仅是在文本区域中启用自动换行功能,那么您可以使用 JTextArea 的内置函数 setLineWrap

将布尔值参数设为 true,例如 setLineWrap(true),将会为 JTextArea 组件启用自动换行功能。将布尔值参数设为 false,将会关闭自动换行功能。

在您的代码中,使用方式如下:

output.setLineWrap(true);

然后,Converter 构造函数将如下所示:

public Converter() {
    
    // 打开自动换行功能。
    output.setLineWrap(true);
    
    setLayout(null);
    setSize(300, 400);
    // 左-下-宽-高
    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);
}
英文:

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.

Passing a true boolean value as a parameter to setLineWrap such as setLineWrap(true) will turn on line wrapping for the JTextArea component. Passing a false boolean value as a parameter will turn off line wrapping

In your code, it would be used as follows.

output.setLineWrap(true);

The Converter constructor will then look as follows.

	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);
}

huangapple
  • 本文由 发表于 2020年9月22日 22:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64011494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定