javagui编程代码 Javagui编程
帮我看看这段Java GUI代码
你好,在构造方法FrameOut()中调用setLayout()方法加入一种控件布局形式,例如加入setLayout(new FlowLayout());即可以流布局的形式显示控件。
创新互联建站主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、成都全网营销、网站程序开发、HTML5响应式网站建设、手机网站制作、微商城、网站托管及成都网站维护公司、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为成都办公窗帘行业客户提供了网站设计服务。
完整代码如下:
import java.awt.*;
import java.awt.event.*;
public class ApplicationInOut{
public static void main(String args[ ]){
new FrameInOut();
}
}
class FrameInOut extends Frame implements ActionListener{
Label prompt;
TextField input,output;
FrameInOut( ){
super("图形界面的Java Application程序");
prompt=new Label("Java 是面向对象的语言吗?");
input=new TextField(6);
output=new TextField(20);
add(prompt);
add(input);
add(output);
input.addActionListener(this);
setLayout(new FlowLayout()); //此处即为添加布局形式
setSize(800,600);
setVisible(true); //show( );
}
public void actionPerformed(ActionEvent e){
output.setText(input.getText()+"OK!");
}
}
编写一个Java GUI
试一下下面的代码
(如果点击按钮后没有任何变化,将窗口最小化一下就有了)
没有出现这个问题的话,也请告诉我一下~
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class painting extends JFrame implements ActionListener{
private JButton round,rectangle,ellipse,beeline;
private JLabel xaxis,yaxis,remain,information;
private JTextField xTF,yTF;
private BorderLayout layout;
private Container cp;
private JPanel pCenter;
VectorObject v=new VectorObject(); //定义一个集合类用于存储按钮对象
public painting(){ //构造方法 ------------------框架初始化-------------------
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("painting");
setSize(400,500);
layout = new BorderLayout();
cp = getContentPane();
cp.setLayout(layout);
round= new JButton("画圆");
rectangle= new JButton("画矩形");
ellipse= new JButton("画椭圆");
beeline= new JButton("画直线");
xaxis=new JLabel("x坐标");
yaxis=new JLabel("y坐标");
remain=new JLabel("右下角坐标(400,500) ");
xTF=new JTextField("0",5);
yTF=new JTextField("0",5);
JPanel pUp= new JPanel();//第一个面板 在上部
pUp.add(remain);
pUp.add(xaxis);//置两个文本框
pUp.add(xTF);
pUp.add(yaxis);
pUp.add(yTF);
cp.add(pUp, "North");
//pCenter=new JPanel();//第二个面板 在中部
//pCenter.add(information);//置显示说明与画图区
//cp.add(pCenter,"Center");
JPanel pDown= new JPanel();//第三个面板 在下部
pDown.add(round);// 置四个按钮
pDown.add(rectangle);
pDown.add(ellipse);
pDown.add(beeline);
cp.add(pDown, "South");
round.addActionListener(this); //置按钮监听--------------按钮行为监听与响应-------------
rectangle.addActionListener(this);
ellipse.addActionListener(this);
beeline.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {//监听响应
v.add(e.getSource());//将按钮情况存入v中
}
public void paint(Graphics g) { //--------------绘图响应-------------
super.paint(g);
int xx=Integer.parseInt(xTF.getText());//获取位置值
int yy=Integer.parseInt(yTF.getText());
int size=0;
Object o;
//while(v.size()!=size){//当用户点击按钮选择某一种图形时,v的大小就会比size值大1,当绘图完成后,v.size又等于size;效果就是:出现点击 即刻处理
o=v.lastElement();
if(o == round) {g.drawOval(xx,yy,50,50);}
else if (o == rectangle){g.drawRect(xx,yy,100,50);}
else if (o == ellipse) {g.drawOval(xx,yy,100,50);}
else if(o == beeline) {g.drawLine(xx,yy,xx+100,yy);}
size++;
}
}
public static void main(String[] args){ // ------------程序入口-------------
JFrame frame = new painting();
frame.setVisible(true);
}
}
编写一个java GUI程序(其实帮我改改)
把frame=new subJFrame("DrawShapes");改成frame=new JFrame("DrawShapes");
程序基本没问题,在public void paint(Graphics g)中加上如下程序就可以了。
public void paint(Graphics g){
switch(i){
case 1: g.drawOval(20,20,40,40);break;
case 2: g.drawRect(20,20,40,40);break;
case 3: g.drawOval(20,30,40,50);break;
case 4: g.drawLine(20,20,40,40);break;
}
}
JAVA GUI 代码问题
import java.awt.Container;
import java.awt.FlowLayout;
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.SwingUtilities;
class aa implements ActionListener
{
JButton a, b;
public aa(JButton c, JButton d)
{
a = c;
b = d;
}
public void actionPerformed(ActionEvent e)
{
a.setVisible(false);
b.setVisible(false);
}
}
public class J1 extends JFrame
{
private static final long serialVersionUID = 1L;
JButton j1 = new JButton("Game1");
JButton j2 = new JButton("Game2");
JButton j3 = new JButton("点击摇骰子");
J1()
{
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
final Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(j1);
c.add(j2);
j1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
c.add(new JLabel("Game1"));
j3.setVisible(true);
c.add(j3);
}
});
j1.addActionListener(new aa(j1, j2));
j2.addActionListener(new aa(j1, j2));
j3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int a = (int) (Math.random() * 6);
switch(a)
{
case 0:
c.add(new JLabel("1"));
break;
case 1:
c.add(new JLabel("2"));
break;
case 2:
c.add(new JLabel("3"));
break;
case 3:
c.add(new JLabel("4"));
break;
case 4:
c.add(new JLabel("5"));
break;
case 5:
c.add(new JLabel("6"));
break;
}
SwingUtilities.updateComponentTreeUI(c);
}
});
}
public static void main(String[] args)
{
new J1();
}
}
编写Java GUI程序。
dty@ubuntu:~$ cat CC.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
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;
public class CC extends JFrame implements ActionListener {
private JPanel jp = null;
private JButton b1 = null, b2 = null;
public CC() {
Container c = this.getContentPane();
//创建panel
jp = new JPanel();
//为panel设置底色
jp.setBackground(Color.red);
//jp用流水布局
jp.setLayout(new FlowLayout());
//创建按钮
b1 = new JButton("红色");
b2 = new JButton("蓝色");
/**
* 添加按钮事件
*/
b1.addActionListener(this);
b2.addActionListener(this);
/**
* 将按钮添加相应panel上
*/
jp.add(b1);
jp.add(new JLabel());
jp.add(b2);
/**
* 将panel添加到容器
*/
c.add(jp, BorderLayout.CENTER);
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new CC();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == b1) {
jp.setBackground(Color.red);
} else if (e.getSource() == b2) {
jp.setBackground(Color.blue);
}
}
}
求解:写一段Java程序,要求简单实现计算器的功能,是GUI编程,代码简洁最好。
import java.awt.BorderLayout;
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 计算器 implements ActionListener {
JTextField jtf = new JTextField(10);
private boolean append = false;
private String op1 = "0";
private String operator = "+";
@Override
public void actionPerformed(ActionEvent e) {
String comn = e.getActionCommand();
// 处理数字
if ("0123456789".indexOf(comn) != -1) {
if (append) {// 追加
String temp = jtf.getText();
jtf.setText(temp + comn);
} else {// 替换
jtf.setText(comn);
append = true;
}
}
// 处理运算符
else if ("+-*/".indexOf(comn) != -1) {
op1 = jtf.getText();
operator = comn;
append = false;
} else if ("=".indexOf(comn) != -1) {
String op2 = jtf.getText();
double d1 = Double.parseDouble(op1);
double d2 = Double.parseDouble(op2);
if ("+".equals(operator)) {
d1 = d1 + d2;
} else if ("-".equals(operator)) {
d1 = d1 - d2;
} else if ("*".equals(operator)) {
d1 = d1 * d2;
} else if ("/".equals(operator)) {
d1 = d1 / d2;
}
jtf.setText(d1 + "");
append = false;
} else if (".".equals(comn)) {
String temp = jtf.getText();
if (temp.indexOf(".") == -1) {
jtf.setText(temp + ".");
append = true;
}
} else if ("+/-".equals(comn)) {
String temp = jtf.getText();
if (temp.startsWith("-1")) {
jtf.setText(temp.substring(1));
} else {
jtf.setText("-" + temp);
}
} else if ("Backspace".equals(comn)) {
String temp = jtf.getText();
if (temp.length() 0) {
jtf.setText(temp.substring(0, temp.length() - 1));
}
} else if ("CE".equals(comn) || "C".equals(comn)) {
jtf.setText("0");
append = false;
}
}
public 计算器() {
JFrame jf = new JFrame("计算器");
jf.add(jtf, BorderLayout.NORTH);
String[] s1 = { "Backspace", "CE", "C", "+", "7", "8", "9", "/", "4",
"5", "6", "*", "1", "2", "3", "-", "0", "+/-", ".", "=" };
JPanel jp = new JPanel();
jf.add(jp, BorderLayout.CENTER);
GridLayout gl = new GridLayout(5, 4);
jp.setLayout(gl);
JButton[] jb = new JButton[s1.length];
for (int i = 0; i s1.length; i++) {
jb[i] = new JButton(s1[i]);
jp.add(jb[i]);
jb[i].addActionListener(this);
}
jf.add(jp);
jtf.setEditable(false);
jf.setLocation(400, 300);
jf.pack();
jf.setResizable(false);// 设置窗口的大小不可变
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new 计算器();
}
}
这个功能比较简单 不知道能不能满足要求
当前文章:javagui编程代码 Javagui编程
本文来源:http://ybzwz.com/article/hiisgd.html