资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java初级30行代码的简单介绍

java gridlayout 如何实现30行2列如图示的排列?

GridLayout是这样的,都是平均分配的。根据楼主的需求,我提供两种思路:

公司主营业务:成都做网站、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出新干免费做网站回馈大家。

其一:使用JSplitPane分割,以下代码:

private void init{} {

//左侧布局

JPanel left = new JPanel() ;

left.setLayout(new GridLayout(30,1,1,1));

for(int i=0;i30;i++) {

//添加JLabel

}

//右侧布局

JPanel right = new JPanel() ;

right.setLayout(new GridLayout(30,1,1,1));

for(int i=0;i30;i++) {

//添加JTextField

}

//整体布局

JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,right);//使用JSplitPane

jsp.setDividerLocation(100);

this.add(jsp) ;

this.setLocation(100, 100);

this.setSize(400, 800);

this.setVisible(true);

}

注意:jsp.setDividerLocation(100);方法是设置分隔栏的位置;其中数字要配合this.setSize(400, 800);使用,长400,分隔栏100,也就是大约左侧占四分之一,右侧占四分之三。这种方法比较灵活。

其二:使用BorderLayout布局器,代码就不写了,太麻烦

就是把左边的JLabel布局好以后放在WEST中,右侧的JTextField布局好后放在CENTER中。

求基础级java代码,150-200行,自己写的

我有计算器程序

import javax.swing.*;

import javax.swing.border.Border;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.HashMap;

/**

* 我的计算器。MyCalculator 继承于 JFrame,是计算器的界面

*/

public class Calculator extends JFrame {

/**

*

*/

private static final long serialVersionUID = 1L;

private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);

private JTextField textbox = new JTextField("0");

private CalculatorCore core = new CalculatorCore();

private ActionListener listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton) e.getSource();

String label = b.getText();

String result = core.process(label);

textbox.setText(result);

}

};

public Calculator(String title) throws HeadlessException {

super(title); // 调用父类构造方法

setupFrame(); // 调整窗体属性

setupControls(); // 创建控件

}

private void setupControls() {

setupDisplayPanel(); // 创建文本面板

setupButtonsPanel(); // 创建按钮面板

}

// 创建按钮面板并添加按钮

private void setupButtonsPanel() {

JPanel panel = new JPanel();

panel.setBorder(border);

panel.setLayout(new GridLayout(4, 5, 3, 3));

createButtons(panel, new String[]{

"7", "8", "9", "+", "C",

"4", "5", "6", "-", "CE",

"1", "2", "3", "*", "", // 空字符串表示这个位置没有按钮

"0", ".", "=", "/", ""

});

this.add(panel, BorderLayout.CENTER);

}

/**

* 在指定的面板上创建按钮

*

* @param panel 要创建按钮的面板

* @param labels 按钮文字

*/

private void createButtons(JPanel panel, String[] labels) {

for (String label : labels) {

// 如果 label 为空,则表示创建一个空面板。否则创建一个按钮。

if (label.equals("")) {

panel.add(new JPanel());

} else {

JButton b = new JButton(label);

b.addActionListener(listener); // 为按钮添加侦听器

panel.add(b);

}

}

}

// 设置显示面板,用一个文本框来作为计算器的显示部分。

private void setupDisplayPanel() {

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBorder(border);

setupTextbox();

panel.add(textbox, BorderLayout.CENTER);

this.add(panel, BorderLayout.NORTH);

}

// 调整文本框

private void setupTextbox() {

textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右对齐

textbox.setEditable(false); // 文本框只读

textbox.setBackground(Color.white); // 文本框背景色为白色

}

// 调整窗体

private void setupFrame() {

this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 当窗体关闭时程序结束

this.setLocation(100, 50); // 设置窗体显示在桌面上的位置

this.setSize(300, 200); // 设置窗体大小

this.setResizable(false); // 窗体大小固定

}

// 程序入口

public static void main(String[] args) throws Exception {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Calculator frame = new Calculator("我的计算器");

frame.setVisible(true); // 在桌面上显示窗体

}

}

/**

* 计算器核心逻辑。这个逻辑只能处理 1~2 个数的运算。

*/

class CalculatorCore {

private String displayText = "0"; // 要显示的文本

boolean reset = true;

int MaxLen = 30;

private BigDecimal number1, number2;

private String operator;

private HashMapString, Operator operators = new HashMapString, Operator();

private HashMapString, Processor processors = new HashMapString, Processor();

CalculatorCore() {

setupOperators();

setupProcessors();

}

// 为每种命令添加处理方式

private void setupProcessors() {

processors.put("[0-9]", new Processor() {

public void calculate(String command) {

numberClicked(command);

}

});

processors.put("\\.", new Processor() {

public void calculate(String command) {

dotClicked();

}

});

processors.put("=", new Processor() {

public void calculate(String command) {

equalsClicked();

}

});

processors.put("[+\\-*/]", new Processor() {

public void calculate(String command) {

operatorClicked(command);

}

});

processors.put("C", new Processor() {

public void calculate(String command) {

clearClicked();

}

});

processors.put("CE", new Processor() {

public void calculate(String command) {

clearErrorClicked();

}

});

}

// 为每种 operator 添加处理方式

private void setupOperators() {

operators.put("+", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.add(number2);

}

});

operators.put("-", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.subtract(number2);

}

});

operators.put("*", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.multiply(number2);

}

});

operators.put("/", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.divide(number2, 30, RoundingMode.HALF_UP);

}

});

}

// 根据命令处理。这里的命令实际上就是按钮文本。

public String process(String command) {

for (String pattern : processors.keySet()) {

if (command.matches(pattern)) {

processors.get(pattern).calculate(command);

break;

}

}

return displayText;

}

// 当按下 CE 时

private void clearErrorClicked() {

if (operator == null) {

number1 = null;

} else {

number2 = null;

}

displayText = "0";

reset = true;

}

// 当按下 C 时,将计算器置为初始状态。

private void clearClicked() {

number1 = null;

number2 = null;

operator = null;

displayText = "0";

reset = true;

}

// 当按下 = 时

private void equalsClicked() {

calculateResult();

number1 = null;

number2 = null;

operator = null;

reset = true;

}

// 计算结果

/**

*

*/

private void calculateResult() {

number2 = new BigDecimal(displayText);

Operator oper = operators.get(operator);

if (oper != null) {

try {

BigDecimal result = oper.process(number1, number2);

displayText = result.toString();

} catch (RuntimeException e) {

clearClicked();//将计算器置为初始状态

JOptionPane.showMessageDialog(null,"不能用零作除数","出错了",JOptionPane.OK_OPTION);

//e.printStackTrace();

}

}

}

// 当按下 +-*/ 时(这里也可以扩展成其他中间操作符)

private void operatorClicked(String command) {

if (operator != null) {

calculateResult();

}

number1 = new BigDecimal(displayText);

operator = command;

reset = true;

}

// 当按下 . 时

private void dotClicked() {

if (displayText.indexOf(".") == -1) {

displayText += ".";

} else if (reset) {

displayText = "0.";

}

reset = false;

}

// 当按下 0-9 时

private void numberClicked(String command) {

if (reset) {

displayText = command;

} else {

if(displayText.length() MaxLen)

displayText += command;

else

JOptionPane.showMessageDialog(null,"输入的数字太长了","出错了",JOptionPane.OK_OPTION);

}

reset = false;

}

// 运算符处理接口

interface Operator {

BigDecimal process(BigDecimal number1, BigDecimal number2);

}

// 按钮处理接口

interface Processor {

void calculate(String command);

}

}

求一Java代码,要简单的。大概一百多行的的。

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?

这个题目,下面我会贴出来两种代码。其实这个题目,我烦了简单计算,想搞得有趣味性一点,结果耽误了好几天时间,最后发现搞的也不好。

先第一种,为了解题而解题。

==== Main.java ====

