java计算器16个按钮(要用for循环打出来的)源代码
自己写的,给你参考参考import java.awt.BorderLayout;
站在用户的角度思考问题,与客户深入沟通,找到社旗网站设计与社旗网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、成都网站制作、企业官网、英文网站、手机端网站、网站推广、申请域名、网页空间、企业邮箱。业务覆盖社旗地区。
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;public class Counter implements ActionListener{ /**
* @param args
*/
private JFrame frame;
private JTextField field;
private JButton clear_but;
private JButton[] buttons;
String str="123+456-789*0.=/";
public Counter() {
frame=new JFrame("计算器t\t\t\t\t\t\t");
field=new JTextField(15);
clear_but=new JButton("C");
buttons=new JButton[25];
for(int i=0;istr.length();i++)
{
buttons[i]=new JButton(str.substring(i, i+1));
}
field.setText("");
}
private void init(){
addEventHandler();
frame.setLayout(new BorderLayout());
JPanel n_panel=new JPanel();
JPanel c_panel=new JPanel();
n_panel.setLayout(new FlowLayout());
frame.add(n_panel,BorderLayout.NORTH);
n_panel.add(field);
n_panel.add(clear_but);
c_panel.setLayout(new GridLayout(4,4));
for(int i=0;i16;i++)
{
c_panel.add(buttons[i]);
}
frame.add(c_panel, BorderLayout.CENTER);
}
public void showMe(){
init();
frame.setSize(300, 240);
//frame.pack();// frame.setLocation(400,360);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void addEventHandler(){
//clear_but.addActionListener(new MyActionListener());
clear_but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
field.setText("");
}
});
for(int i=0;i16;i++)
{
final int in=i;
buttons[i].addActionListener(this);
// @Override
// public void actionPerformed(ActionEvent e) {
// System.out.print(e.getActionCommand());
// //System.out.print(str.substring(in, in+1));
// }
//
}
}
String num="";
String flag="";
//@Override
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if(str.equals("=")){
Double a=Double.valueOf(num);
Double b=Double.valueOf(field.getText());
double sum=0;
if(flag.equals("+")){
sum=a+b;
field.setText(sum+"");
}
if(flag.equals("-")){
sum=a-b;
field.setText(""+sum);
}
if(flag.equals("*")){
sum=a*b;
field.setText(""+sum);
}
if(flag.equals("/")){
sum=a/b;
field.setText(""+sum);
}
System.out.println(a+flag+b+"="+sum);
}
else if(".0123456789".indexOf(str)!=-1){
String newtext=field.getText();
newtext+=str;
field.setText(newtext);
//System.out.print(str);
}
else if(str.matches("[+*-/]{1}")){
//else if("+-*/".indexOf(str)!=-1){
num=field.getText();
field.setText("");
flag=str;
// System.out.print(str);
}
}
public static void main(String[] args) {
new Counter().showMe();
}}
求java源代码。使用swing或AWT。实现功能:点击按钮,选择一个txt文本文件,并将txt中
搞定了
package com.monubia;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Txt extends javax.swing.JFrame {
private JButton jButton_open;
private JTextArea jTextArea1;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Txt inst = new Txt();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public Txt() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jButton_open = new JButton();
getContentPane().add(jButton_open);
jButton_open.setText("Open");
jButton_open.setBounds(155, 114, 92, 49);
jButton_open.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
jButton_openMouseClicked(evt);
}
});
}
{
jTextArea1 = new JTextArea();
getContentPane().add(jTextArea1);
jTextArea1.setBounds(0, 0, 384, 262);
}
pack();
setSize(400, 300);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private void jButton_openMouseClicked(MouseEvent evt) {
//点击了打开
JFileChooser open=new JFileChooser();
FileNameExtensionFilter txt= new FileNameExtensionFilter("Txt File", "txt");
open.setFileFilter(txt);
int ret=open.showOpenDialog(this);
if(ret==JFileChooser.APPROVE_OPTION)
{
jButton_open.setOpaque(false);
jButton_open.setVisible(false);
System.out.println(open.getSelectedFile().getAbsolutePath());
try {
BufferedReader br=new BufferedReader(new FileReader(open.getSelectedFile().getAbsolutePath()));
String line=null;
while((line=br.readLine())!=null)
{
jTextArea1.append(line+"\n");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
java课程设计源代码(急!!!!)
import java.awt.Color;
import java.awt.Font;
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.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
public class game21 extends JFrame {
private JLabel label_2;
private int number;
private int sum;
final JLabel label = new JLabel();
final JLabel label_1 = new JLabel();
public static void main(String[] args) {
new game21();
}
public game21() {
super("21点?!");
getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
onClick();
}
});
button.setText("出牌");
button.setBounds(170, 350, 106, 28);
getContentPane().add(button);
label.setBorder(new LineBorder(Color.black, 1, false));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("", Font.BOLD, 26));
label.setText("背面");
label.setBounds(158, 81, 137, 153);
getContentPane().add(label);
label_1.setText("你已经拥有的牌:");
label_1.setBounds(109, 22, 270, 45);
getContentPane().add(label_1);
this.setBounds(200, 300, 501, 528);
this.setVisible(true);
getContentPane().add(getLabel_2());
}
public int randNumber() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return (int) (Math.random() * 10 + 1);
}
public void onClick() {
number = this.randNumber();
this.sum += number;
label.setText("" + number);
String strTemp = this.label_1.getText();
strTemp += "" + number + " ";
label_1.setText(strTemp);
String temp = "合计:" + sum;
label_2.setText(temp);
isWin();
}
public void isWin() {
if (sum 21) {
JOptionPane.showMessageDialog(this, "你输了");
clear();
return;
} else if (sum == 21) {
JOptionPane.showMessageDialog(this, "你赢了");
clear();
return;
} else {
int i = JOptionPane.showOptionDialog(this, "是否继续?", "提示",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (i == JOptionPane.OK_OPTION) {
onClick();
} else
return;
}
}
private void clear() {
label_2.setText("合计:");
sum = 0;
number = 0;
label_1.setText("你已经拥有的牌:");
}
/**
* @return
*/
protected JLabel getLabel_2() {
if (label_2 == null) {
label_2 = new JLabel();
label_2.setText("合计:");
label_2.setBounds(313, 35, 66, 18);
}
return label_2;
}
}
真好无聊中。。
怎么用java编写一个按钮界面
新建一个窗口,然后实现一个关闭按钮”窗口的功能
import java.awt.*;
import java.awt.event.*;
public class TestWindowEvent {
public static void main (String[] args) {
new Frame88 ("WindowAdapter");
}
}
class Frame88 extends Frame {
Frame88 (String s) {
super(s);
setBounds (300,300,200,70);
setLayout (null);
setVisible (true);
addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible (false);
System.exit(0);
}
} );
}
}
网页名称:java设计按钮源代码 java按钮设置图标
链接分享:http://lswzjz.com/article/doehede.html