编写一个具有图形用户界面的Java程序

编写一个具有图形用户界面的Java程序,第1张

import javaximageio;

import javaxswing;

import javaawt;

import javaawtgeom;

import javaio;

/

@author Hardneedl

/

class ImageZoom extends JFrame {

private static final Dimension minSize = new Dimension(300, 200);

private static final Dimension maxSize = new Dimension(1024, 768);

private static final Dimension preferredSize = new Dimension(600, 400);

public Dimension getMaximumSize() {return maxSize;}

public Dimension getMinimumSize() {return minSize;}

public Dimension getPreferredSize() {return preferredSize;}

public String getTitle() {return "Frame Title";}

private class ImageCanvas extends JComponent{

private Image img;

private AffineTransform af=new AffineTransform();

private ImageCanvas(Image g,float s) {setImage(g);setScale(s);}

private void setImage(Image img){

thisimg = img;

if (isVisible()) paintImmediately(getBounds());

}

protected void paintComponent(Graphics g) {

superpaintComponent(g);

if (img!=null) {

Graphics2D g2d = (Graphics2D)gcreate();

g2ddrawImage(img,af,this);

}

}

private void setScale(float scale){

af = AffineTransformgetScaleInstance(scale,scale);

if (isVisible()) paintImmediately(getBounds());

}

};

private static ImageCanvas canvas;

private Image img;private float s;

ImageZoom(Image g,float s) throws HeadlessException {

img =g;thiss=s;

init();

doLay();

attachListeners();

}

private void init() {

canvas = new ImageCanvas(img,s);

}

private void doLay() {

Container container = getContentPane();

containeradd(canvas,BorderLayoutCENTER);

pack();

}

private void attachListeners() {

setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

}

public static void main(final String[] args) {

SwingUtilitiesinvokeLater(

new Runnable(){

public void run() {

try {

new ImageZoom(ImageIOread(new FileInputStream(args[0])),FloatparseFloat(args[1]))setVisible(true);

} catch (IOException e) {

eprintStackTrace();

}

}

}

);

}

}

你用的是TextField只能改变长度不能改变宽度的

你可以使用TextArea这个是可以改变宽度的

PS用Awt编程不好看 建议用Swing 可能在视觉上要好看一些 不过无所谓

package Test1;

import javaawt;

import javaxswing;

class Jie5 extends JFrame {

JPanel contentPane, northPanel, southPanel;

JTextField jtf1, jtf2;

JButton jb[];

public Jie5() {

contentPane = new JPanel();

northPanel = new JPanel();

southPanel = new JPanel();

jtf1 = new JTextField();

jtf2 = new JTextField();

jb = new JButton[10];

thissetContentPane(contentPane);

contentPanesetLayout(new BorderLayout(4,4));

contentPaneadd(northPanel,BorderLayoutNORTH);

northPanelsetLayout(new GridLayout(3, 3, 4, 4));

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

{

jb[i] = new JButton(""+(i+1));

northPaneladd(jb[i]);

}

contentPaneadd(southPanel,BorderLayoutSOUTH);

southPanelsetLayout(new GridLayout(1,2,4,4));

southPaneladd(jtf1);

jtf1setColumns(10);

southPaneladd(jtf2);

jtf2setColumns(10);

thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

thissetBounds(30,30,400,400);

thissetTitle("hello");

}

}

public class Demo5 {

/

@param args

/

public static void main(String[] args) {

try {

// TODO Auto-generated method stub

UIManager

setLookAndFeel("comsunjavaswingplafnimbusNimbusLookAndFeel");

} catch (Exception e) {

// TODO: handle exception

eprintStackTrace();

}

EventQueueinvokeLater(new Runnable(){

@Override

public void run()

{

Jie5 jie5 = new Jie5();

jie5setVisible(true);

}

});

}

}

制作一个类似于QQ登陆的界面,以下数据库部分,你可以自己修该下,默认为Access;

import javaawt;

import javaxswing;

import javaawtevent;

import javasql;

class JieMian extends JFrame;

{

JFrame f;

JLabel lb1;

JLabel lb2;

JTextField tf;

JPasswordField pwd;

JButton b1;

JButton b2;

JPanel p1;

JPanel p2;

Connection con;

Statement stmt;

String use;

char passw[]=new char[10];

ResultSet rs;

String k1,k2;

public void init()

f=new JFrame("登陆界面");

init();

b1addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ei)

{

try

{

ClassforName("sunjdbcodbcJdbcOdbcDriver");

con=DriverManagergetConnection("jdbc:odbc:dat");

stmt=concreateStatement();

use=tfgetText();

passw=pwdgetPassword();

String passww=new String(passw);

rs=stmtexecuteQuery("select from user where 用户名='"+use+"' and  密码='"+passww+"'");

while(rsnext())

{

Systemoutprintln(k1=rsgetString("用户名"));

Systemoutprintln(k2=rsgetString("密码"));

}

if(k1equals(use)&&(k2equals(passww)))

{

JOptionPaneshowMessageDialog(null,"用户登陆成功");//登陆成功

}

else

{

JOptionPaneshowMessageDialog(null,"请检查用户信息,登陆失败");//登录失败

}

rsclose();

stmtclose();

conclose();

}

catch (Exception ey){JOptionPaneshowMessageDialog(null,"请检查用户信息,登陆失败");}

}

});

fsetLayout(new GridLayout(2,1,10,5));

