java恶作剧小程序
① 谁能帮我做一个java的小程序 不是很复杂的那种 类似调色板那种
我这里有一个程序,是读文件和用调色板设置背景色的程序,你看看如何。
import java.awt.*;
import javax.swing.*;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.*;
import java.awt.event.*;
public class Test11 extends JFrame{
//添加一个颜色对话框窗口
JFrame color=new JFrame();
JDialog color_diglog=new JDialog(color,"颜色",true);
Container contentpane=this.getContentPane();
JTextArea text=new JTextArea();//文本域
JFileChooser filechooser=new JFileChooser();//文件选择器
JColorChooser colorchooser=new JColorChooser();//颜色选择器
ColorSelectionModel model=colorchooser.getSelectionModel();//用以获取颜色模型
//创建菜单栏
JMenuBar menubar=new JMenuBar();
JMenu F_menu=new JMenu("文件(F)"),
C_menu=new JMenu("颜色(C)");
JMenuItem FC=new JMenuItem("打开(文件选择器)"),
CC=new JMenuItem("颜色(颜色选择器)");
public Test11(){
super("简单文本编辑器");//调用父类(JFrame)的构造方法
contentpane.setLayout(new BorderLayout());
text.setLineWrap(true);
F_menu.add(FC);
C_menu.add(CC);
menubar.add(F_menu);
menubar.add(C_menu);
contentpane.add(menubar,"North");
contentpane.add(text);
color_diglog.add(colorchooser);
color_diglog.setSize(300, 400);
fileshow();//事件监听器类
}
public void fileshow(){
//文件查看器
FC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int result=filechooser.showOpenDialog(null);
File file=filechooser.getSelectedFile();
if(file!=null&&result==JFileChooser.APPROVE_OPTION){
try {//将读出的文件赋给text,text用read方法读出
FileReader fr=new FileReader(file.getPath());
text.read(fr,null);
fr.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
});
//颜色查看器
CC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color_diglog.setLocationRelativeTo(Test11.this);//在color_dialog中显示颜色选择器
model.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
text.setBackground(colorchooser.getColor());//将文本域的背景色改变为获取的颜色
}
});
color_diglog.setVisible(true);
}
});
}
public static void main(String[] args) {
JFrame f=new Test11();
f.setBounds(300,300,300,300);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
System.exit(0);
}
});
}
}
② java:求一个用swing来做小程序,我是用来修改配置文件用的,求代码谢谢
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.JPanel;
@SuppressWarnings("serial")
public class Test02 extends JFrame {
private JPanel jp = new JPanel();
private JButton jb01 = new JButton("按钮一");
private JButton jb02 = new JButton("按钮二");
private JButton jb03 = new JButton("按钮三");
private JButton[] jb = new JButton[] { jb01, jb02, jb03 };
private JLabel jl = new JLabel("请单击按钮!");
private int count = 0;
public Test02() {
for (int i = 0; i < jb.length; i++) {
jp.add(jb[i]);
}
jp.add(jl);
this.add(jp);
this.setTitle("点按钮,记录单击按钮的次数和名字!");
jb01.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Test02.this.jl.setText(jl.getText());
}
});
for (int i = 0; i < jb.length; i++) {
jb[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jb01) {
Test02.this.jl.setText("您单击的是按钮一,您总共单机了" + (++count)
+ "次按钮");
} else if (e.getSource() == jb02) {
Test02.this.jl.setText("您单击的是按钮二,您总共单机了" + (++count)
+ "次按钮");
} else if (e.getSource() == jb03) {
Test02.this.jl.setText("您单击的是按钮三,您总共单机了" + (++count)
+ "次按钮");
}
}
});
this.setBounds(100, 100, 480, 130);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
new Test02();
}
}
③ 求java经典小程序代码
代码如下:
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
System.out.println("Hello World!");
}
public static int f(int a, int b){
return a*a + a*b + b*b;
}
}
结果如下: