java设计按钮源代码 java中的按钮怎么设计

怎么用java编写一个按钮界面新建一个窗口java设计按钮源代码,然后实现一个关闭按钮”窗口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计算器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);
【java设计按钮源代码 java中的按钮怎么设计】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 voidinit(){
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 visitfor 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设计按钮源代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java中的按钮怎么设计、java设计按钮源代码的信息别忘了在本站进行查找喔 。

    推荐阅读