「java怎么编写图形界面」java怎么做图形界面

博主:adminadmin 2023-01-21 23:12:07 249

今天给各位分享java怎么编写图形界面的知识,其中也会对java怎么做图形界面进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java编写图形用户界面程序

运行如图

参考代码如下

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class RegDemo extends JFrame implements ActionListener{

JTextField jtf;

JPasswordField jpf;

public RegDemo() {

        //组件的创建, 和布局安排

JPanel jpc = new JPanel();//默认流式布局

JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10));//网格布局

jp1.setBorder(BorderFactory.createTitledBorder("用户注册"));

JLabel jl1 = new JLabel("用户名:");

jtf = new JTextField(10);

JLabel jl2 = new JLabel("密码:");

jpf = new JPasswordField(10);

                jpf.setEchoChar('*');//用*号来隐藏密码的显示

jp1.add(jl1);jp1.add(jtf);

jp1.add(jl2);jp1.add(jpf);

jpc.add(jp1);

add(jpc);

JButton jb1 = new JButton("提交");

jb1.addActionListener(this);

jb1.setActionCommand("yes");

JButton jb2 = new JButton("取消");

jb2.addActionListener(this);

jb2.setActionCommand("no");

JPanel jp2 = new JPanel();

jp2.add(jb1);jp2.add(jb2);

add(jp2,BorderLayout.SOUTH);

setTitle("用户注册界面");

        setSize(280, 280);

        setLocationRelativeTo(null);//窗口居中

        setDefaultCloseOperation(EXIT_ON_CLOSE);//

        setVisible(true);

}

public static void main(String[] args) {

new RegDemo();

}

@Override

public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("yes")){

String name  = jtf.getText().trim();

String pwd = new String(jpf.getPassword());

if(name.equals("")||pwd.equals("")){

JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码");

}else{

JOptionPane.showMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd);

}

}else{

jtf.setText("");

jpf.setText("");

}

}

}

java怎么实现图形化界面

java图形化界面还是有很多内容要学习的,可以参考 如下实例:

public class Test extends JFrame{

MyPanel mp=null;

public static void main(String[] args){

// TODO Auto-generated method stub

Test jf= new Test();

}

public Test(){

mp=new MyPanel();

this.add(mp);

//设置标题

this.setTitle("绘图");

//设置窗体大小

this.setSize(400, 300);

//设置窗体的位置

this.setLocation(100,100);

//限制窗体的大小

this.setResizable(false);

//关闭窗体时,同时退出java虚拟机

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//显示窗体

this.setVisible(true);

}

}

//定义一个MyPanel(我自己的面板,用于绘图和实现绘图区域)

class MyPanel extends JPanel

{

//覆盖JPanel的paint方法

//Graphics是绘图的重要类,可以把它理解成一只画笔

public void paint(Graphics g)

{

//1。调用父类函数完成初始化

super.paint(g);

// //画圆

// g.drawOval(100, 100, 20, 20);

// //画直线

// g.drawLine(50, 150,150, 200);

// //画矩形边框

// g.drawRect(150, 150, 30, 40);

//

// //设置颜色。默认为黑色

// g.setColor(Color.blue);

// //填充矩形

// g.fillRect(10, 10, 20, 30);

//画弧形

g.drawArc(200,10, 100,150, 120,-80);

//在面板上画图片

Image im=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("图片路径"));

//显示图片

g.drawImage(im, 10, 10,200,180,this);

//画字

g.setColor(Color.red);

g.setFont(new Font("华文彩云",Font.BOLD,20));

g.drawString("要写的字", 80,220);

}

}

java编写一个图形界面程序

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.border.Border;

