我正在尝试用Java设计Mile to Nautical Mile计算器。
我能够通过控制台创建并运行它,但我在学习如何在JFrame中进行操作时遇到了麻烦。
我基本上想要做的是:有两个文本字段和一个按钮,一个文本字段英里和一个海里,当您将金额输入一个然后按下按钮时,结果将显示在另一个字段中。
我已将代码粘贴在
下面不确定下一步该做什么
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) {
}
}
答案 0 :(得分:3)
欢迎来到可变字体,分辨率和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 :(得分:1)
在actionPerformed
方法中编写转换代码:
JTextField
英里JTextField
s getText()
方法返回String
)JTextField
方法nautical.setText();
航海
答案 2 :(得分:0)
这是我的版本。它可能并不完美,也没有任何异常处理:
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 + "");
}
}
}