第5周:GUI编程和JDK API的使用
1、字体设置,P183例9-3字体设置,尝试添加一个文本框TextField,把目前选择的字体的名字,显示在文本框中。(类名:FontSet)
2、《Java语言程序设计》P443程序清单16-6贷款计算器。(类名:LoanCalculator)
//P183 例9-3字体设置
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FontSet
{
public static void main(String args[])
{
FontFrame frame=new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class FontFrame extends JFrame
{
public FontFrame()
{
setTitle("设置字体");
setSize(WIDTH,HEIGHT);
FontPanel panel=new FontPanel();//将panel加入到Frame
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int WIDTH=300;//显示框的大小
public static final int HEIGHT=140;
}
@SuppressWarnings("serial")
class FontPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//设置字体
Font f=new Font("宋体",Font.BOLD+Font.ITALIC,20);
g.setFont(f);
//显示文字
int x=0,y=0;
g.drawString("Java欢迎您!",x,y);
}
public int x=55;
public int y=50;
}
//贷款计算器
import java.awt. *;
import java.awt.event. *;
import java.io.File;
import java.util.concurrent.locks.Lock;
import javax.swing. *;
import javax.swing.border.TitledBorder;
import javax.swing.text.JTextComponent;
@SuppressWarnings({ "unused", "serial" })
public class LoanCalculator extends JFrame
{
private JTextField jtfAnnualInterestRate=new JTextField();
private JTextField jtfNumberOfYears=new JTextField();
private JTextField jtfLoanAmount=new JTextField();
private JTextField jtfMonthlyPayment=new JTextField();
private JTextField jtfTotlePayment=new JTextField();
private JButton jbtComputeLoan=new JButton("Compute Payment");
public LoanCalculator ()
{
JPanel p1=new JPanel(new CardLayout(5,2));
p1.add(new JLabel("Annual Interest Rate"));
p1.add(jtfAnnualInterestRate);
p1.add(new JLabel("Number Of Years"));
p1.add(jtfNumberOfYears);
p1.add(new JLabel("LoanAmount"));
p1.add(jtfLoanAmount);
p1.add(new JLabel("Monthly Payment"));
p1.add(jtfTotlePayment);
p1.add(new JLabel("Totle Payment"));
p1.add(jtfTotlePayment);
p1.setBorder(new TitledBorder("Enter Loan amount,interest rate,and year"));
JPanel p2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
jbtComputeLoan.addActionListener((ActionListener) new ButtonListener());
}
}
class ButtonListener implements ActionListener
{
@SuppressWarnings("null")
public void actionPerformed1(ActionEvent e)
{
JTextComponent jtfAnnualInterestRate = null;
double interest=Double.parseDouble(jtfAnnualInterestRate.getText());
JTextComponent jtfNumberOfYears = null;
int year=Integer.parseInt(jtfNumberOfYears.getText());
JTextComponent jtfLoanAmount = null;
@SuppressWarnings("unused")
double amount=Double.parseDouble(jtfLoanAmount.getText());
Object loadAmount = null;
@SuppressWarnings("unused")
Object loanAmount;
@SuppressWarnings("unused")
File fileinput = new File("E:\\Loan.txt");
Loan loan=new Loan(interest,year,loadAmount);
@SuppressWarnings("unused")
File fileoutput = new File("E:\\Loan.txt");
JTextComponent jtfMonthlyPayment = null;
jtfMonthlyPayment.setText(String.format("%.2f",loan.getMonthlyPayment()));
JTextComponent jtfTotlePayment = null;
jtfTotlePayment.setText(String.format("%.2f",loan.getTotlePayment()));
}
public static void main(String args[])
{
LoanCalculator frame=new LoanCalculator();
frame.pack();
frame.setTitle("LoanCalculator");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}