📄 poundtokg.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.io.*;
import java.util.*;
import java.math.*;
public class PoundToKg
{
public static void main(String[] args)
{
PoundToKgFrame frame = new PoundToKgFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//Create Frame
class PoundToKgFrame extends JFrame
{
public PoundToKgPanel panel;
public DisplayPanel displaypanel;
public PoundToKgFrame()
{
setTitle("Weight from Pound to Kg");
setSize(DefualtX,DefualtY);
//the panel on the frame
////the top panel of the frame
panel =new PoundToKgPanel();
panel.setPreferredSize(new java.awt.Dimension(100,50));
////the bottom panel of the frame
displaypanel =new DisplayPanel();
displaypanel.setPreferredSize(new java.awt.Dimension(600,500));
//separate the panels
setLayout(new BorderLayout());
add(panel, BorderLayout.NORTH);
add(displaypanel, BorderLayout.CENTER);
//set background color to show the previous action
panel.PoundToKgButton.addActionListener(new ColorAction(Color.YELLOW));
panel.clearButton.addActionListener(new ColorAction(Color.MAGENTA));
//add action of all the buttons
panel.PoundToKgButton.addActionListener(new PoundToKgAction());
panel.clearButton.addActionListener(new clearAction());
}
public static final int DefualtX = 300;
public static final int DefualtY = 400;
//Action of PoundToKg Button
private class PoundToKgAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{ reading(); //call the function reading
}
}
//Action of Clear Button
private class clearAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{ clearDisplayText(); //call the function clearDisplayText
}
}
//Action of background color change
private class ColorAction implements ActionListener
{ public ColorAction(Color c)
{ backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{ panel.setBackground(backgroundColor);
}
private Color backgroundColor;
}
//Get data from user
public void reading()
{
String input=""; //initial the input string
input = JOptionPane.showInputDialog("Please input your weight in pound : ");
double weight= Double.parseDouble(input);
;
display("\nInput weight in pound: "+ weight);
//call the function comparison
exchange(weight);
}
//to change thw weight in kg
public void exchange(double Weight)
{ double POUND_IN_ONE_KG=2.20463;
double WeightInKg;
WeightInKg= Weight/POUND_IN_ONE_KG;
display("\nYour weight is "+WeightInKg+" Kg\n");
}
//display the output data to the textArea
public void display(String outputDisplay)
{
//append the display text
displaypanel.screen.append(outputDisplay);
}
//clear the text on the display
public void clearDisplayText()
{
//reset the display text
displaypanel.screen.setText("");
}
} //end of frame
//Create PoundToKgPanel
class PoundToKgPanel extends JPanel
{
public JTextArea screen;
public DisplayPanel displaypanel;
public PoundToKgPanel panel;
public JButton PoundToKgButton;
public JButton clearButton;
public PoundToKgPanel()
{
//Button for input the data
PoundToKgButton = new JButton("Pound To Kg");
add(PoundToKgButton,BorderLayout.NORTH);
//Button for clear display text
clearButton = new JButton("Clear");
add(clearButton,BorderLayout.NORTH);
//Button for exit
JButton ExitButton = new JButton("Exit");
add(ExitButton,BorderLayout.NORTH);
ExitButton.addActionListener(new ExitAction());
}
//Action of Exit Button
private class ExitAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{ System.exit(0); //exit the system
}
}
}//end for PoundToKgPanel
//DisplayPanel
class DisplayPanel extends JPanel
{
public JTextArea screen;
public DisplayPanel()
{
//Screen for display
setLayout(new BorderLayout());
screen = new JTextArea("Screen for display\n",25,25);
JScrollPane scroller=new JScrollPane();
add(scroller, BorderLayout.CENTER);
scroller.setViewportView(screen);
screen.setEditable(false);
screen.setLineWrap(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -