當前位置:首頁 » 編程語言 » java惡作劇小程序

java惡作劇小程序

發布時間: 2025-02-07 23:53:48

① 誰能幫我做一個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;

}

}

  • 結果如下:

熱點內容
il腳本 發布:2025-02-08 03:08:49 瀏覽:315
我的世界介紹神奇寶貝伺服器 發布:2025-02-08 03:02:52 瀏覽:747
咪咕音樂linux 發布:2025-02-08 02:53:04 瀏覽:410
我的世界手機版大陸練習伺服器 發布:2025-02-08 02:50:43 瀏覽:213
php的特點與優勢 發布:2025-02-08 02:43:16 瀏覽:718
微信公眾號怎麼上傳pdf 發布:2025-02-08 02:42:41 瀏覽:348
安卓如何查看通話總時長 發布:2025-02-08 02:27:49 瀏覽:579
快速dct演算法 發布:2025-02-08 02:19:04 瀏覽:623
淘寶交易密碼如何改 發布:2025-02-08 02:17:32 瀏覽:775
php的進階 發布:2025-02-08 02:17:28 瀏覽:677