import java.awt.*
import java.awt.event.*
import javax.swing.*
import javax.swing.border.*
public class carshop extends JFrame
{
// JPanel to hold all pictures
private JPanel windowJPanel
private String[] cars = { "","阿斯顿马丁", "美洲虎", "凯迪拉克",
"罗孚", "劳斯莱斯","别克"}
private int[] jiage = { 0,150000, 260000, 230000,
140000, 290000, 150000}
// JLabels for first snack shown
private JLabel oneJLabel
private JLabel oneIconJLabel
// JLabels for second snack shown
private JLabel twoJLabel
private JLabel twoIconJLabel
// JLabels for third snack shown
private JLabel threeJLabel
private JLabel threeIconJLabel
// JLabels for fourth snack shown
private JLabel fourJLabel
private JLabel fourIconJLabel
// JLabels for fifth snack shown
private JLabel fiveJLabel
private JLabel fiveIconJLabel
// JLabels for sixth snack shown
private JLabel sixJLabel
private JLabel sixIconJLabel
// JTextField for displaying snack price
private JTextArea displayJTextArea
// JLabel and JTextField for user input
private JLabel inputJLabel
private JComboBox selectCountryJComboBox
private JLabel inputJLabel2
private JTextField inputJTextField2
// JButton to enter user input
private JButton enterJButton
//JButton to clear the components
private JButton clearJButton
// no-argument constructor
public carshop()
{
createUserInterface()
}
// create and position GUI componentsregister event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane()
// enable explicit positioning of GUI components
contentPane.setLayout( null )
// set up windowJPanel
windowJPanel = new JPanel()
windowJPanel.setBounds( 10, 20, 340, 200 )
windowJPanel.setBorder( new LineBorder( Color.BLACK ) )
windowJPanel.setLayout( null )
contentPane.add( windowJPanel )
// set up oneIconJLabel
oneIconJLabel = new JLabel()
oneIconJLabel.setBounds( 10, 20, 100, 65 )
oneIconJLabel.setIcon( new ImageIcon( "images/阿斯顿马丁.jpg" ) )
windowJPanel.add( oneIconJLabel )
// set up oneJLabel
oneJLabel = new JLabel()
oneJLabel.setBounds( 10, 60, 100, 70 )
oneJLabel.setText( "阿斯顿马丁" )
oneJLabel.setHorizontalAlignment( JLabel.CENTER )
windowJPanel.add( oneJLabel )
// set up twoIconJLabel
twoIconJLabel = new JLabel()
twoIconJLabel.setBounds( 120, 20, 100, 65 )
twoIconJLabel.setIcon( new ImageIcon( "images/美洲虎.jpg" ) )
windowJPanel.add( twoIconJLabel )
// set up twoJLabel
twoJLabel = new JLabel()
twoJLabel.setBounds( 110, 60, 100, 70 )
twoJLabel.setText( "美洲虎" )
twoJLabel.setHorizontalAlignment( JLabel.CENTER )
windowJPanel.add( twoJLabel )
// set up threeIconJLabel
threeIconJLabel = new JLabel()
threeIconJLabel.setBounds( 230, 20, 100, 65 )
threeIconJLabel.setIcon( new ImageIcon(
"images/凯迪拉克.jpg" ) )
windowJPanel.add( threeIconJLabel )
// set up threeJLabel
threeJLabel = new JLabel()
threeJLabel.setBounds( 230, 60, 100, 70)
threeJLabel.setText( "凯迪拉克" )
threeJLabel.setHorizontalAlignment( JLabel.CENTER )
windowJPanel.add( threeJLabel )
// set up fourIconJLabel
fourIconJLabel = new JLabel()
fourIconJLabel.setBounds( 10, 100, 100, 65 )
fourIconJLabel.setIcon( new ImageIcon( "images/罗孚.jpg" ) )
windowJPanel.add( fourIconJLabel )
// set up fourJLabel
fourJLabel = new JLabel()
fourJLabel.setBounds( 10, 150, 50, 70 )
fourJLabel.setText( "罗孚" )
fourJLabel.setHorizontalAlignment( JLabel.CENTER )
windowJPanel.add( fourJLabel )
// set up fiveIconJLabel
fiveIconJLabel = new JLabel()
fiveIconJLabel.setBounds( 120, 100, 100, 65 )
fiveIconJLabel.setIcon( new ImageIcon(
"images/劳斯莱斯.jpg" ) )
windowJPanel.add( fiveIconJLabel )
// set up fiveJLabel
fiveJLabel = new JLabel()
fiveJLabel.setBounds( 110, 150, 100, 70 )
fiveJLabel.setText( "劳斯莱斯" )
fiveJLabel.setHorizontalAlignment( JLabel.CENTER )
windowJPanel.add( fiveJLabel )
// set up sixIconJLabel
sixIconJLabel = new JLabel()
sixIconJLabel.setBounds( 230, 100, 100, 65 )
sixIconJLabel.setIcon( new ImageIcon( "images/别克.jpg" ) )
windowJPanel.add( sixIconJLabel )
// set up sixJLabel
sixJLabel = new JLabel()
sixJLabel.setBounds( 230, 150, 100, 70 )
sixJLabel.setText( "别克" )
sixJLabel.setHorizontalAlignment( JLabel.CENTER )
windowJPanel.add( sixJLabel )
//set up enterJButton
enterJButton = new JButton()
enterJButton.setBounds( 390, 160, 135, 30 )
enterJButton.setText( "Enter" )
contentPane.add( enterJButton )
enterJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when enterJButton is clicked
public void actionPerformed( ActionEvent event )
{
enterJButtonActionPerformed( event )
}
} // end anonymous inner class
)// end call to addActionListener
//set up clearJButton
clearJButton = new JButton()
clearJButton.setBounds( 390, 200, 135, 30 )
clearJButton.setText( "Clear" )
contentPane.add( clearJButton )
// set up inputJLabel
inputJLabel = new JLabel()
inputJLabel.setBounds( 390, 25, 135, 25 )
inputJLabel.setText( "Please make selection:" )
contentPane.add( inputJLabel )
selectCountryJComboBox = new JComboBox( cars )
selectCountryJComboBox.setBounds( 390, 50, 135, 21 )
selectCountryJComboBox.setMaximumRowCount( 3 )
contentPane.add( selectCountryJComboBox )
// set up inputJTextField
inputJLabel2 = new JLabel()
inputJLabel2.setBounds( 390, 80, 150, 20 )
inputJLabel2.setText( "Input the Numble:" )
contentPane.add( inputJLabel2 )
// set up inputJTextField
inputJTextField2 = new JTextField()
inputJTextField2.setBounds( 390, 100, 135, 25 )
inputJTextField2.setHorizontalAlignment( JTextField.RIGHT )
contentPane.add( inputJTextField2 )
clearJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is clicked
public void actionPerformed( ActionEvent event )
{
clearJButtonActionPerformed( event )
}
} // end anonymous inner class
)
// set up displayJTextField
displayJTextArea = new JTextArea()
displayJTextArea.setBounds( 10, 237,515, 70 )
displayJTextArea.setEditable( false )
contentPane.add( displayJTextArea )
// set properties of application's window
setTitle( "My car Shop" )// set title bar string
setSize( 550, 360 ) // set window size
setVisible( true ) // display window
} // end method createUserInterface
private void clearJButtonActionPerformed( ActionEvent event )
{
// clear the JTextFields
inputJTextField2.setText( "" )
displayJTextArea.setText("")
} // end method clearJButtonActionPerformed
private void enterJButtonActionPerformed( ActionEvent event )
{
double z
double c
int x
int y
x=selectCountryJComboBox.getSelectedIndex()
y=Integer.parseInt(inputJTextField2.getText())
double discountRate
int amount = Integer.parseInt( inputJTextField2.getText())
switch (amount/5)
{
case 0:
discountRate = 0
break
case 1:
discountRate = 1
break
case 2:
discountRate = 2
break
case 3:
discountRate = 3
break
default:
discountRate = 4
} // end switch statement
c=1-discountRate/100
z=jiage[x]*y*c
displayJTextArea.append("你选择的是:"+cars[x]+";"+
"它的单价是:"+jiage[x]+";" +"你购买该产品的数量是:"+y+"," +"\n"+"该数量的折扣是:"
+discountRate + " %"+";"+"本次消费的总价格是:"+z+"元"+"!"+"\n")
}
public static void main( String args[] )
{
carshop application = new carshop()
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE )
} // end method main
} // end class carshop
mport java.util.*public class HuiWen
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in)
System.out.println("please input a String:")
String st=in.nextLine()
String s=st.toLowerCase()
int i=0
int j=s.length()-1
boolean t=true
char first=s.charAt(i)
char last=s.charAt(j)
for(i<j)
{
while(!(first>='a'&&first<='z'))
{
i++
first=s.charAt(i)
}
while(!(last>='a'&&last<='z'))
{
j--
last=s.charAt(i)
}
if(i>=j)
break
if(first==last)
{
i++
j--
first=s.charAt(i)
last=s.charAt(j)
}
else
{
t=false
break
}
}
if(t)
System.out.println(st+"是回文串")
else
System.out.println(st+"不是回文串")
}
}
//判断是否为回文字串
//什么实用的程序,说具体点!你要计算器的程序吗?
//下面是计算器的程序,把分拿来吧!
import java.awt.*
import java.awt.event.*
import javax.swing.*
public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame=new CalculatorFrame()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setVisible(true)
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setTitle("Calculator")
CalculatorPanel panel=new CalculatorPanel()
add(panel)
pack()
}
}
class CalculatorPanel extends Panel
{
public CalculatorPanel()
{
setLayout(new BorderLayout())
result=0
lastCommand="="
start=true
flag=true
display=new JButton("0")
display.setEnabled(false)
add(display,BorderLayout.NORTH)
ActionListener insert=new InsertAction()
ActionListener command=new CommandAction()
panel=new JPanel()
panel.setLayout(new GridLayout(4,5))
addButton("7",insert)
addButton("8",insert)
addButton("9",insert)
addButton("/",command)
addButton("CE",command)
addButton("4",insert)
addButton("5",insert)
addButton("6",insert)
addButton("*",command)
addButton("Backspace",command)
addButton("1",insert)
addButton("2",insert)
addButton("3",insert)
addButton("-",command)
addButton("sqrt",command)
addButton("0",insert)
addButton(".",insert)
addButton("=",command)
addButton("+",command)
addButton("1/x",command)
add(panel,BorderLayout.CENTER)
}
private void addButton(String label,ActionListener listener)
{
JButton button=new JButton(label)
button.addActionListener(listener)
panel.add(button)
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input=event.getActionCommand()
if(start&&flag)
{
display.setText("")
start=false
}
if(flag)
display.setText(display.getText()+input)
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String command=event.getActionCommand()
if(command.equals("CE"))
{
display.setText("0")
start=true
flag=true
command="="
}
else
if(start&&flag)
{
if(command.equals("-"))
{
display.setText(command)
start=false
}
else
if((command.equals("1/x")||command.equals("sqrt"))&&flag)
calculate(Double.parseDouble(display.getText()),command)
else
if(flag)
lastCommand=command
}
else
{
if(command.equals("Backspace")&&flag)
{
String s=display.getText()
char[] s1=s.toCharArray()
if(s.length()>=2)
{
String s2=new String(s1,0,s.length()-1)
display.setText(s2)
}
else
{
display.setText("0")
start=true
}
}
else if(flag)
{
calculate(Double.parseDouble(display.getText()),command)
lastCommand=command
start=true
}
}
}
}
public void calculate(double x,String command)
{
if(lastCommand.equals("+")) result+=x
else if(lastCommand.equals("-")) result-=x
else if(lastCommand.equals("/"))
{
if(x!=0)
result/=x
else
{
display.setText("除数不能为0")
start=false
flag=false
return
}
}
else if(lastCommand.equals("*")) result*=x
else if(command.equals("1/x"))
{
if(x!=0)
result=1/x
else
{
display.setText("除数不能为0")
start=false
flag=false
return
}
}
else if(command.equals("sqrt"))
{
if(x>=0)
result=Math.sqrt(x)
else
{
display.setText("函数输入无效")
start=false
flag=false
return
}
}
else if(lastCommand.equals("=")) result=x
display.setText(""+result)
}
private JButton display
private JPanel panel
private double result
private String lastCommand
private boolean start
private boolean flag
}
也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里。经测试可以运行。//Start.java
import java.awt.*
import javax.swing.*
class Start{
public static void main(String [] args){
DateFrame frame=new DateFrame()
frame.setLocationRelativeTo(frame)
frame.setResizable(false)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setVisible(true)
}
}
//DateInfo.java
import java.util.*
public class DateInfo{
private int mYear, mMonth
private int mDayOfMonth, mFristWeek
public DateInfo(int year, int month) throws DateException{
mYear = year
if (month <0 || month >12){
throw (new DateException())
}
mMonth = month
mDayOfMonth = getDayOfMonth(mYear, mMonth)
mFristWeek = getFristWeek(mYear, mMonth)
}
private int getDayOfMonth(int year, int month){
int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}
return (ary[isLeapYear(year)][month])
}
private int isLeapYear(int year){
if (year % 4 == 0 &&year % 100 != 0 ||year % 400 == 0){
return (1)
}
else{
return (0)
}
}
private int getFristWeek(int year, int month){
java.util.Calendar cal = Calendar.getInstance()
cal.set(year, month - 1, 1)
return (cal.get(Calendar.DAY_OF_WEEK) - 1)
}
public String toString(){
String str
str = "\t\t" + mYear + "年" + mMonth + "月\n"
str += "日\t一\t二\t三\t四\t五\t六\n"
int i
for (i = 1i <= mFristWeeki++){
str += " \t"
}
for (int j = 1j <= mDayOfMonthj++, i++){
str +=j+"\t"
if (i % 7 == 0){
str += "\n"
}
}
return (str)
}
}
//DateFrame.java
import java.awt.*
import java.awt.event.*
import javax.swing.*
import java.util.Calendar
class DateFrame extends JFrame implements Runnable{
Calendar date=Calendar.getInstance()
String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"}
JLabel lblYear=new JLabel("年 ")
JLabel lblMonth=new JLabel("月 ")
JLabel lblDate=new JLabel("现在的时间是:")
JLabel lblShowDate=new JLabel()
// javax.swing.JTextField trxt =new JTextField(10)
// trxt.setHorizontalAlignment(JTextField.RIGHT) //设置文本从右边输入
JComboBox cboMonth=new JComboBox(str)
JComboBox cboYear=new JComboBox()
JTextArea txaShow=new JTextArea()
JPanel pnlNorth=new JPanel()
JPanel pnlSOUTH=new JPanel()
JButton btnShow=new JButton("显示")
JButton btnClose=new JButton("关闭")
JScrollPane jsp=new JScrollPane(txaShow)
Container c=this.getContentPane()
public DateFrame(){
Thread thread=new Thread(this)
thread.start()
this.setTitle("玩玩日历拉!!!")
this.setSize(300,260)
for (int i = 1990i<=2025i++) {
cboYear.addItem(""+i)
}
cboYear.setSelectedItem(""+(date.get(Calendar.YEAR)))
cboMonth.setSelectedItem(""+(date.get(Calendar.MONTH)+1))
pnlNorth.add(cboYear)
txaShow.setTabSize(4) //设置tab键的距离
txaShow.setForeground(Color.GREEN)
pnlNorth.add(lblYear)
pnlNorth.add(cboMonth)
pnlNorth.add(lblMonth)
pnlNorth.add(lblDate)
pnlNorth.add(lblShowDate)
c.add(pnlNorth,BorderLayout.NORTH)
c.add(jsp)
pnlSOUTH.add(btnShow)
pnlSOUTH.add(btnClose)
c.add(pnlSOUTH,BorderLayout.SOUTH)
btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int year=Integer.parseInt((String)cboYear.getSelectedItem())
int month=Integer.parseInt((String)cboMonth.getSelectedItem())
try {
DateInfo date=new DateInfo(year,month)
txaShow.setText(""+date)
}
catch (DateException ex) {
ex.printStackTrace()
}
}
})
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0)
}
})
}
public void run(){
try {
while(true){
Thread.sleep(1000)
int hour=date.get(Calendar.HOUR)
int minute=date.get(Calendar.MINUTE)
int second=date.get(Calendar.SECOND)
String str=hour+":"+minute+":"+second
lblShowDate.setText(str)
//this.repaint()
}
}
catch (Exception ex) {
ex.printStackTrace()
}
}
}
//DateException.java
public class DateException extends Exception{
public DateException(){
super("日期数据不合法.")
}
}