myeclipse中的可视化图形界面

这篇博客介绍了如何在MyEclipse中创建一个Java图形界面应用,包括步骤和代码实现。通过新建工程,创建可视化图形界面,并展示了包含时间显示、按钮监听和面板布局的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

建立一个工程文件 File---new--java  project                    Use default location 选择保存的地址

建立一个可视化图形界面  leelen(工程名称)点击右键 new--other-- Application Window       new--folder--images(photo)




下面为关于一个图形界面,按键的监听,和面板排版的一个实例

(引用的照片为images内容,未上传,可在百度云里查找)


package leelen;


import java.awt.EventQueue;
import java.awt.EventQueue;


import javax.swing.JFrame;
import javax.swing.JPanel;


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;


import javax.swing.JLabel;


import java.awt.Font;


import javax.swing.JSplitPane;
import javax.swing.JButton;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;


import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.awt.Graphics;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


import javax.swing.*;




public class Family    {
private JTextArea textArea;
private JFrame frame;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //时间显示格式
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Family window = new Family();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the application.
*/
public Family() {
initialize();
}


boolean  firstDigit =true;
/**
     * 处理数字键被按下的事件
     *
     * @param key
     */
    private void handleNumber(String key) {
        if (firstDigit) {
            // 输入的第一个数字
        textArea.setText(key);
        } else if (!key.equals(".")) {
            // 如果输入的不是小数点,则将数字附在结果文本框的后面
        textArea.setText(textArea.getText() + key);
        }
        // 以后输入的肯定不是第一个数字了
        if(key.equals("c")){
        textArea.setText("");
        }
        if(key.equals("00#0001")){
        textArea.setText("00#0001");
         }
         firstDigit = false;
        firstDigit = false;
    }




/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1000, 720);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer timer = new Timer();
   timer.schedule(new ShowTime(), new Date(), 1000);
   JInternalFrame subFrame = new JInternalFrame();//JInternalFrame 内容窗格是添加子组件的地方





JPanel panel = new JPanel();
panel.setBackground(new Color(86,135,216));
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setVgap(20);
flowLayout.setHgap(20);

frame.getContentPane().add(panel, BorderLayout.NORTH);

JLabel label_1 = new JLabel("\u5BB6\u5EAD\u5B89\u9632\u76D1\u63A7\u5BA2\u6237\u7AEF");
label_1.setFont(new Font("宋体", Font.PLAIN, 22));
label_1.setForeground(Color.WHITE);
panel.add(label_1);

JLabel label = new JLabel();
label.setForeground(Color.WHITE);
label.setFont(new Font("宋体", Font.PLAIN, 30));
panel.add(label);

TimePanel panel_1 = new TimePanel();
panel_1.setBackground(new Color(86,135,216));
FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
flowLayout_1.setHgap(15);
flowLayout_1.setVgap(15);
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
//panel_1.add(new TimePanel(),BorderLayout.SOUTH);

JPanel panel_2 = new JPanel();
FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
flowLayout_2.setVgap(100);
flowLayout_2.setHgap(45);
frame.getContentPane().add(panel_2, BorderLayout.WEST);

JPanel panel_4 = new JPanel();
panel_4.setBackground(Color.LIGHT_GRAY);
FlowLayout flowLayout_3 = (FlowLayout) panel_4.getLayout();
flowLayout_3.setVgap(200);
flowLayout_3.setHgap(300);
panel_2.add(panel_4);

JPanel panel_3 = new JPanel();
frame.getContentPane().add(panel_3, BorderLayout.CENTER);

JButton btnNewButton = new JButton("");
btnNewButton.setBackground(Color.WHITE);
btnNewButton.setForeground(Color.WHITE);
btnNewButton.setIcon(new ImageIcon("images/photo.png"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panel_3.add(btnNewButton);
 
JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u53F7\u7801\uFF1A\uFF08\u697C\u5B87#\u623F\u53F7\uFF09   \u5982\uFF1A16#2305");
panel_3.add(lblNewLabel);

textArea = new JTextArea(18,18);
textArea.setText("                                    ");
textArea.setBackground(Color.LIGHT_GRAY);
textArea.setLineWrap(true);
textArea.setTabSize(10);
textArea.setRows(2);
panel_3.add(textArea);

JButton btnNewButton_1 = new JButton("");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
 
handleNumber("1");
 


}
});

