java Swing GUI 入门-简易货币计算器
觉得有用的话,欢迎一起讨论相互学习~
![](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404123341456-2091227860.png)
![我的微博](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404125636151-1411970006.png)
![我的github](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404125603323-1670542535.jpg)
![我的B站](https://img2020.cnblogs.com/blog/949241/202004/949241-20200404142715197-1867301511.jpg)
设计布局
- JcomboBox下拉框
- JTextField的不可编辑的属性
- 可以通过调节一个控件的Vertical Align属性以调节一个元素的位置,特别是将其设置为top可以将这个控件调得较高,默认为center
![](https://img-blog.csdnimg.cn/20201221145413171.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1NTU3MTk=,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/2020122114540131.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1NTU3MTk=,size_16,color_FFFFFF,t_70)
注意一定不要选中Custom Create
- 对于 ComboBox 控件,单击model属性
![](https://img-blog.csdnimg.cn/20201221150732776.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1NTU3MTk=,size_16,color_FFFFFF,t_70)
添加事件操作
添加下拉框事件
- 为了使下拉框变化时,JLabel中的值也能相应的变化,向下拉框添加动作监听器.
![](https://img-blog.csdnimg.cn/20201221151348179.png)
public CurrencyConverter() {
comboBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moneyTypeLabel.setText(" "+comboBox1.getSelectedItem());
}
});
}//end CurrencyConverter()
- 添加main函数,右键添加
![](https://img-blog.csdnimg.cn/20201221151850107.png)
添加按钮事件
- 思路:点击这个按钮之后将前一个TextField中的值读取出来乘以其和JCombox中国家币种得汇率,得到的结果输出到第二个TextField中.
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double[] rates = {0.1253, 15.7953, 0.1527};//Eurc,Yen,U.S.dollar
double selectedRate = rates[comboBox1.getSelectedIndex()];
double converted = Double.parseDouble(canadianField.getText()) * selectedRate;
convertedField.setText(" " + converted + " in " + comboBox1.getSelectedItem());
}
});
程序演示
完整代码
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CurrencyConverter {
private JPanel mainPanel;
private JPanel topPanel;
private JPanel bottomPanel;
private JTextField canadianField;
private JComboBox comboBox1;
private JButton convertButton;
private JTextField convertedField;
private JLabel moneyTypeLabel;
public CurrencyConverter() {
comboBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moneyTypeLabel.setText(" " + comboBox1.getSelectedItem());
}
});
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double[] rates = {0.1253, 15.7953, 0.1527};//Eurc,Yen,U.S.dollar
double selectedRate = rates[comboBox1.getSelectedIndex()];
double converted = Double.parseDouble(canadianField.getText()) * selectedRate;
convertedField.setText(" " + converted + " in " + comboBox1.getSelectedItem());
}
});
}//end CurrencyConverter()
public static void main(String[] args) {
JFrame frame = new JFrame("Currency Converter");
frame.setContentPane(new CurrencyConverter().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}//end class