用java写应用小程序,在网页上输出一条直线

用java写应用小程序,在网页上输出一条直线,第1张

package comjack;

import javaawt;

import javaxswing;

import javaawtevent;

public class MoveLine extends JFrame{

public MoveLine(){

MyLinePane p = new MyLinePane(10,10,100,100);

psetSize(400,400);

setContentPane(p);

setSize(500,500);

}

public static void main(String[] args) {

MoveLine ml = new MoveLine();

mlsetVisible(true);

mlsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

}

}

class MyLinePane extends JPanel implements MouseListener,MouseMotionListener{

int x1,x2,y1,y2;//当前值

int oldx1,oldy1,oldx2,oldy2;//原始值

boolean selected=false;

boolean move=false;

public MyLinePane(int x1,int y1,int x2,int y2){

thisx1=x1;

thisx2=x2;

thisy1=y1;

thisy2=y2;

thisoldx1=x1;

thisoldx2=x2;

thisoldy1=y1;

thisoldy2=y2;

thisaddMouseListener(this);

thisaddMouseMotionListener(this);

}

public void paintComponent(Graphics g){

superpaintComponent(g);

gdrawLine(x1,y1,x2,y2);

if(selected){

gfillRect(x1,y1,x1+1,y1+1);

gfillRect(x2,y2,x2+1,y2+1);

}

//此处根据状态,selected,move 编写方法

//每次擦掉原来的线和点,绘制新的线和点

}

//以下几个方法中判断当前的 *** 作,并纪录坐标

public void mouseDragged(MouseEvent e){

}

public void mouseMoved(MouseEvent e){

}

public void mouseClicked(MouseEvent e){

if(egetButton()==MouseEventBUTTON1){

selected=true;

}

thisupdateUI();

}

public void mousePressed(MouseEvent e){

}

public void mouseReleased(MouseEvent e){

}

public void mouseEntered(MouseEvent e){

}

public void mouseExited(MouseEvent e){

}

}

我这里有一个程序,是读文件和用调色板设置背景色的程序,你看看如何。

import javaawt;

import javaxswing;

import javaxswingcolorchooserColorSelectionModel;

import javaxswingeventChangeEvent;

import javaxswingeventChangeListener;

import javaio;

import javaawtevent;

public class Test11 extends JFrame{

//添加一个颜色对话框窗口

JFrame color=new JFrame();

JDialog color_diglog=new JDialog(color,"颜色",true);

Container contentpane=thisgetContentPane();

JTextArea text=new JTextArea();//文本域

JFileChooser filechooser=new JFileChooser();//文件选择器

JColorChooser colorchooser=new JColorChooser();//颜色选择器

ColorSelectionModel model=colorchoosergetSelectionModel();//用以获取颜色模型

//创建菜单栏

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)的构造方法

contentpanesetLayout(new BorderLayout());

textsetLineWrap(true);

F_menuadd(FC);

C_menuadd(CC);

menubaradd(F_menu);

menubaradd(C_menu);

contentpaneadd(menubar,"North");

contentpaneadd(text);

color_diglogadd(colorchooser);

color_diglogsetSize(300, 400);

fileshow();//事件监听器类

}

public void fileshow(){

//文件查看器

FCaddActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

int result=filechoosershowOpenDialog(null);

File file=filechoosergetSelectedFile();

if(file!=null&&result==JFileChooserAPPROVE_OPTION){

try {//将读出的文件赋给text,text用read方法读出

FileReader fr=new FileReader(filegetPath());

textread(fr,null);

frclose();

} catch (FileNotFoundException e1) {

e1printStackTrace();

} catch (IOException e2) {

e2printStackTrace();

}

}

}

});

//颜色查看器

CCaddActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

color_diglogsetLocationRelativeTo(Test11this);//在color_dialog中显示颜色选择器

modeladdChangeListener(new ChangeListener(){

public void stateChanged(ChangeEvent e){

textsetBackground(colorchoosergetColor());//将文本域的背景色改变为获取的颜色

}

});

color_diglogsetVisible(true);

}

});

}

public static void main(String[] args) {

JFrame f=new Test11();

fsetBounds(300,300,300,300);

fsetVisible(true);

fsetDefaultCloseOperation(WindowConstantsDISPOSE_ON_CLOSE);

fsetDefaultLookAndFeelDecorated(true);

faddWindowListener(new WindowAdapter(){

public void windowClosed(WindowEvent e){

Systemexit(0);

}

});

}

}

int top = 0, sa[];

这句话是有问题的 int类型定义了2个量 一个是top 一个是sa[]

然而sa[] 却应该是一个int[]类型的 可以这个改下

int top = 0;

int[] sa = new int[10];

下边程序的qe也一样改过来

此外 你的程序中还要数组越界的问题 好好看看把

a  = scnextInt();

b  = scnextInt();

a < 0||b < 0

a

完整代码:

public class ComDiv {

public static void main(String[] args) {

int a;

int b;

int r;

Scanner sc = new Scanner(Systemin);

do {

Systemoutprintln("请输入两个整数:");

a  = scnextInt();

b  = scnextInt();

} while (a < 0||b < 0);

scclose();

while(b != 0){

r = a%b;

a = b;

b = r;

}

Systemoutprintln("两个数的最大公约数是:" +a);

}

}

演示:

import javaxswing;

import javaawt;

import javaawtevent;

public class Calculator extends JFrame implements ActionListener