class MainFrame extends JFrame {

private static final long serialVersionUID = 1L;

private MapString, Integer sizes = new HashMapString, Integer();

private MapString, Integer styles = new HashMapString, Integer();

private MapString, Integer toppings = new HashMapString, Integer();

public MainFrame() {

sizes.put("Extra Large", 10);

sizes.put("Large", 8);

sizes.put("Medium", 5);

sizes.put("Small", 3);

styles.put("Deep Dish", 20);

styles.put("Regular", 10);

styles.put("Thin Crust", 5);

styles.put("Chicago", 3);

toppings.put("Cheese", 8);

toppings.put("Tomato", 7);

toppings.put("Peppers", 6);

toppings.put("Peperoni", 5);

this.setTitle("布局及事件处理");

this.setSize(450, 350);

this.setLayout(new BorderLayout());

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel lblTitle = new JLabel();

lblTitle.setText("Pizzeria Juno");

lblTitle.setFont(new Font("宋体", Font.BOLD, 36));

lblTitle.setHorizontalAlignment(SwingConstants.CENTER);

this.add("North", lblTitle);

JPanel bodyPanel = new JPanel();

bodyPanel.setLayout(new GridLayout(2, 1));

this.add("Center", bodyPanel);

JPanel listPanel = new JPanel();

listPanel.setLayout(new GridLayout(1, 3));

listPanel.setSize(200, 200);

bodyPanel.add(listPanel);

Border lineBorder = BorderFactory.createLineBorder(Color.BLACK);

JPanel sizePanel = new JPanel();

sizePanel.setLayout(new BorderLayout());

listPanel.add(sizePanel);

JLabel sizeTitle = new JLabel();

sizeTitle.setText("Sizes");

sizePanel.add("North", sizeTitle);

JList sizeList = new JList(sizes.keySet().toArray());

sizeList.setSize(100, 100);

sizeList.setBorder(lineBorder);

sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

sizePanel.add(sizeList);

JPanel stylePanel = new JPanel();

stylePanel.setLayout(new BorderLayout());

listPanel.add(stylePanel);

JLabel styleTitle = new JLabel();

styleTitle.setText("Styles");

stylePanel.add("North", styleTitle);

JList styleList = new JList(styles.keySet().toArray());

styleList.setSize(100, 100);

styleList.setBorder(lineBorder);

styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

stylePanel.add(styleList);

JPanel toppingPanel = new JPanel();

toppingPanel.setLayout(new BorderLayout());

listPanel.add(toppingPanel);

JLabel toppingTitle = new JLabel();

toppingTitle.setText("Toppings");

toppingPanel.add("North", toppingTitle);

JList toppingList = new JList(toppings.keySet().toArray());

toppingList.setSize(100, 100);

toppingList.setBorder(lineBorder);

toppingPanel.add(toppingList);

JTextArea txtResult = new JTextArea();

txtResult.setEditable(false);

bodyPanel.add(txtResult);

JPanel bottomPanel = new JPanel();

bottomPanel.setLayout(new GridLayout(1, 3));

this.add("South", bottomPanel);

JLabel label1 = new JLabel("Click to complete order");

bottomPanel.add(label1);

JButton btnRingUp = new JButton("Ring up");

btnRingUp.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if(sizeList.getSelectedValue() == null) {

JOptionPane.showMessageDialog(MainFrame.this, "Please select size.");

return;

}

if(styleList.getSelectedValue() == null) {

JOptionPane.showMessageDialog(MainFrame.this, "Please select style.");

return;

}

if(toppingList.getSelectedValue() == null) {

JOptionPane.showMessageDialog(MainFrame.this, "Please select topping.");

return;

}

float total = 0;

String size = sizeList.getSelectedValue().toString();

total += sizes.get(size);

String style = styleList.getSelectedValue().toString();

total += styles.get(style);

String result = size + " Pizza, " + style + " Style";

Object[] toppings = toppingList.getSelectedValues();

for(Object topping : toppings) {

result += "\n  +" + topping.toString();

total += MainFrame.this.toppings.get(topping.toString());

}

result += "\n  Total: " + total;

txtResult.setText(result);

}

});

bottomPanel.add(btnRingUp);

JButton btnQuit = new JButton("Quit");

btnQuit.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

MainFrame.this.dispose();

}

});

bottomPanel.add(btnQuit);

}

}

public class App {

public static void main(String[] args) {

MainFrame mainFrame = new MainFrame();

mainFrame.setVisible(true);

}

}

JAVA图形界面程序编写

我真的是抽风了,手痒了,给你写了这段代码,如果楼主、、、

嘻嘻,追点分给我吧

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.*;

public class baidu_9 extends JFrame implements ActionListener

{

static final String OUTPUT="C://Test.txt";

JPanel pnl;

JLabel lbl;

JTextField txt1,txt2;

JButton btnCopy,btnClear,btnOutput,btnColor;

public baidu_9()

{

super("百度题目");

pnl=new JPanel();

this.setContentPane(pnl);

pnl.setLayout(null);

pnl.setBackground(Color.WHITE);

lbl=new JLabel("百度");

txt1=new JTextField(10);

txt2=new JTextField(10);

btnCopy=new JButton("复制");

btnCopy.addActionListener(this);

btnClear=new JButton("清除");

btnClear.addActionListener(this);

btnOutput=new JButton("写入");

btnOutput.addActionListener(this);

btnColor=new JButton("变色");

btnColor.addActionListener(this);

lbl.setBounds(100, 10, 80, 20);

txt1.setBounds(10, 40, 100, 20);

txt2.setBounds(120, 40, 100, 20);

btnCopy.setBounds(10, 70, 60, 20);

btnClear.setBounds(75, 70, 60, 20);

btnOutput.setBounds(140, 70, 60, 20);

btnColor.setBounds(205, 70, 60, 20);

pnl.add(lbl);

pnl.add(txt1);

pnl.add(txt2);

pnl.add(btnCopy);

pnl.add(btnClear);

pnl.add(btnOutput);

pnl.add(btnColor);

setSize(300,150);

setVisible(true);

}

public void Copy()

{

txt2.setText(txt1.getText());

}

public void Clear()

{

txt1.setText("");

txt2.setText("");

pnl.setBackground(Color.WHITE);

}

public void Color()

{

pnl.setBackground(Color.BLACK);

}

public void Ouput()

{

try

{

File fl=new File("C:\\Test.txt");

FileWriter fw = new FileWriter(fl);

BufferedWriter bw = new BufferedWriter(fw);

bw.write(txt1.getText());

bw.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==btnCopy)

this.Copy();

if(e.getSource()==btnClear)

this.Clear();

if(e.getSource()==btnColor)

this.Color();

if(e.getSource()==btnOutput)

this.Ouput();

}

public static void main(String[] args)

{

new baidu_9();

}

}

关于java怎么编写图形界面和java怎么做图形界面的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。