求救一题,悬赏100分。 求助一题,要源程序,最好是运行验证过的。题目:使用Awt或swing或Applet编程实现一个按钮,当单击按钮时,弹出信息框,框中提示信息“java你好”。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import javax.swing.*;import java.awt.event.*;class HelloJava extends JPanel { private JButton button; public HelloJava() { button = new JButton("Button"); this.add(button); // Button event handle button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(HelloJava.this, "Hello Java"); } }); } private static void createUIAndShow() { JFrame frame = new JFrame("Swing Example"); frame.setSize(400, 400); frame.setLocation(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new HelloJava(); frame.getContentPane().add(panel); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { HelloJava.createUIAndShow(); } }); }}编译运行吧。 import javax.swing.*;import java.awt.event.*;class HelloJava { public static void main(String[] args) { JFrame frame = new JFrame("Swing Example"); frame.setSize(400, 400); frame.setLocation(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel panel = new JPanel(); JButton button = new JButton("Button"); panel.add(button); frame.getContentPane().add(panel); frame.setVisible(true); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(panel, "Hello Java"); } }); }}你真够省的啊,第一个程序是Java官方写swing程序的标准方式。 这种问题随便翻翻swing的书,第一页估计就是了.....散分吧!public class World { public static void main(String[] args){ javax.swing.JFrame frame = new javax.swing.JFrame("Sample"); javax.swing.JButton button = new javax.swing.JButton("click"); frame.getContentPane().add(button); button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { javax.swing.JOptionPane.showConfirmDialog(null,"Hello!","Information",-1,1); } }); frame.pack(); frame.setVisible(true); }}凑凑热闹! diggywang(Miner Lover!) 的已经不能再精简了...... public class Hello{ public static void main(String[] args) { final JFrame jf=new JFrame("Hello"); jf.getContentPane().add(new JButton(new AbstractAction("点我"){ public void actionPerformed(ActionEvent ae){ JOptionPane.showMessageDialog(jf,"Hello java!"); } })); jf.pack(); jf.setVisible(true); }}我这个更短 import javax.swing.JFrame;import javax.swing.JButton;import java.awt.event.ActionListener;import javax.swing.JOptionPane;import java.awt.event.ActionEvent;public class HelloWorld implements ActionEvent{ public static void main(String[] args){ JFrame frame = new JFrame("Sample"); JButton btn = new JButton("click"); frame.getContentPane().add(btn); btn.addActionListener(this); frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { javax.swing.JOptionPane.showConfirmDialog(null,"HelloJava","Information",-1,1); }} 还是注释我自己的吧,对楼主我只能彻底无语,感觉被耍了.........public class World {//主函数入口 public static void main(String[] args){ //要显示的窗口 javax.swing.JFrame frame = new javax.swing.JFrame("Sample"); //定义按钮 javax.swing.JButton button = new javax.swing.JButton("click"); //得到窗口容器,并在上面添加一个按钮 frame.getContentPane().add(button); //注册按钮监听 button.addActionListener(new java.awt.event.ActionListener() { //按钮被按下后的动作 public void actionPerformed(java.awt.event.ActionEvent e) { //显示对话框 javax.swing.JOptionPane.showConfirmDialog(null,"Hello!"," Information",-1,1); } }); //排版界面,并显示 frame.pack(); frame.setVisible(true); }} package untitled1;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JButton;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JOptionPane;public class Frame1 extends JFrame { JPanel contentPane; JButton jButton1 = new JButton(); private Object panel; public Frame1() { try { setDefaultCloseOperation(EXIT_ON_CLOSE); jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } /** * Component initialization. * * @throws java.lang.Exception */ private void jbInit() throws Exception { contentPane = (JPanel) getContentPane(); contentPane.setLayout(null); setSize(new Dimension(400, 300)); setTitle("Frame Title"); jButton1.setBounds(new Rectangle(107, 35, 81, 24)); jButton1.setText("点击"); jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this)); contentPane.add(jButton1); } public void jButton1_actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Hello Java"); }}class Frame1_jButton1_actionAdapter implements ActionListener { private Frame1 adaptee; Frame1_jButton1_actionAdapter(Frame1 adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }} 从楼主的意思,可以看出,楼主只是对JAVA界面编程的好奇,其实界面和其它的类一样,只是一个类,比如你new 窗口 代码是 new JFrame() ,产生这个窗口对像后,你偏可以设置其中一些属性,比如背景,大小,及增加监听器之类的了,至于其窗口类是怎么实现的,你现在可以不管。代码,以上各们都给出很好的了,不再重复。如果有什么不明白的地方,可以问,最好不要只是要代码,学东西,重在理解! diggywang 的注释挺清楚,谢谢,明天就结。 新手,怎么把一个数字转换成时间格式输出 正规表达式,只保留html图片代码当中的src="",去掉里面的其它代码 怎么让jsp回车不刷新 Class 对象问题 java播放音频的求助 新手求助,哪位大侠能帮一下忙! 虚拟键码和键盘掩码有什么区别? 如何能将中文pdf文件中的内容正确的读出来? 请问如何在c++里面调用java? Jsoup成对抓取标签内容 为什么我在netbeans中不能修改控件的代码呢? 好人帮帮我吧~~~
import java.awt.event.*;class HelloJava extends JPanel {
private JButton button;
public HelloJava() {
button = new JButton("Button");
this.add(button);
// Button event handle
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(HelloJava.this, "Hello Java");
}
});
}
private static void createUIAndShow() {
JFrame frame = new JFrame("Swing Example");
frame.setSize(400, 400);
frame.setLocation(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new HelloJava();
frame.getContentPane().add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
HelloJava.createUIAndShow();
}
});
}}
编译运行吧。
import java.awt.event.*;class HelloJava {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
frame.setSize(400, 400);
frame.setLocation(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
JButton button = new JButton("Button");
panel.add(button);
frame.getContentPane().add(panel);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(panel, "Hello Java");
}
});
}
}
你真够省的啊,第一个程序是Java官方写swing程序的标准方式。
散分吧!
public class World {
public static void main(String[] args){
javax.swing.JFrame frame = new javax.swing.JFrame("Sample");
javax.swing.JButton button = new javax.swing.JButton("click");
frame.getContentPane().add(button);
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
javax.swing.JOptionPane.showConfirmDialog(null,"Hello!","Information",-1,1);
}
});
frame.pack();
frame.setVisible(true);
}
}
凑凑热闹!
public static void main(String[] args) {
final JFrame jf=new JFrame("Hello");
jf.getContentPane().add(new JButton(new AbstractAction("点我"){
public void actionPerformed(ActionEvent ae){
JOptionPane.showMessageDialog(jf,"Hello java!");
}
}));
jf.pack();
jf.setVisible(true);
}
}
我这个更短
import javax.swing.JButton;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
public class HelloWorld implements ActionEvent{ public static void main(String[] args){
JFrame frame = new JFrame("Sample");
JButton btn = new JButton("click");
frame.getContentPane().add(btn);
btn.addActionListener(this);
frame.pack();
frame.setVisible(true);
} public void actionPerformed(ActionEvent e) {
javax.swing.JOptionPane.showConfirmDialog(null,"HelloJava","Information",-1,1);
}}
public class World {
//主函数入口
public static void main(String[] args){
//要显示的窗口
javax.swing.JFrame frame = new javax.swing.JFrame("Sample");
//定义按钮
javax.swing.JButton button = new javax.swing.JButton("click");
//得到窗口容器,并在上面添加一个按钮
frame.getContentPane().add(button);
//注册按钮监听
button.addActionListener(new java.awt.event.ActionListener() {
//按钮被按下后的动作
public void actionPerformed(java.awt.event.ActionEvent e) {
//显示对话框
javax.swing.JOptionPane.showConfirmDialog(null,"Hello!","
Information",-1,1);
}
});
//排版界面,并显示
frame.pack();
frame.setVisible(true);
}
}
import java.awt.Dimension;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;public class Frame1 extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
private Object panel;
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} /**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jButton1.setBounds(new Rectangle(107, 35, 81, 24));
jButton1.setText("点击");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.add(jButton1);
} public void jButton1_actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello Java");
}
}
class Frame1_jButton1_actionAdapter implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
其实界面和其它的类一样,只是一个类,
比如你new 窗口 代码是 new JFrame() ,
产生这个窗口对像后,你偏可以设置其中一些属性,
比如背景,大小,及增加监听器之类的了,
至于其窗口类是怎么实现的,你现在可以不管。
代码,以上各们都给出很好的了,不再重复。
如果有什么不明白的地方,可以问,
最好不要只是要代码,
学东西,重在理解!