{

private boolean dotExist, operated, equaled; // 帮助运算的布尔变量

private double storedNumber; // 目前的结果

private char lastOperator; // 表示上一运算符

private JTextField operation; // 结果栏

private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符

private JButton[] numbers; // 数字

// 构造者

public Calculator()

{

setTitle("Calculator");

// 初始化变量

dotExist = false; // 表示当前的数是否有小数点

operated = false; // 表示任意运算符是否被按下

equaled = false; // 表示等号是否被按下

storedNumber = 0;

lastOperator = '';

// 初始化窗口变量

operation = new JTextField("0");

operationsetEditable(false);

numbers = new JButton[10];

for (int i = 0; i < 10; i++)

numbers[i] = new JButton("" + i);

dot = new JButton("");

plus = new JButton("+");

minus = new JButton("-");

multi = new JButton("");

div = new JButton("/");

sqrt = new JButton("√");

equal = new JButton("=");

changePN = new JButton("±");

clear = new JButton("AC");

// 将窗口物体放入窗口

GridBagLayout layout = new GridBagLayout();

getContentPane()setLayout(layout);

addComponent(layout, operation, 0, 0, 4, 1);

addComponent(layout, numbers[1], 1, 0, 1, 1);

addComponent(layout, numbers[2], 1, 1, 1, 1);

addComponent(layout, numbers[3], 1, 2, 1, 1);

addComponent(layout, numbers[4], 2, 0, 1, 1);

addComponent(layout, numbers[5], 2, 1, 1, 1);

addComponent(layout, numbers[6], 2, 2, 1, 1);

addComponent(layout, numbers[7], 3, 0, 1, 1);

addComponent(layout, numbers[8], 3, 1, 1, 1);

addComponent(layout, numbers[9], 3, 2, 1, 1);

addComponent(layout, dot, 4, 0, 1, 1);

addComponent(layout, numbers[0], 4, 1, 1, 1);

addComponent(layout, sqrt, 4, 2, 1, 1);

addComponent(layout, plus, 1, 3, 1, 1);

addComponent(layout, minus, 2, 3, 1, 1);

addComponent(layout, multi, 3, 3, 1, 1);

addComponent(layout, div, 4, 3, 1, 1);

addComponent(layout, equal, 5, 0, 2, 1);

addComponent(layout, changePN, 5, 2, 1, 1);

addComponent(layout, clear, 5, 3, 1, 1);

}

// 对按钮进行反应的方法

public void actionPerformed(ActionEvent e)

{

JButton btn = (JButton)egetSource();

if (btn == clear)

{

operationsetText("0");

dotExist = false;

storedNumber = 0;

lastOperator = '';

}

else if (btn == equal)

{

operate('=');

equaled = true;

}

else if (btn == plus)

{

operate('+');

equaled = false;

}

else if (btn == minus)

{

operate('-');

equaled = false;

}

else if (btn == multi)

{

operate('');

equaled = false;

}

else if (btn == div)

{

operate('/');

equaled = false;

}

else if (btn == changePN)

{

operate('p');

operate('=');

equaled = true;

}

else if (btn == sqrt)

{

operate('s');

operate('=');

equaled = true;

}

else

{

if (equaled)

storedNumber = 0;

for (int i = 0; i < 10; i++)

if (btn == numbers[i])

{

if (operationgetText()equals("0"))

operationsetText("" + i);

else if(! operated)

operationsetText(operationgetText() + i);

else

{

operationsetText("" + i);

operated = false;

}

}

if (btn == dot && ! dotExist)

{

operationsetText(operationgetText() + "");

dotExist = true;

}

}

}

// 进行运算的方法

private void operate(char operator)

{

double currentNumber = DoublevalueOf(operationgetText())doubleValue();

if (lastOperator == '')

storedNumber = currentNumber;

else if (lastOperator == '+')

storedNumber += currentNumber;

else if (lastOperator == '-')

storedNumber -= currentNumber;

else if (lastOperator == '')

storedNumber = currentNumber;

else if (lastOperator == '/')

storedNumber /= currentNumber;

else if (lastOperator == 'p')

storedNumber = -1;

else if (lastOperator == 's')

storedNumber = Mathsqrt(currentNumber);

else if (lastOperator == '=' && equaled)

storedNumber = currentNumber;

operationsetText("" + storedNumber);

operated = true;

lastOperator = operator;

}

// 快捷使用GridBagLayout的方法

private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)

{

GridBagConstraints constraints = new GridBagConstraints();

constraintsfill = GridBagConstraintsBOTH;

constraintsinsets = new Insets(10, 2, 10, 2);

constraintsweightx = 100;

constraintsweighty = 100;

constraintsgridx = col;

constraintsgridy = row;

constraintsgridwidth = width;

constraintsgridheight = height;

layoutsetConstraints(component, constraints);

if (component instanceof JButton)

((JButton)component)addActionListener(this);

getContentPane()add(component);

}

// 主方法初始化并显示窗口

public static void main(String[] args)

{

Calculator calc = new Calculator();

calcsetSize(290, 400);

calcsetVisible(true);

}

}

如果你想加sin cos tan的话就建它们的按钮 在actionPerformed方法中的if-else语句中加else if (btn == sin){operate('S');operate('=');equaled = true;}然后在operate方法加对应的语句就行了记忆M+, M-没时间搞 这个应该比较简单容易明白吧

用eclipse编写java小程序——网站数据提交工具:

1、创建Java Project项目,“File——》New——》Java Project”

2、小程序主要代码结构

3、主函数中代码

以上就是关于用java写应用小程序,在网页上输出一条直线全部的内容,包括:用java写应用小程序,在网页上输出一条直线、谁能帮我做一个java的小程序 不是很复杂的那种 类似调色板那种、java 小程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10634355.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-10
下一篇 2023-05-10

发表评论

登录后才能评论

评论列表(0条)

保存