fsetVisible(true);

fsetSize(210,150);

fsetLocation(550,300);

fsetResizable(false);

fsetDefaultCloseOperation(EXIT_ON_CLOSE);

b2addActionListener(new ActionListener(){//58

public void actionPerformed(ActionEvent e){

fsetVisible(false);

}

});

}

}

public class DengLu

{

public static void main(String[] args)

{

new JieMian();

}

}。

在远标做过直接使用javaxswing和javaawt两个包

一个简单的GUI程序如下:

packagesix;

importjavaxswing;

importjavaawt;

importjavaawteventActionEvent;

importjavaawteventActionListener;

publicclassMain extendsJFrame{ //类Main继承自JFrame

privateJPanel pane = null;

privateJPanel p = null;

privateCardLayout card = null;

privateJButton button_1 = null;

privateJButton button_2 = null;

privateJButton b1 = null,b2 = null,b3 = null;

privateJPanel p1 = null,p2 = null,p3 = null;

publicMain() //

{

super("卡片布局管理器测试");

try{

UIManagersetLookAndFeel("comsunjavaswingplafwindowsWindowsLookAndFeel");

}

catch(Exception ex){

exprintStackTrace();

}

//创建新卡片布局

card = newCardLayout(5,5);

pane = newJPanel(card);

p = newJPanel();

button_1 = newJButton("< 上一步");

button_2 = newJButton("下一步 >");

b1 = newJButton("1");b2 = newJButton("2");b3 = newJButton("3");

b1setMargin(newInsets(2,2,2,2));

b2setMargin(newInsets(2,2,2,2));

b3setMargin(newInsets(2,2,2,2));

padd(button_1);padd(b1);padd(b2);padd(b3);padd(button_2);

p1 = newJPanel();

p2 = newJPanel();

p3 = newJPanel();

p1setBackground(ColorRED);

p2setBackground(ColorBLUE);

p3setBackground(ColorGREEN);

p1add(newJLabel("JPanel_1"));

p2add(newJLabel("JPanel_2"));

p3add(newJLabel("JPanel_3"));

paneadd(p1,"p1");paneadd(p2,"p2");paneadd(p3,"p3");

//翻转卡片布局动作

button_1addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvent e){

cardprevious(pane);

}

});

button_2addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvent e){

cardnext(pane);

}

});

b1addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvent e){

cardshow(pane, "p1");

}

});

b2addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvent e){

cardshow(pane,"p2");

}

});

b3addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvent e){

cardshow(pane, "p3");

}

});

thisgetContentPane()add(pane);

thisgetContentPane()add(p,BorderLayoutSOUTH);

thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

thissetSize(300, 200);

thissetVisible(true);

}

publicstaticvoidmain(String[]args)

{

newMain(); //

}

}

import javaxswing;import javaawt;import javaawtevent;

public class li53{

public static void main(String args[]){

ButtonDemo myButtonGUI=new ButtonDemo();

myButtonGUIsetVisible(true);

}

}

class ButtonDemo extends JFrame implements ActionListener{

public static final int Width=250;

public static final int Height=200;

ButtonDemo(){

setSize(Width,Height); setTitle("按钮事件样例");

Container conPane=getContentPane();

conPanesetBackground(Colorblue);

conPanesetLayout(new FlowLayout());

JButton redBut=new JButton("red");

redButaddActionListener(this);

conPaneadd(redBut);

JButton greenBut=new JButton("green");

greenButaddActionListener(this);

conPaneadd(greenBut);

}

public void actionPerformed(ActionEvent e){

Container conPane=getContentPane();

if(egetActionCommand()equals("red"))

conPanesetBackground(Colorred);

else if(egetActionCommand()equals("green"))

conPanesetBackground(Colorgreen);

}

}

我帮你改好了,在

else if(egetActionCommand()equals("green"));//这里多了个分号

conPanesetBackground(Colorgreen);

所以编程习惯要养成,在if后面无论几句话都要用{}将方法体括起来,你这样很容易错的。改起来也吃力。

//这是哪个背景

import javaawtImage;

import javaawt;

import javaxswing;

public class ImageBack extends JPanel{

Image im = null;

public imageback(Image im){

thisim=im;

int width = ToolkitgetDefaultToolkit()getScreenSize()width;

int height = ToolkitgetDefaultToolkit()getScreenSize()height;

thissetSize(width,height);

thissetVisible(true);

}

public static void main(String args[]){

}

public void paintComponent(Graphics g){

//清屏

superpaintComponent(g);

gdrawImage(im, 0, 0, thisgetWidth(),thisgetHeight(),this);

}

}

import javaxswing;

import javaioFile;

import javaioIOException;

import javanet;

/

学生管理系统主界面

/

public class StuMain extends JFrame{

//背景

Image image=null;

public StuMain() {

try {

image = ImageIOread(new File("images/startjpg"));

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

//把你要的背景放到背景面板里

imageback im = new imageback(image);

//把有背景的的JPanel放到JFrame里面

thisadd(im);

//添加框架的关闭事件处理

thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

thispack();

//设置框架的大小

//设置标题

thissetTitle("学生管理系统");

thissetVisible(true);

}

public static void main(String args[]){

new StuMain();

}

}

如果那不懂随时米我 qq896182794

以上就是关于编写一个具有图形用户界面的Java程序全部的内容,包括:编写一个具有图形用户界面的Java程序、java 图形界面、java 图形界面编程问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存