Java编写一个简单的窗口程序
用Java编写一个窗口程序,该窗口只有一个菜单组——学生查询,学生查询只有两个菜单选项——确认和取消。当点击确认时,有事件响应,弹出“你点击了确认按钮”对话框。就这样。因...
用Java编写一个窗口程序,该窗口只有一个菜单组——学生查询,学生查询只有两个菜单选项——确认和取消。当点击确认时,有事件响应,弹出“你点击了确认按钮”对话框。就这样。因为添加其他菜单组功能类似,不麻烦各位。谢谢!
展开
1个回答
展开全部
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class MenuItem extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JMenu jMenu = null;
private JMenuItem jMenuItem = null;
private JMenuItem jMenuItem1 = null;
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getJMenu());
}
return jJMenuBar;
}
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
jMenu.setText("学生查询");
jMenu.add(getJMenuItem());
jMenu.add(getJMenuItem1());
}
return jMenu;
}
private JMenuItem getJMenuItem() {
if (jMenuItem == null) {
jMenuItem = new JMenuItem("确认");
jMenuItem.addActionListener(this);
}
return jMenuItem;
}
private JMenuItem getJMenuItem1() {
if (jMenuItem1 == null) {
jMenuItem1 = new JMenuItem("取消");
jMenuItem1.addActionListener(this);
}
return jMenuItem1;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MenuItem thisClass = new MenuItem();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public MenuItem() {
super();
initialize();
}
private void initialize() {
this.setSize(301, 200);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
}
return jContentPane;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jMenuItem)
{
JOptionPane.showMessageDialog(null, "你点击了确认按钮");
}
else if (e.getSource() == jMenuItem1) {
JOptionPane.showMessageDialog(null, "你点击了取消按钮");
}
}
}
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class MenuItem extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JMenu jMenu = null;
private JMenuItem jMenuItem = null;
private JMenuItem jMenuItem1 = null;
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getJMenu());
}
return jJMenuBar;
}
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
jMenu.setText("学生查询");
jMenu.add(getJMenuItem());
jMenu.add(getJMenuItem1());
}
return jMenu;
}
private JMenuItem getJMenuItem() {
if (jMenuItem == null) {
jMenuItem = new JMenuItem("确认");
jMenuItem.addActionListener(this);
}
return jMenuItem;
}
private JMenuItem getJMenuItem1() {
if (jMenuItem1 == null) {
jMenuItem1 = new JMenuItem("取消");
jMenuItem1.addActionListener(this);
}
return jMenuItem1;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MenuItem thisClass = new MenuItem();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public MenuItem() {
super();
initialize();
}
private void initialize() {
this.setSize(301, 200);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
}
return jContentPane;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jMenuItem)
{
JOptionPane.showMessageDialog(null, "你点击了确认按钮");
}
else if (e.getSource() == jMenuItem1) {
JOptionPane.showMessageDialog(null, "你点击了取消按钮");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询