javajubtton的简单介绍
本篇文章给大家谈谈javajubtton,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java中如何在拷贝文件时加入进度条,进度条跟文件拷贝进度一致
- 2、java编一个计算器的代码
- 3、通过jbutton改变jpanel内容
- 4、java中JButton有没有哪个动作是用于禁用这个按钮?
java中如何在拷贝文件时加入进度条,进度条跟文件拷贝进度一致
给你个例子,你再改进下
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class JprogressBar extends JFrame implements Runnable {
public JprogressBar() {
initUI();
}
private void initUI() {
btCopy = new JButton("open file....");
JButton btCancel = new JButton("cancel");
JButton btSavePath = new JButton("save Path...");
copyFileProgressBar = new JProgressBar(0, 100);
copyFileProgressBar.setPreferredSize(new Dimension(450, 15));
copyFileProgressBar.setBackground(Color.GREEN);
copyFileProgressBar.setForeground(Color.PINK);
copyFileProgressBar.setStringPainted(true);
copyFileProgressBar.setVisible(false);
openFileDialog = new JFileChooser(".");
this.setLayout(new BorderLayout());
JPanel tmpPanel1 = new JPanel();
JPanel tmpPanel2 = new JPanel();
btCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread t = new Thread(JprogressBar.this);
t.start();
}
});
btSavePath.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
savePath = setSavePath();
}
});
btCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop = true;
btCopy.setEnabled(true);
}
});
tmpPanel1.add(btCopy);
tmpPanel1.add(btSavePath);
tmpPanel1.add(btCancel);
tmpPanel2.add(copyFileProgressBar);
this.add(tmpPanel1, BorderLayout.NORTH);
this.add(tmpPanel2, BorderLayout.SOUTH);
this.setTitle("read and copy file");
this.setPreferredSize(new Dimension(450, 95));
this.pack();
Dimension cd = centerIt(this);
this.setLocation(cd.width, cd.height);
final Dimension des = this.getPreferredSize();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
stop = true;
System.exit(0);
}
public void windowStateChanged(WindowEvent e) {
System.out.println("ss");
if (e.paramString().equals("WINDOW_STATE_CHANGED")) {
System.out.println("ss");
JprogressBar.this.setSize(des);
}
}
});
this.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
// System.out.println("ss");
// JprogressBar.this.setSize(des);
}
});
}
/*
* start read file..
*/
public void run() {
stop = false;
int c = openFileDialog.showOpenDialog(this);
if (c == JFileChooser.APPROVE_OPTION) {
try {
File selectFile = openFileDialog.getSelectedFile();
if (selectFile.equals(savePath)) {
JOptionPane.showMessageDialog(this, " \t target file and source file can't as the same !");
return;
}
if (savePath == null) {
JOptionPane.showMessageDialog(this, " \t please select a path to save file !");
return;
}
btCopy.setEnabled(false);
copyFileProgressBar.setVisible(true);
long size = selectFile.length();
copyFileProgressBar.setMaximum((int) size);
FileInputStream fin = new FileInputStream(selectFile);
FileOutputStream fout = new FileOutputStream(savePath);
byte[] buff = new byte[1024];
int s;
int count = 0;
long startTime = System.currentTimeMillis();
while ((s = fin.read(buff)) 0 !stop) {
count += s;
fout.write(buff, 0, s);
String str = "" + 100 * (count / (size + 0.01));
str = forMatString(str);
long endTime = System.currentTimeMillis();
String speedStr = getSpeed(count, startTime, endTime);
String remailTime = getRemailTime(count, size, startTime, endTime);
copyFileProgressBar.setString(" precent: " + str + " %" + " speed: " + speedStr + " " + " remail time : " + remailTime);
copyFileProgressBar.setValue(count);
}
fin.close();
fout.close();
if (!stop) {
JOptionPane.showMessageDialog(this, " \t copy file complete !");
}
stop = true;
savePath = null;
btCopy.setEnabled(true);
copyFileProgressBar.setValue(0);
copyFileProgressBar.setString("");
copyFileProgressBar.setVisible(false);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "err:\n" + ex.getMessage());
}
}
}
/*
*select save file path
*/
private File setSavePath() {
File path = null;
int c = openFileDialog.showSaveDialog(this);
if (c == JFileChooser.APPROVE_OPTION) {
path = openFileDialog.getSelectedFile();
}
return path;
}
/*
* make frame center
*/
private Dimension centerIt(Component c) {
Dimension size = c.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int sH = screenSize.height;
int sW = screenSize.width;
int cW = size.width;
int cH = size.height;
return new Dimension((sW - cW) / 2, (sH - cH) / 2);
}
/*
* show copy file speed
*/
private String getSpeed(long readByte, long startTime, long endTime) {
long speed;
if (endTime - startTime != 0) {
speed = (readByte / (endTime - startTime)) * 1000;
if (speed 1024 * 1024) {
return forMatString(speed / (1024 * 1024 + 0.1) + "") + " m/s";
} else if (speed 1024) {
return forMatString(speed / (1024 + 0.1) + "") + " k/s";
} else {
return speed + " b/s";
}
} else {
return "0 b/s";
}
}
/*
* format string
*/
private String forMatString(String str) {
String values;
int index = str.indexOf(".");
values = str.substring(0, index + 3);
return values;
}
/*
* get remail time
*/
private String getRemailTime(long readByte, long totalByte, long startTime, long endTime) {
long hour;
long minute;
long second;
String h;
String m;
String s;
try {
long speed = readByte / (endTime - startTime);
long time = ((totalByte - readByte) / speed) / 1000;
hour = time / 3600;
minute = time % 3600 / 60;
second = time % 3600 % 60;
h = hour + "";
m = minute + "";
s = second + "";
if (hour 10) {
m = "0" + minute;
}
if (minute 10) {
m = "0" + minute;
}
if (second 10) {
s = "0" + second;
}
return h + ":" + m + ":" + s;
} catch (Exception ex) {
return "00:00:00";
}
}
/*
* show frm
*/
public static void main(String[] args) {
JprogressBar frm = new JprogressBar();
frm.setVisible(true);
}
/*
*
*/
private JButton btCopy;
private JFileChooser openFileDialog;
private JProgressBar copyFileProgressBar;
private File savePath = null;
private boolean stop = false;
}
java编一个计算器的代码
界面漂亮堪比系统自带计算器,功能完美加减乘除开平方等等全部具备,还有清零按钮,小数点的使用,连加连乘功能完全参考系统官方计算器经过长期调试改进而成,马上拷贝代码拿去试试看吧,绝不后悔!
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Counter {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frame.show();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenu menuFile1 = new JMenu();
JMenu menuFile2 = new JMenu();
JMenu menuFile3 = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("文件");
menuFile1.setText("编辑");
menuFile2.setText("查看");
menuFile3.setText("帮助");
menuFileExit.setText("退出");
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CounterFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuBar.add(menuFile1);
menuBar.add(menuFile2);
menuBar.add(menuFile3);
setTitle("计算器");
setJMenuBar(menuBar);
setSize(new Dimension(400, 280));
this.getContentPane().add(new Allpanel());
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (this.field);
}
}
class Allpanel extends JPanel {
public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/**
* Method Centercenter
*
*
*/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Tool.getinstance().getfield();
this.setLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
for(int i=0;i20;i++)
{
final JButton b=new JButton(arg[i]);
//this.add(new JButton(arg[i]));
this.add(b);
if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();
if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}
else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();
}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}
}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}
begin="no";
to="yes";
}
});
}
if(i==17)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}
if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}
if(!Vec.contains(".")!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}
}
});
}
if(i==18)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
jq=Double.parseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}
if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="/";
}
});
}
if(i==4)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
text.setText(String.valueOf(Math.sqrt(cq)));
}
});
}
if(i==8)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}
if(i==19)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}
else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}
else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}
else
{
jg=String.valueOf(dy);
}
}
text.setText(jg);
Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Tool.getinstance().getfield();
JButton jb1=new JButton("Backspace");
JButton jb2=new JButton(" CE ");
JButton jb3=new JButton(" C ");
this.add(jb1);
this.add(jb2);
this.add(jb3);
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String back=Tool.getinstance().getfield().getText();
text.setText(backmethod(back));
Centercenter.Vec.remove(Centercenter.Vec.size()-1);
}
});
jb3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
text.setText("0.");
Centercenter.Vec.clear();
Centercenter.Vec.add(".");
Centercenter.vc.add("a");
Centercenter.begin="yes";
Centercenter.vc1.clear();
Centercenter.what=null;
Centercenter.tool=0;
}
});
}
public String backmethod(String str)
{
return str.substring(0,str.length()-1);
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
this.setLayout(new GridLayout(4,1,3,3));
this.add(new JButton("MC"));
this.add(new JButton("MR"));
this.add(new JButton("MS"));
this.add(new JButton("M+"));
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}
---------------------------------------------------------------------------
=============《按你要求特意后改过的最简单功能的代码如下》========================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Counter2 {
public static void main(String[] args) {
CounterFrame frame = new CounterFrame();
frame.show();
}
}
class CounterFrame extends JFrame {
public CounterFrame() {
setTitle("计算器");
setSize(new Dimension(400, 280));
this.getContentPane().add(new Allpanel());
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CounterFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
class Tool {
public static Tool instance;
private JTextField field;
private Tool() {
this.field=new JTextField(30);
this.field.setHorizontalAlignment(JTextField.RIGHT);
}
public static Tool getinstance()
{
if(instance==null)
{
instance=new Tool();
}
return instance;
}
public JTextField getfield()
{
return (this.field);
}
}
class Allpanel extends JPanel {
public Allpanel() {
this.setLayout(new BorderLayout(0,7));
Northpanel np=new Northpanel();
Centerpanel cp=new Centerpanel();
this.add(np,BorderLayout.NORTH);
this.add(cp,BorderLayout.CENTER);
}
}
class Centercenter extends JPanel {
static Vector Vec=new Vector();
static Vector vc=new Vector();
static Vector vc1=new Vector();
static Vector vc2=new Vector();
static Vector vc3=new Vector();
static String begin="yes";
static double add;
static double jq;
static double cs;
static double cq;
static double dy;
static String jg;
static String what;
static double tool=0;
static String to="yes";
/**
* Method Centercenter
*
*
*/
public Centercenter() {
// TODO: Add your code here
final JTextField text=Tool.getinstance().getfield();
this.setLayout(new GridLayout(4,5,3,3));
String arg[] ={"7","8","9","/","4","5","6","*","1","2","3","-","0","=",".","+"};
for(int i=0;i16;i++)
{
final JButton b=new JButton(arg[i]);
//this.add(new JButton(arg[i]));
this.add(b);
if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mark=b.getText();
String ma=text.getText();
if(vc3.contains("v3"))
{
text.setText("0."+mark);
vc3.clear();
}
else if(vc.contains("a"))
{
if(vc2.contains("v2"))
{
text.setText("0."+mark);
vc.clear();
vc2.clear();
}
else
{
text.setText(mark);
vc.clear();
Vec.clear();
Vec.add(mark);
}
}
else
{
text.setText(ma.trim()+mark);
Vec.add(mark);
}
begin="no";
to="yes";
}
});
}
if(i==14)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String mar=b.getText();
String m=text.getText();
if("yes".equals(begin))
{
vc3.add("v3");
}
if(vc1.contains("v1"))
{
vc2.add("v2");
vc1.clear();
}
if(!Vec.contains(".")!vc.contains("a"))
{
text.setText(m.trim()+mar);
Vec.add(".");
}
}
});
}
if(i==15)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
add=Double.parseDouble(ma);
if(what==null)
{
tool=add;
what="add";
}
else
{
tool=tool+add;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="+";
}
});
}
if(i==11)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
jq=Double.parseDouble(ma);
if(what==null)
{
tool=jq;
what="jq";
}
else
{
tool=tool-jq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="-";
}
});
}
if(i==3)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cq=Double.parseDouble(ma);
if(what==null)
{
tool=cq;
what="cq";
}
else
{
tool=tool/cq;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="/";
}
});
}
if(i==7)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
cs=Double.parseDouble(ma);
if(what==null)
{
tool=cs;
what="cs";
}
else
{
tool=tool*cs;
text.setText(String.valueOf((tool)));
}
vc.add("a");
vc1.add("v1");
to="*";
}
});
}
if(i==13)
{
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ma=text.getText();
dy=Double.parseDouble(ma);
if(what=="add")
{
jg=String.valueOf((tool+dy));
}
if(what=="jq")
{
jg=String.valueOf((tool-dy));
}
if(what=="cs")
{
jg=String.valueOf((tool*dy));
}
if(what=="cq")
{
jg=String.valueOf((tool/dy));
}
if(what==null)
{
if(to=="+")
{
tool=add;
jg=String.valueOf(tool+dy);
}
else if(to=="-")
{
tool=jq;
jg=String.valueOf(dy-tool);
}
else if(to=="*")
{
tool=cs;
jg=String.valueOf(dy*tool);
}
else if(to=="/")
{
tool=cq;
jg=String.valueOf(dy/tool);
}
else
{
jg=String.valueOf(dy);
}
}
text.setText(jg);
Vec.clear();
Vec.add(".");
vc.add("a");
vc1.add("v1");
what=null;
tool=0;
}
});
}
}
}
}
class Centernorth extends JPanel {
public Centernorth() {
final JTextField text=Tool.getinstance().getfield();
}
}
class Centerpanel extends JPanel {
public Centerpanel() {
this.setLayout(new BorderLayout(8,7));
Centernorth cn=new Centernorth();
Centercenter cc=new Centercenter();
Centerwest cw=new Centerwest();
this.add(cn,BorderLayout.NORTH);
this.add(cc,BorderLayout.CENTER);
this.add(cw,BorderLayout.WEST);
}
}
class Centerwest extends JPanel {
public Centerwest() {
}
}
class Northpanel extends JPanel {
private JTextField tf;
public Northpanel() {
tf=Tool.getinstance().getfield();
this.add(tf);
}
}
------------------------------------------------------------
才子_辉祝您愉快!
通过jbutton改变jpanel内容
好久没玩swing了,我记得应该可以直接添加JButton,至于说添加JPanel是否会覆盖整个面板那就得看你的布局啦。你要是想控件放在你自己想的特定位置,就用空布局
Jframe.setLayout(null)
然后控件Jbutton.setBounds(x1,y1,x2,y2);指定到你想要的位置上去
java中JButton有没有哪个动作是用于禁用这个按钮?
需要在监听事件里面设置
if(jbutton.isEnabled()){
//响应点击事件
jubtton.setEnabled(false);
}
关于javajubtton和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-28,除非注明,否则均为
原创文章,转载请注明出处。