JButton btnNewButton_welcome = new JButton("welcome home");

JButton btnNewButton_w = new JButton("\u7269\u4E1A");
btnNewButton_w.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("00#0001");
}
});
panel_3.add(btnNewButton_w);
panel_3.add(btnNewButton_1);
btnNewButton_1.setBackground(Color.WHITE);
btnNewButton_1.setForeground(Color.WHITE);
btnNewButton_1.setIcon(new ImageIcon("images/1.png"));
btnNewButton_1.setHorizontalAlignment(SwingConstants.LEFT);
panel_3.add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("2");
}
});
btnNewButton_2.setBackground(Color.WHITE);
btnNewButton_2.setForeground(Color.WHITE);
btnNewButton_2.setIcon(new ImageIcon("images/2.png"));
panel_3.add(btnNewButton_2);

JButton btnNewButton_3 = new JButton("");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("3");
}
});
btnNewButton_3.setBackground(Color.WHITE);
btnNewButton_3.setForeground(Color.WHITE);
btnNewButton_3.setIcon(new ImageIcon("images/3.png"));
btnNewButton_3.setHorizontalAlignment(SwingConstants.RIGHT);
panel_3.add(btnNewButton_3);

JButton btnNewButton_4 = new JButton("");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("4");
}
});
btnNewButton_4.setBackground(Color.WHITE);
btnNewButton_4.setForeground(Color.WHITE);
btnNewButton_4.setIcon(new ImageIcon("images/4.png"));
panel_3.add(btnNewButton_4);

JButton btnNewButton_5 = new JButton("");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("5");
}
});
btnNewButton_5.setBackground(Color.WHITE);
btnNewButton_5.setForeground(Color.WHITE);
btnNewButton_5.setIcon(new ImageIcon("images/5.png"));
panel_3.add(btnNewButton_5);

JButton btnNewButton_6 = new JButton("");
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("6");
}
});
btnNewButton_6.setBackground(Color.WHITE);
btnNewButton_6.setForeground(Color.WHITE);
btnNewButton_6.setIcon(new ImageIcon("images/6.png"));
panel_3.add(btnNewButton_6);

JButton btnNewButton_7 = new JButton("");
btnNewButton_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("7");
}
});
btnNewButton_7.setBackground(Color.WHITE);
btnNewButton_7.setForeground(Color.WHITE);
btnNewButton_7.setIcon(new ImageIcon("images/7.png"));
panel_3.add(btnNewButton_7);

JButton btnNewButton_8 = new JButton("");
btnNewButton_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("8");
}
});
btnNewButton_8.setBackground(Color.WHITE);
btnNewButton_8.setForeground(Color.WHITE);
btnNewButton_8.setIcon(new ImageIcon("images/8.png"));
panel_3.add(btnNewButton_8);

JButton btnNewButton_9 = new JButton("");
btnNewButton_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("9");
}
});
btnNewButton_9.setBackground(Color.WHITE);
btnNewButton_9.setForeground(Color.WHITE);
btnNewButton_9.setIcon(new ImageIcon("images/9.png"));
panel_3.add(btnNewButton_9);

JButton btnNewButton_xing = new JButton("");
btnNewButton_xing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("*");
}
});
btnNewButton_xing.setBackground(Color.WHITE);
btnNewButton_xing.setForeground(Color.WHITE);
btnNewButton_xing.setIcon(new ImageIcon("images/xing.png"));
panel_3.add(btnNewButton_xing);

JButton btnNewButton_0 = new JButton("");
btnNewButton_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("0");
}
});
btnNewButton_0.setBackground(Color.WHITE);
btnNewButton_0.setForeground(Color.WHITE);
btnNewButton_0.setIcon(new ImageIcon("images/0.png"));
panel_3.add(btnNewButton_0);