public class Main {

private double TotalHeight = 100;

private double CurHeight = 50;

public void drop(int times) {

if ((times - 1) == 0) {

return;

}

setTotalHeight(getTotalHeight() + 2 * getCurHeight());

setCurHeight(getCurHeight() / 2);

drop(times - 1);

}

public double getTotalHeight() {

return TotalHeight;

}

public void setTotalHeight(double totalHeight) {

TotalHeight = totalHeight;

}

public double getCurHeight() {

return CurHeight;

}

public void setCurHeight(double curHeight) {

CurHeight = curHeight;

}

public static void main(String[] args) {

Main main = new Main();

main.drop(2);

System.out.println("Total height is " +

main.getTotalHeight());

System.out.println("Current height is " +

main.getCurHeight());

}

}

==== 然后是第二种 =====

==== Main.java ====

package main;

import javax.swing.JFrame;

import panel.BallPanel;

import time.Delay;

public class MainFrame extends JFrame {

public MainFrame(String title) {

super(title);

}

public static void main(String[] args) {

Delay delay = new Delay();

MainFrame frame = new MainFrame("Hello JFrame");

BallPanel ballPanel = new BallPanel();

frame.add(ballPanel);

frame.setSize(500, 500);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

delay.initDelay();

ballPanel.setDelay(delay.getDelay());

ballPanel.loopDrop(0);

}

}

==== Delay.java ====

package time;

public class Delay {

public void initDelay() {

int g = 10;

int i = 0;

double s, t, t0 = 0.0;

delay = new int[100];

for (s = 100; s 10100; s += 100) {

t = Math.sqrt(2 * s / g);

delay[i++] = (int) ((t - t0) * 100);

t0 = t;

}

}

public int[] getDelay() {

return delay;

}

private int delay[];

}

==== BallPanel.java ====

package panel;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JPanel;

