‘壹’ 求教用java实现如下图形界面功能。
粗略地做了一个出来,闪烁的问题暂时无法解决,其他的还可以吧,先上代码:
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.awt.event.MouseMotionListener;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importjavax.imageio.ImageIO;
importjavax.swing.JFrame;
public桐旁,MouseMotionListener{
privateImageoffScreenImage;
privateImagebackground,sprite;
privatestaticfinalintWIDTH=800;
privatestaticfinalintHEIGHT=600;
privateintx0=0,y0=0;
privateintx1,y1;
privateList<Bounds>bounds;
privateBoundsmCurrentBounds=null;
publicTestImage(){
super("绘图");
bounds=newArrayList<Bounds>();
}
privatevoidlaunch(){
setSize(WIDTH,HEIGHT);
setResizable(false);
setVisible(true);
try{
background=ImageIO.read(TestImage.class.getResource("/res/clock.jpg"));
sprite=ImageIO.read(TestImage.class.getResource("/res/light_buble2.png"链槐));
}catch(IOExceptione){
e.printStackTrace();
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
newThread(newRepaint()).start();
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
publicvoipdate(Graphicsg){
if(null==offScreenImage){
offScreenImage=createImage(WIDTH,HEIGHT);
}
GraphicsgImage=offScreenImage.getGraphics();
gImage.clearRect(0,0,WIDTH,HEIGHT);
paint(gImage);
g.drawImage(offScreenImage,0,0,null);
}
@Override
publicvoidpaint(Graphicsg){
g.drawImage(background,0,0,WIDTH,HEIGHT,null);
for(Boundsbound:bounds)棚轮友{
g.drawImage(sprite,bound.x0,bound.y0,bound.x1-bound.x0,bound.y1-bound.y0,null);
}
if(null!=mCurrentBounds){
g.drawImage(sprite,mCurrentBounds.x0,mCurrentBounds.y0,
Math.abs(mCurrentBounds.x1-mCurrentBounds.x0),
Math.abs(mCurrentBounds.y1-mCurrentBounds.y0),null);
}
}
publicstaticvoidmain(String[]args){
newTestImage().launch();
}
@Override
publicvoidmouseClicked(MouseEvente){
}
@Override
publicvoidmouseEntered(MouseEvente){
}
@Override
publicvoidmouseExited(MouseEvente){
}
@Override
publicvoidmousePressed(MouseEvente){
x1=x0=e.getX();
y1=y0=e.getY();
mCurrentBounds=newBounds(x0,y0,x1,y1);
}
@Override
publicvoidmouseReleased(MouseEvente){
if(null!=mCurrentBounds){
bounds.add(mCurrentBounds);
}
mCurrentBounds=null;
}
@Override
publicvoidmouseDragged(MouseEvente){
if(null!=mCurrentBounds){
intx=e.getX();
inty=e.getY();
if(x>x0){
x1=x;
}else{
x0=x;
}
if(y>y0){
y1=y;
}else{
y0=y;
}
mCurrentBounds.setBounds(x0,x1,y0,y1);
}
}
@Override
publicvoidmouseMoved(MouseEvente){
}
{
@Override
publicvoidrun(){
while(true){
try{
Thread.sleep(30L);
repaint();
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
}
classBounds{
publicintx0;
publicinty0;
publicintx1;
publicinty1;
publicBounds(intx0,inty0,intx1,inty1){
this.x0=x0;
this.y0=y0;
this.x1=x1;
this.y1=y1;
}
publicvoidsetBounds(intx0,intx1,inty0,inty1){
this.x0=x0;
this.x1=x1;
this.y0=y0;
this.y1=y1;
}
}
运行时就是鼠标点击画板并按住,然后拖拽就可以画一个电灯出来。所需的图片资源在附件内,根据代码相信你应该能够知道要放到哪个目录下吧。运行效果也截一个图给你。
‘贰’ Java编写一个图形界面
我觉得还是我的好些蚂亏。。。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileOutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class TestSwing extends JFrame{
private JLabel label1, label2, label3, label4,label5,label6,label7;
private JPanel panel,panel1,panel2;
private JTextField tField1,tField2,tField3,tField4,tField5,tField6,tField7,tField8,tField9;
private JButton button;
private String filepath="c:\\info.dat";
public TestSwing(){
init();
this.setSize(300, 280);
this.setLocation(
(Toolkit.getDefaultToolkit().getScreenSize().width - this
.getSize().width) /搏物尘基禅 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - this
.getSize().height) / 2);
this.setResizable(false);
this.setVisible(true);
this.setTitle("info");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(){
initPanel();
initLabel();
initTextField();
initButton();
initContent();
}
private void initContent(){
panel1.add(label1);
panel1.add(tField1);
panel1.add(label2);
panel1.add(tField2);
panel1.add(label3);
panel1.add(tField3);
panel1.add(label4);
panel1.add(tField4);
panel1.add(label5);
panel1.add(tField5);
panel1.add(label6);
panel1.add(tField6);
panel1.add(label7);
panel1.add(tField7);
panel1.add(tField8);
panel1.add(tField9);
panel2.add(button);
panel.add(panel1,BorderLayout.CENTER);
panel.add(panel2,BorderLayout.SOUTH);
getContentPane().add(panel);
}
private void initPanel(){
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.WHITE);
panel1 = new JPanel();
GridLayout upLayout = new GridLayout(8, 2);
panel1.setLayout(upLayout);
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
}
private void initLabel(){
label1 = new JLabel("IP地址", SwingConstants.LEFT);
label2 = new JLabel("端口", SwingConstants.LEFT);
label3 = new JLabel("访问地址", SwingConstants.LEFT);
label4 = new JLabel("XXXXX", SwingConstants.LEFT);
label5 = new JLabel("XXXXX", SwingConstants.LEFT);
label6 = new JLabel("XXXXX", SwingConstants.LEFT);
label7 = new JLabel("XXXXX", SwingConstants.LEFT);
}
private void initTextField(){
tField1 = new JTextField(10);
tField2 = new JTextField(10);
tField3 = new JTextField(10);
tField4 = new JTextField(10);
tField5 = new JTextField(10);
tField6 = new JTextField(10);
tField7 = new JTextField(10);
tField8 = new JTextField(10);
tField9 = new JTextField(10);
}
private void initButton(){
button = new JButton("完成");
button.addMouseListener(new MouseAdapter(){
//左键点击
public void mouseClicked(MouseEvent e) {
String ipv = tField1.getText().trim();
String portv = tField2.getText().trim();
String addrv = tField3.getText().trim();
String xx1v = tField4.getText().trim();
String xx2v = tField5.getText().trim();
String xx3v = tField6.getText().trim();
String xx4v = tField7.getText().trim();
String inputx = tField8.getText().trim();
String inputxv = tField9.getText().trim();
createFile(ipv, portv, addrv, xx1v, xx2v, xx3v, xx4v, inputx, inputxv);
JOptionPane.showMessageDialog(null, "保存成功");
tField1.setText("");
tField2.setText("");
tField3.setText("");
tField4.setText("");
tField5.setText("");
tField6.setText("");
tField7.setText("");
tField8.setText("");
tField9.setText("");
}
});
}
public static void main(String[]args){
new TestSwing();
}
private void createFile(String ipv,String portv ,String addrv,String xx1v,String xx2v,String xx3v,String xx4v,String inputx,String inputxv){
StringBuffer context = new StringBuffer();
context.append("ip=").append(ipv).append("\n");
context.append("port=").append(portv).append("\n");
context.append("addr=").append(addrv).append("\n");
context.append("xx1=").append(xx1v).append("\n");
context.append("xx2=").append(xx2v).append("\n");
context.append("xx3=").append(xx3v).append("\n");
context.append("xx4=").append(xx4v).append("\n");
context.append(inputx).append("=").append(inputxv).append("");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filepath);
fos.write(context.toString().getBytes());
} catch (Exception e) {
} finally {
if (fos != null)
try {
fos.close();
} catch (Exception e) {
}
}
}
}
‘叁’ 如何在Java中利用图形界面技术设计一个能够完成加、减、乘、除和取余运算的图形用户界面应用程序
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Z extends JFrame implements ActionListener, FocusListener {
private String[] total = { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", ".", "=", "/" }; // 总数组
private String[] other = { "clear", "delete" }; // 其它功能数组
private String[] symbol = { "+", "-", "*", "/" }; // 符号数组
private String multinumber = "0", operator1 = "", exists = "", operator2 = null, s = "";
// multinumber(多位数) operator1(运算符) exists(存在的)
private double d1, d2 = 0;
private int Count = 0;
JPanel jptotal = new JPanel(); // 总面板
JTextField jtexternal = new JTextField("0"); // 外部文本框
JTextField jtinside = new JTextField(); // 内部文本框
JPanel jptop1 = new JPanel(); // 顶部面板1
JPanel jptop2 = new JPanel(); // 顶部面板2
JPanel jpmiddle = new JPanel(); // 中部面板
public Z() {
jptotal.setLayout(new BorderLayout());
// 使用边界布局管理器
jptop1.setLayout(new GridLayout(2, 0));
jptop1.add(jtexternal);
jptop2.setLayout(new GridLayout(0, 2));
for (int i = 0; i < other.length; i++) {
JButton jbi = new JButton("" + other[i]);
jbi.setSize(100, 10);
jptop2.add(jbi);
jbi.addActionListener(this);
}
jpmiddle.setLayout(new GridLayout(4, 0));
for (int i = 0; i < total.length; i++) {
JButton jbi = new JButton("" + total[i]);
jpmiddle.add(jbi);
jbi.addActionListener(this);
}
jtexternal.addFocusListener(this);
jtexternal.add(jtinside);
jtinside.addFocusListener(this);
// 上面三行代码的作用是让文本框不可被选中
jptop1.add(jptop2);
jptotal.add(jptop1, "North");
// 以jp1为一个整体,让jp1里面的内容从最上面开始填充
jptotal.add(jpmiddle);
add(jptotal);
setVisible(true);
setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
// ctrl+/ :注释
public void actionPerformed(ActionEvent e) throws ArithmeticException {
try {
String inputing = e.getActionCommand();
// 表示获取当前输入的内容
if (inputing.equals(".")) {
Count++;
}
exists = jtexternal.getText();
if (operator2 != null) {
int k = 0;
for (int i = 0; i < symbol.length; i++) {
if (operator2.equals(symbol[i])) {
k = k + 1;
}
if (inputing.equals(symbol[i])) {
k = k + 1;
}
if (k == 2) {
jtexternal.setText(exists.substring(0, exists.length() - 1) + inputing);
operator1 = inputing;
return;
}
// 实现四则运算符不可同时存在,新输入的替换旧的
if (operator2.equals(symbol[i]) && inputing.equals(".")) {
return;
}
// 实现四则运算符和点不可挨着出现(点后面可以出现四则运算符,点前面不可出现)
}
}
if (exists.equals("0")) {
if (inputing.equals(".") || (inputing.equals("+")) || (inputing.equals("-")) || (inputing.equals("*"))
|| (inputing.equals("/"))) {
jtexternal.setText("0" + inputing);
} else {
jtexternal.setText(inputing);
}
} else {
jtexternal.setText(exists + inputing);
}
// 实现如果0前面没有非0数字的话,那么无论输入多少次0,都只显示一个0,如果输入其它数字,0将会被替换
if ((d1 == 0.0 || d2 == 0.0) && inputing.equals("=") && !exists.substring(0, 1).equals("0")) {
return;
}
// 实现如果文本框内存在数字,那么输入等号,将打印其本身
if (inputing.equals("clear")) {
multinumber = "0";
exists = "";
operator1 = "";
jtexternal.setText("0");
operator2 = null;
d2 = 0;
s = "";
Count = 0;
d1 = 0.0;
// 实现,当点击clear时,文本框内将被清空至0,所有数据都将还原至初始状态
} else if (inputing.equals("delete")) {
if (exists.length() <= 1 || s.equals("=")) {
jtexternal.setText("0");
multinumber = "0";
exists = "";
operator1 = "";
operator2 = null;
d2 = 0;
s = "";
Count = 0;
d1 = 0;
// 实现,按delete删除刚输入的内容,如果将最后一个数字删除,那么效果同按clear
} else {
if (inputing.equals("+") || inputing.equals("-") || inputing.equals("*") || inputing.equals("/")) {
Count = 1;
}
jtexternal.setText(exists.substring(0, exists.length() - 1));
// 将operator2存储上一次值
operator2 = exists.substring(exists.length() - 2, exists.length() - 1);
/*
* 实现如果四则运算符后面有数字的话
* 那么如果此时按删除键
* 删除之后如果新输入的还是四则运算符话
* 那么将会将原来的四则运算符替换成新输入的
*/
}
} else if (!inputing.equals("+") && !inputing.equals("-") && !inputing.equals("*") && !inputing.equals("/")
&& !inputing.equals("=")) {
// 判断输入是否为数字
if (s.equals("=")) {
if (exists.equals("除数不能为0") || new Double(exists) == d1) {
if (inputing.equals(".")) {
jtexternal.setText("0" + inputing);
} else {
jtexternal.setText(inputing);
}
}
s = "";
}
// 实现一次计算完成之后,可以进行新的运算
if (multinumber == null) { // 多位数计算
multinumber = inputing;
} else {
multinumber += inputing;
}
d1 = new Double(multinumber);
if (operator1.equals("+")) { // 进行四则运算
if (d2 == 0) {
if (operator1.equals("*") || operator1.equals("/")) {
d2 = 1;
}
}
// 实现,当四则运算符后面没有数字时,那么输入等号后结果将为符号前面的结果,如果符号前面只有一个数字,那么将打印自身
d1 = d1 + d2;
} else if (operator1.equals("-")) {
d1 = d2 - d1;
} else if (operator1.equals("*")) {
d1 = d1 * d2;
} else if (operator1.equals("/")) {
d1 = d2 / d1;
}
// 利用临时变量f2,实现四则运算
operator2 = inputing;
} else if (!inputing.equals("=")) { // [四则运算符] 输入等号后继续进行计算
multinumber = "0";
operator1 = inputing;
d2 = d1;
operator2 = inputing;
s = "";
Count = 0;
// 实现小数运算
} else { // 就是 [=] 得出最终结果
jtexternal.setText("" + d1);
Count = 0;
s = "=";
multinumber = "0";
if (operator2.equals("0") && "/".equals(operator1)) {
operator2 = null;
d1 = 0;// 将非法运算值替换成正确值
throw new ArithmeticException();// 指定抛出异常的条件
//赋值计算不抛算数异常,如果想让其抛出,必须指定条件重新抛出异常
} else {
operator1 = inputing;
operator2 = null;
d2 = 0;
} // 计算结果
if (inputing.equals("=") && (d1 == 0.0 || d2 == 0.0)) {
return;
}
// 实现等号不可重复输入
}
} catch (ArithmeticException e1) {
jtexternal.setText("除数不能为0");
// 指定抛出异常内容
}
}
public static void main(String[] args) {
new Z();
}
public void focusGained(FocusEvent e) {
jtinside.requestFocus();
// 让焦点聚焦到内部文本框,实现外部文本框的不可聚焦功能
}
public void focusLost(FocusEvent e) {
}
}
‘肆’ java 如何在图形界面显示程序运行结果
如果只是一些文本要显示,可以在上面加个JTextArea,JTextField之类的组件,用这些组件的对象调用setText("")方法(括号里是字符串)来显示文本
比如:
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.HashMap;
importjava.util.Map;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
/**文本区域
*
*@authorAdministrator
*
*/
{
=2306597749734227012L;
privateJButtonb1=newJButton("AddData"),b2=newJButton("ClearData");
privateJTextAreatext=newJTextArea(20,40);
privateMap<String,String>map=
newHashMap<String,String>();
public迹信TextArea(){
//Useupallthedata:
map.put("a","1");
map.put("b","2");
b1.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
for(@SuppressWarnings("rawtypes")Map.Entryme:map.entrySet()){
text.append(me.getKey()+":"+me.getValue()+" ");
}
}
});
b2.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
text.setText("");
}
});
setLayout(newFlowLayout());
add(newJScrollPane(text));
add(b1);
add(b2);
租核
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
GUItools.SwingConsole.run(newTextArea(),475,475);
}
}
JTextField和这差不多,只不过是显示单行的。
如果要显示其他字体的,可以用HTML格式,如下:
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
/**Swing组件上的HTML
*
*@authorAdministrator
*
*/
@SuppressWarnings("serial")
{
privateJButtonb=newJButton(
"<html><b>姿型轮<fontsize=+2>"+
"<center>Hello!<br><i>Pressmenow!");
//必须使文本以"<html>"标记开始。
publicHTMLButton(){
//TODOAuto-generatedconstructorstub
b.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
add(newJLabel("<html>"+"<i><fontsize=+4>Kapow!"));
//Forceare-layouttoincludethenewlabel:
validate();
}
});
setLayout(newFlowLayout());
add(b);
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
GUItools.SwingConsole.run(newHTMLButton(),200,500);
}
}
‘伍’ 怎样用java编写图形界面的Application程序
java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码租碰,然后自己再根据需要修改,如:
package mainFrame;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame。