英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论