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

博主:adminadmin 2022-12-14 05:57:06 62

本篇文章给大家谈谈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编写图形界面的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java做图形界面、java编写图形界面的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-14,除非注明,否则均为首码项目网原创文章,转载请注明出处。