JButton btnNewButton_j= new JButton("");
btnNewButton_j.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("#");
}
});
btnNewButton_j.setBackground(Color.WHITE);
btnNewButton_j.setForeground(Color.WHITE);
btnNewButton_j.setIcon(new ImageIcon("images/jing.png"));
panel_3.add(btnNewButton_j);

JButton btnNewButton_call = new JButton("");
btnNewButton_call.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_call.setBackground(Color.WHITE);
btnNewButton_call.setForeground(Color.WHITE);
btnNewButton_call.setIcon(new ImageIcon("images/call.png"));
panel_3.add(btnNewButton_call);

JButton button = new JButton("");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button.setBackground(Color.WHITE);
button.setForeground(Color.WHITE);
button.setIcon(new ImageIcon("images/end.png"));
panel_3.add(button);

JButton btnNewButton_c= new JButton("");
btnNewButton_c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleNumber("c");
}
});
btnNewButton_c.setIcon(new ImageIcon("images/c.png"));
panel_3.add(btnNewButton_c);

}
class ShowTime extends TimerTask {
   public void run() {
       // 刷新
    frame.repaint();
   }
}


class TimePanel extends JPanel {
   public void paint(Graphics g) {
       super.paint(g);
           // 显示时间
       g.drawString(sdf.format(new Date()), 10,10);
   }
}

}


还在为基于C/S模式,使用GUI的Swing组件开发的界面排版烦恼吗,快使用可视化图形界面吧,至少各个组件的位置可以通过直接拖拽来设置,字体,大小位置,颜色等属性可以直接在界面上操作,而不用再通过写代码控制,这样设计出的界面既美观又节省了很多时间!但是不是所有版本的myEclipse软件都带有Swing组件可视化图形界面功能的,myeclipse7.0只能使用一次,myeclpse8.0也需要安装插件才可以使用。下面的就介绍一下在myeclipse8.0上安装可视化图形界面的具体步骤。(这是通过本人验证过的,如下是一步步截图说明的!据说此方法也适合myeclipse6.5,我试过,不知是什么原因,但没成功.。) myEclipse8.0添加可视化图形界面插件的方法说明: 在myEclipse8.0安装目录下的:如C:\Program Files\Genuitec\Pulse Explorer\plugins 文件里,将下载的插件包net.sf.fjep.fatjar_0.0.31.jar拷贝进该文件夹,然后再删除C:\Program Files\Genuitec\Pulse Explorer\configuration目录下除了config.ini文件外的其他文件,最后再重启电脑即可在myEclipse里使用可视化图形界面工具即Swing组件。 1.现在新建一个类(new->Other…),会在出现的如下的界面中选择MyEclipse目录下的Swing 子目录下的Matisse Form,当然,只有你插件安装成功了才会有Swing这个子目录,不安装可视化图形界面插件的话是不会有这个子目录的! 2.按如上操作点击Matisse Form会进入如下界面:输入Name:如Example ,再选择你需要的容器类型,如JFrame。然后点击Finish。 3. 你会看到包下面会生成2个文件:一个是我们新建的类Example.java ,另一个就是可视化的操作界面Example.form。 打开Example.form,注意这两个文件不可以同时打开的,你在Example.form中对界面组件的设置,在Example.java中会有相应代码自动生成!如图,你可以尽情的根据自己的需要对界面进行设计! 你可以在myeclipse中的菜单栏里打开以下3个常用工具栏:Outline ,Matisse Palette及Propertise (Window->Show view->选择上面的那3个窗口)。在Outline中,你可以看到你在这个页面上所添加的窗体,容器,组件等具体结构,你也可以在这上面对页面上的组件进行增删改!Matisse Palette里主要提供了供选择的窗体,容器,组件等,你可以选中你想要的组件然后在form里点击一下,然后拖拽设置你想要的大小和想放置的位置!选中组件直接按delete键就可以将选中的组件删除!在Propertise里你可以设置组件的一些属性,如背景颜色,字体大小,颜色,给组件添加图片等。 更多你自己去试试吧! 4.这个可视化图形界面是非常人性化的,如果你想看一下运行后会是什么效果,不必真去运行一下,只要点击form文件里的工具栏上的第三个按钮:预览 ,即可预览运行后的效果,如下图所示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值