public class BallPanel extends JPanel {

public BallPanel() {

super();

}

public void paint(Graphics g) {

g.clearRect(0, 0, this.getWidth(), this.getHeight());

g.setColor(Color.BLUE);

g.fillOval(250, ballCenter, 30, 30);

}

public void loopDrop(int height) {

int i;

if (this.height == height) { // At bottom

for (i = 0; i targetHeight; i += MUL) {

ballCenter = this.height - i;

this.repaint();

try {

Thread.sleep(delay[(targetHeight - i - 1) / MUL]);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

loopDrop(this.height - i);

} else { // At top

for (i = height; i this.height; i += MUL) {

ballCenter = i;

this.repaint();

try {

Thread.sleep(delay[(i - height) / MUL]);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

targetHeightV = targetHeightV / 2;

targetHeight = targetHeightV;

if (targetHeight != 0) {

loopDrop(i);

}

}

}

public void setDelay(int delay[]) {

this.delay = delay;

}

private int extracted() {

return 100 * MUL;

}

private int targetHeight = extracted();

private int targetHeightV = extracted();

private int ballCenter = 0;

private int height = extracted();

private int delay[];

private final int MUL = 4;

}

其实所谓的第二种,是用的JFrame在JPanel上画图,直观的展示出来每次弹起来的效果。因为100像素实在太小了,所以我做了一个变量MUL,相

当于是等比例扩大的效果。问题就是弹不到10次距离就用光了,所以。。。。。权当娱乐了。。当然可以等比例在放大,例如100M看成是10000像素,这

样能多弹几次。这个程序,最后球就在那里不动了,程序不会自己退出。

java 源代码 基础点的 谢谢

package com.regex;

import java.io.*;

import java.net.URLDecoder;

import java.util.regex.*;

public class Regex {

private int REMARK=0;

private int LOGIC=0;

private int PHYSIC=0;

boolean start=false;

/**

* @param args

*/

public static void main(String[] args) { //测试方法

// TODO Auto-generated method stub

Regex re=new Regex();

re.regCount("Regex.java");

System.out.println("remark Line: "+re.REMARK);

System.out.println("logic Line: "+re.LOGIC);

System.out.println("physic Line: "+re.PHYSIC);

}/**

* @author BlueDance

* @param s

* @deprecated count

*/

public void regCount(String s){

String url=null;

try {

url=URLDecoder.decode(this.getClass().getResource(s).getPath(),"UTF-8");

} catch (Exception e) {

e.printStackTrace();

// TODO: handle exception

}

try {

BufferedReader br=new BufferedReader(new FileReader(new File(url)));

String s1=null;

while((s1=br.readLine())!=null){

PHYSIC++;

if(CheckChar(s1)==1){

REMARK++;

System.out.println("纯注释行:"+s1);

}

if(CheckChar(s1)==2){

LOGIC++;

REMARK++;

System.out.println("非纯注释行:"+s1);

}

if(CheckChar(s1)==3)

LOGIC++;

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}

}

/**

*

* @param s

* @return int

* @version check s

*/

public int CheckChar(String s){

String s1=null;

if(s!=null)

s1=s.trim();

//System.out.println(regCheck(s1,re));

if(regCheck(s1,"(//.*)")) //判断//开头的为纯注释行

return 1;

if(regCheck(s1,"(.*[;{})] *//.*)")) //判断不是//开头的非纯注释行

return 2;

if(regCheck(s1,"(//*.*)")){ //判断/*开头的纯注释行

start=true;

return 1;

}

if(regCheck(s1,"(.*[;{})]//*.*)")){ //判断不是/*开头的非纯注释行

start=true;

return 2;

}

if(regCheck(s1,"(.* */*/)")){ //判断*/结尾的纯注释行

start=false;

return 1;

}

if(regCheck(s1,"(.* */*/.*)")!strCheck(s1)){ //判断不是*/结尾的非纯注释行

if(strCheck(s1)){

start=false;

return 2;

}

}

if(start==true) //状态代码,start即/*开始时start=true*/结束时为false

return 1;

return 3;//ssssllll

}//aeee

/**

*

* @param s

* @param re

* @return boolean

*/

public boolean regCheck(String s,String re){ //正则表达试判断方法

return Pattern.matches(re,s);

}

public boolean strCheck(String s){ //中间有*/的字符判断 此方法最关键

if(s.indexOf("*/")0){

int count=0;

String y[]=s.split("/*/");

boolean boo[]=new boolean[y.length];

for (int i = 0; i y.length-1; i++) {

char c[]=y[i].toCharArray();

for (int j = 0; j c.length; j++) {

if(c[j]=='\\'c[j+1]=='"'){

count++;

}

}

if(count%2==0){

if(countNumber("\"",y[i])%2!=0){

boo[i]=true;

}else{

boo[i]=false;

}

}else{

if(countNumber("\"",y[i])%2==0){

boo[i]=true;

}else{

boo[i]=false;

}

}

}

for(int i=0;iboo.length;i++){

if(!boo[i])

return false;

}

return true;

}

return false;

}

public int countNumber(String s,String y){ //此方法为我前面写的字符串出现次数统计方法,不懂的可以看我前面的文章

int count=0;

String [] k=y.split(s);

if(y.lastIndexOf(s)==(y.length()-s.length()))

count=k.length;

else

count=k.length-1;

if(count==0)

System.out.println ("字符串\""+s+"\"在字符串\""+y+"\"没有出现过");

else

return count;

return -1;

}

}

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class GoodLucky extends JFrame implements ActionListener{

JTextField tf = new JTextField(); //实例化一个文本域

//设置两个按钮

JButton b1 = new JButton("开始");

JButton b2 = new JButton("停止");

boolean isGo = false;

//构造函数

public GoodLucky(){

b1.setActionCommand("start");//在开始按钮上设置一个动作监听 start

JPanel p = new JPanel(); //实例化一个可视化容器

//将两个按钮添加到可视化容器上面,用add方法

p.add(b1);

p.add(b2);

//在两个按钮上增加监听的属性,自动调用下面的监听处理方法actionPerformed(ActionEvent e),如果要代码有更好的可读性,可用内部类实现动作

//监听处理。

b1.addActionListener(this);

b2.addActionListener(this);

//将停止按钮设置为不可编辑(即不可按的状态)

b2.setEnabled(false);

this.getContentPane().add(tf,"North"); //将上面的文本域放在面板的北方,也就是上面(上北下南左西右东)

this.getContentPane().add(p,"South"); //将可视化容器pannel放在南边,也就是下面

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的操作,参数EXIT_ON_CLOSE是使用 System exit 方法退出应用程序。仅在应用程序中使用

this.setSize(300,200); //设置面板大小,宽和高

this.setLocation(300,300); //设置面板刚开始的出现的位置

Cursor cu = new Cursor(Cursor.HAND_CURSOR); //用指定名称创建一个新的定制光标对象,参数表示手状光标类型

this.setCursor(cu); //为指定的光标设置光标图像,即设置光标图像为上面所创建的手状光标类型

this.setVisible(true); //将面板可视化设置为true,即可视,如果为false,即程序运行时面板会隐藏

tf.setText("welcome you! "); //设置面板的标题为欢迎

this.go(); //调用go方法

}

public void go(){

while(true){ //这里是死循环,也就是说用户不点击停止按钮的话他一直循环出现随机数,直到用户点击停止按钮循环才能推出,具体流程在actionPerformed方法中控制。

if(isGo == true){ //上面所定义的isGo的初始值为false,所以程序第一次到此会跳过

String s = ""; //设置空字符串

for(int j = 1; j = 7;j++){ //产生7个随机数

int i = (int)(Math.random() * 36) + 1;//每个随机数产生方式,这里定义灵活,可以自由定义随机数产生的方式

if(i 10){

s = s + " 0" + i; //如果产生的随机数小于10的话做处理:这里就牵扯到一个重要的概念,简单叙述一下:

/*

当一个字符串与一个整型数项相加的意思是连接,上面的s = s + " 0" + i的意思是字符串s链接0再连接整型i值,而不会导致0和整型的i相加,

产生的效果为s0i,由于s为空字符串(上面定义过的),所以当i小于零时,在个位数前面加上0,比如产生的随机数i为7的话,显示效果为 07.

*/

}else{

s = s + " " + i; //如果产生的随机数比10打的话,那么加上空格显示,即数字和数字之间有个空格

}

//以上循环循环七次,以保证能出现7个随机数

}

tf.setText(s); //将产生的随机数全部显示在文本域上,用文本域对象tf调用它的设置文本的方法setText(String)实现。

}

//以下为线程延迟

try{

Thread.sleep(10); //线程类同步方法sleep,睡眠方法,括号里的单位为ms。

}catch(java.lang.InterruptedException e){

e.printStackTrace(); //异常捕获,不用多说。

}

}

}

//以下是上面设置的事件监听的具体处理办法,即监听时间处理方法,自动调用

public void actionPerformed(ActionEvent e){ //传入一个动作事件的参数e

String s = e.getActionCommand(); //设置字符串s来存储获得动作监听,上面的start

/*

以下这个条件语句块的作用为:用户点击开始后(捕获start,用方法getActionCommand()),将命令触发设置为true,从而执行上面的go方法中的循环体(因为循环体中要求isGo参数为true,而初始为false)。

执行循环快产生随机数,并将开始按钮不可编辑化,而用户只可以使用停止按钮去停止。如果用户按下停止时,也就是没有传入参数“start”的时候,

执行else语句块中的语句,isGo设置为false,将不执行上面go中的循环语句块,从而停止产生随机数,并显示,并且把开始按钮设置为可用,而把

停止按钮设置为不可用,等待用户按下开始再去开始新一轮循环产生随机数。

*/

if(s.equals("start")){ //如果捕获到start,也就是用户触发了动作监听器,那么下面处理

isGo = true; //设置isGo为true

b1.setEnabled(false); //将开始按钮设置为不可用

b2.setEnabled(true); //将停止按钮设置为可用

}else{

isGo = false; //将isGo设置为false,isGo为循环标志位

b2.setEnabled(false); //设置停止按钮为不可用(注意看是b2,b2是停止按钮)

b1.setEnabled(true); //设置开始按钮为可用

}

}

public static void main(String[] args){

new GoodLucky(); //产生类的实例,执行方法

}

}


网页题目:java初级30行代码的简单介绍
当前网址:http://cdkjz.cn/article/dodesgi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220