用java程序设计, 编写GUI程序
1.编写GUI程序,要求:用户在密码框中输入数据,用另外一个文本框显示密码框中输入的内容。2.编写GUI程序,要求,实现一个下拉式菜单系统,包含三个菜单“文件”,“编辑”...
1.编写GUI程序,要求:用户在密码框中输入数据,用另外一个文本框显示密码框中输入的内容。
2.编写GUI程序,要求,实现一个下拉式菜单系统,包含三个菜单“文件”,“编辑”,“帮助”,选择其中一个菜单添加菜单项,并能够对单击菜单做出响应 展开
2.编写GUI程序,要求,实现一个下拉式菜单系统,包含三个菜单“文件”,“编辑”,“帮助”,选择其中一个菜单添加菜单项,并能够对单击菜单做出响应 展开
1个回答
2013-09-03
展开全部
楼上的 搜来的代码 也要改改才能符合楼主的要求吧…… 我两个都写到一起了 import java.awt.*;import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;public class FrameTest extends JFrame implements DocumentListener{
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPasswordField jPasswordField1 = new JPasswordField();
JTextField jTextField1 = new JTextField();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenu jMenu2 = new JMenu();
JMenu jMenu3 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel(); public FrameTest() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
jMenu1.setText("文件");
jMenu2.setText("编辑");
jMenu2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "菜单“编辑”被点击");
}
}); jMenu3.setText("帮助");
jMenu3.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "菜单“帮助”被点击");
}
}); jMenuItem1.setText("打开");
jMenuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "菜单项“打开”被点击");
}
});
jMenuItem2.setText("退出");
jMenuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "菜单项“退出”被点击,系统关闭");
System.exit(0);
}
});
jLabel1.setText("密码:");
jLabel1.setBounds(new Rectangle(43, 75, 42, 15));
jLabel2.setText("显示密码:");
jLabel2.setBounds(new Rectangle(35, 128, 82, 15)); jPanel1.add(jPasswordField1);
jPanel1.add(jTextField1);
jPanel1.add(jLabel1);
jPanel1.add(jLabel2);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(156, 123, 135, 25));
jPasswordField1.setText("");
jPasswordField1.getDocument().addDocumentListener(this);
jPasswordField1.setBounds(new Rectangle(156, 70, 135, 25));
jPanel1.setLayout(null); this.setJMenuBar(jMenuBar1);
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
this.setSize(500, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
} public void changedUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()));
} public void insertUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()));
} public void removeUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()));
}
public static void main(String[] args) {
new FrameTest();
}
}
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;public class FrameTest extends JFrame implements DocumentListener{
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPasswordField jPasswordField1 = new JPasswordField();
JTextField jTextField1 = new JTextField();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenu jMenu2 = new JMenu();
JMenu jMenu3 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel(); public FrameTest() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
jMenu1.setText("文件");
jMenu2.setText("编辑");
jMenu2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "菜单“编辑”被点击");
}
}); jMenu3.setText("帮助");
jMenu3.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(null, "菜单“帮助”被点击");
}
}); jMenuItem1.setText("打开");
jMenuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "菜单项“打开”被点击");
}
});
jMenuItem2.setText("退出");
jMenuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "菜单项“退出”被点击,系统关闭");
System.exit(0);
}
});
jLabel1.setText("密码:");
jLabel1.setBounds(new Rectangle(43, 75, 42, 15));
jLabel2.setText("显示密码:");
jLabel2.setBounds(new Rectangle(35, 128, 82, 15)); jPanel1.add(jPasswordField1);
jPanel1.add(jTextField1);
jPanel1.add(jLabel1);
jPanel1.add(jLabel2);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(156, 123, 135, 25));
jPasswordField1.setText("");
jPasswordField1.getDocument().addDocumentListener(this);
jPasswordField1.setBounds(new Rectangle(156, 70, 135, 25));
jPanel1.setLayout(null); this.setJMenuBar(jMenuBar1);
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
this.setSize(500, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2); this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
} public void changedUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()));
} public void insertUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()));
} public void removeUpdate(DocumentEvent e) {
jTextField1.setText(String.valueOf(jPasswordField1.getPassword()));
}
public static void main(String[] args) {
new FrameTest();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询