Java JTextField输入和输出
我正试图用Java设计一个英里到海里的计算器 我可以通过控制台创建并运行它,但是我在学习如何在JFrames中执行时遇到了困难 我基本上想做的是: 有两个文本字段和一个按钮,一个文本字段为英里,一个文本字段为海里,当您在其中输入金额,然后按下按钮时,结果将显示在另一个字段中 我已将代码粘贴到下面 不确定下一步该怎么办Java JTextField输入和输出,java,swing,jtextfield,Java,Swing,Jtextfield,我正试图用Java设计一个英里到海里的计算器 我可以通过控制台创建并运行它,但是我在学习如何在JFrames中执行时遇到了困难 我基本上想做的是: 有两个文本字段和一个按钮,一个文本字段为英里,一个文本字段为海里,当您在其中输入金额,然后按下按钮时,结果将显示在另一个字段中 我已将代码粘贴到下面 不确定下一步该怎么办 package Mile_Conversion; import java.awt.*; import java.awt.event.ActionEvent; import jav
package Mile_Conversion;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class OwnWindow implements ActionListener{
public static void main(String[] args) {
MyJFrame();
}
public static void MyJFrame() {
//JLabel
JLabel start = new JLabel("Enter the amount below to calculate");
start.setBounds(/*x*/70, -65/*Y*/, 270, 150); // x, y, width, height x across, y down
JLabel milecon = new JLabel("Miles");
milecon.setBounds(154, 55, 50, 150);
JLabel nautcon = new JLabel("Nautical Miles");
nautcon.setBounds(135, -15, 150, 150);
//JTextField
JTextField miles = new JTextField();
miles.setBounds(100, 140, 150, 25);
JTextField nautical = new JTextField();
nautical.setBounds(100, 70, 150, 25);
double mile, nautmile;
/*
mile = nautmile * 0.868;
nautmile = mile * 1.150;
*/
//JButton
JButton convert = new JButton("Convert");
convert.setBounds(250, 200, 90, 25);
//JFrame
JFrame window = new JFrame("Milage Conversion");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 300);
window.setVisible(true);
window.setLayout(null);
window.setResizable(false);
window.add(start);
window.add(miles);
window.add(nautical);
window.add(convert);
window.add(milecon);
window.add(nautcon);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
在
actionPerformed
方法中编写转换代码:
- 从第一个
miles获取输入JTextField
- 将其解析为int或float(如
sJTextField
方法返回getText()
)String
- 进行转换
- 使用
searical.setText()将转换后的值设置为其他
searical代码>方法JTextField
操作执行
方法中编写转换代码:
- 从第一个
miles获取输入JTextField
- 将其解析为int或float(如
sJTextField
方法返回getText()
)String
- 进行转换
- 使用
searical.setText()将转换后的值设置为其他
searical代码>方法JTextField
- 这是我的版本。它可能并不完美,也没有任何异常处理:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class OwnWindow implements ActionListener
{
JLabel start;
JLabel milecon;
JLabel nautcon;
JTextField miles;
JTextField nautical;
JButton convert;
JFrame window;
double mile, nautmile;
public static void main(String[] args)
{
OwnWindow obj = new OwnWindow();
}
public OwnWindow()
{
start = new JLabel("Enter the amount below to calculate");
start.setBounds(/*x*/70, -65/*Y*/, 270, 150); // x, y, width, height x across, y down
milecon = new JLabel("Miles");
milecon.setBounds(154, 55, 50, 150);
nautcon = new JLabel("Nautical Miles");
nautcon.setBounds(135, -15, 150, 150);
miles = new JTextField();
miles.setBounds(100, 140, 150, 25);
nautical = new JTextField();
nautical.setBounds(100, 70, 150, 25);
convert = new JButton("Convert");
convert.setBounds(250, 200, 90, 25);
convert.addActionListener(this);
window = new JFrame("Milage Conversion");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 300);
window.setVisible(true);
window.setLayout(null);
window.setResizable(false);
window.add(start);
window.add(miles);
window.add(nautical);
window.add(convert);
window.add(milecon);
window.add(nautcon);
}
@Override
public void actionPerformed(ActionEvent e)
{
String strNaut = nautical.getText();
String strMile = miles.getText();
//this means the user wants to convert nautical to normal
if(strNaut.length() > 0)
{
nautmile = Double.parseDouble(strNaut);
miles.setText(nautmile*0.868 + "");
}
else if(strMile.length() > 0)
{
mile = Double.parseDouble(strMile);
nautical.setText(mile*1.150 + "");
}
}
}
这是我的版本。它可能并不完美,也没有任何异常处理:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class OwnWindow implements ActionListener
{
JLabel start;
JLabel milecon;
JLabel nautcon;
JTextField miles;
JTextField nautical;
JButton convert;
JFrame window;
double mile, nautmile;
public static void main(String[] args)
{
OwnWindow obj = new OwnWindow();
}
public OwnWindow()
{
start = new JLabel("Enter the amount below to calculate");
start.setBounds(/*x*/70, -65/*Y*/, 270, 150); // x, y, width, height x across, y down
milecon = new JLabel("Miles");
milecon.setBounds(154, 55, 50, 150);
nautcon = new JLabel("Nautical Miles");
nautcon.setBounds(135, -15, 150, 150);
miles = new JTextField();
miles.setBounds(100, 140, 150, 25);
nautical = new JTextField();
nautical.setBounds(100, 70, 150, 25);
convert = new JButton("Convert");
convert.setBounds(250, 200, 90, 25);
convert.addActionListener(this);
window = new JFrame("Milage Conversion");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 300);
window.setVisible(true);
window.setLayout(null);
window.setResizable(false);
window.add(start);
window.add(miles);
window.add(nautical);
window.add(convert);
window.add(milecon);
window.add(nautcon);
}
@Override
public void actionPerformed(ActionEvent e)
{
String strNaut = nautical.getText();
String strMile = miles.getText();
//this means the user wants to convert nautical to normal
if(strNaut.length() > 0)
{
nautmile = Double.parseDouble(strNaut);
miles.setText(nautmile*0.868 + "");
}
else if(strMile.length() > 0)
{
mile = Double.parseDouble(strMile);
nautical.setText(mile*1.150 + "");
}
}
}
欢迎来到变化多端的字体、分辨率和DPI的奇妙世界,在这里,屏幕上的显示永远无法与其他任何一个相匹配(好吧,这有点夸张,但感觉就像:p) 在这个世界上,布局经理是你的朋友。它们将为你节省很多很多很多小时的挫折和折磨 我建议你通读一遍
public class TestConvertDistance {
public static void main(String[] args) {
new TestConvertDistance();
}
public TestConvertDistance() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new DistancePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class DistancePane extends JPanel {
private JLabel lblInstruct;
private JLabel lblMiles;
private JLabel lblNauticalMiles;
private JTextField fldMiles;
private JTextField fldNauticalMiles;
private JButton btnCalculate;
public DistancePane() {
lblInstruct = new JLabel("Enter the amount below to calculate");
lblMiles = new JLabel("Miles");
lblNauticalMiles = new JLabel("Nautical Miles");
fldMiles = new JTextField(8);
fldNauticalMiles = new JTextField(8);
btnCalculate = new JButton("Convert");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(lblInstruct, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 1;
add(lblMiles, gbc);
gbc.gridy++;
add(lblNauticalMiles, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
add(fldMiles, gbc);
gbc.gridy++;
add(fldNauticalMiles, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btnCalculate, gbc);
btnCalculate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Do your calculation here...
}
});
}
}
}
注意,我使用了
GridBagLayout
,这是核心API中功能最强大、最复杂的布局管理器之一,所以如果一开始看起来很混乱,请不要担心;) 欢迎来到变化多端的字体、分辨率和DPI的奇妙世界,在这里,屏幕上的显示永远无法与其他任何一个匹配(好吧,这有点夸张,但感觉就像这样:p)
在这个世界上,布局经理是你的朋友。它们将为你节省很多很多很多小时的挫折和折磨
我建议你通读一遍
public class TestConvertDistance {
public static void main(String[] args) {
new TestConvertDistance();
}
public TestConvertDistance() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new DistancePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class DistancePane extends JPanel {
private JLabel lblInstruct;
private JLabel lblMiles;
private JLabel lblNauticalMiles;
private JTextField fldMiles;
private JTextField fldNauticalMiles;
private JButton btnCalculate;
public DistancePane() {
lblInstruct = new JLabel("Enter the amount below to calculate");
lblMiles = new JLabel("Miles");
lblNauticalMiles = new JLabel("Nautical Miles");
fldMiles = new JTextField(8);
fldNauticalMiles = new JTextField(8);
btnCalculate = new JButton("Convert");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(lblInstruct, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 1;
add(lblMiles, gbc);
gbc.gridy++;
add(lblNauticalMiles, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
add(fldMiles, gbc);
gbc.gridy++;
add(fldNauticalMiles, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btnCalculate, gbc);
btnCalculate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Do your calculation here...
}
});
}
}
}
注意,我使用了
GridBagLayout
,这是核心API中功能最强大、最复杂的布局管理器之一,所以如果一开始看起来很混乱,请不要担心;) 1)使用2)不要使用null
/Absolute
布局1)使用2)不要使用null
/Absolute
布局足够公平,但是OP在代码中有一些主要缺陷,比如在添加组件之前设置可见的框架,当然您可以将其添加到重构中,从而帮助OP更进一步谢谢,这是很容易遵循和它的工作为我现在!2个问题:1@DavidKroukamp,在添加组件之前将框架设置为可见有什么缺点?2.如果我进行了一次计算并希望在之后执行另一次计算,那么在输入第一个文本框时,如何清除第二个文本框?i、 e-我将计算结果输入英里,然后在海里字段中得到结果,当我点击英里字段时,如何清除海里字段,反之亦然?这很公平,但OP在代码中有一些主要缺陷,比如在添加组件之前设置可见的框架,当然你可以将其添加到重构中,因此对OP的帮助更大。谢谢,这很容易理解,现在对我来说很有用!2个问题:1@DavidKroukamp,在添加组件之前将框架设置为可见有什么缺点?2.如果我进行了一次计算并希望在之后执行另一次计算,那么在输入第一个文本框时,如何清除第二个文本框?i、 e-我输入一个以英里为单位的计算结果,并在“海里”字段中得到结果,当我单击返回到“英里”字段时,如何清除“海里”字段,反之亦然?谢谢您的帮助。你说的布局令人困惑是对的!我现在一直在遭受挫折,直到我的头脑清醒过来!谢谢你的帮助。你说的布局令人困惑是对的!我现在一直在遭受挫折,直到我的头脑清醒过来!