Java Swing问题,为什么窗口显示不出来,求帮解决。
packagegui2;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassTe...
package gui2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestSwing extends JFrame implements ActionListener{
public static void main(String agrs[]){
}
public TestSwing(){
JFrame jf=new JFrame("My JFrame");
JMenuBar jmb=new JMenuBar();
JMenu file =new JMenu("file");
JMenu help =new JMenu("help");
JMenuItem jm_new =new JMenuItem("new");
JMenuItem open= new JMenuItem("OPen");
JMenuItem seve =new JMenuItem("seve");
jm_new.addActionListener(this);
open.addActionListener(this);
seve.addActionListener(this);
jm_new.setMnemonic('n');
open.setMnemonic('o');
seve.setMnemonic('s');
file.setMnemonic('f');
help.setMnemonic('h');
file.add(jm_new);
file.add(open);
file.add(seve);
jmb.add(file);
jmb.add(help);
JToolBar jt =new JToolBar();
JButton jbnew=new JButton(new ImageIcon("1.jpg"));
JButton jbopen =new JButton(new ImageIcon("2.jpg"));
JButton jbseve=new JButton(new ImageIcon("3.jpg"));
jbnew.setActionCommand("new");
jbopen.setActionCommand("open");
jbseve.setActionCommand("seve");
jbnew.setToolTipText("新建文件");
jbopen.setToolTipText("打开文件");
jbseve.setToolTipText("保存文件");
jbnew.addActionListener(this);
jbopen.addActionListener(this);
jbseve.addActionListener(this);
jt.add(jbnew);
jt.add(jbopen);
jt.add(jbseve);
JPanel jp= new JPanel();
JButton jbstart=new JButton("Start");
JButton jbstop =new JButton("stop");
jbstart.setToolTipText("开始操作");
jbstop.setToolTipText("停止操作");
jbstart.setMnemonic('s');
jbstop.setMnemonic('t');
jp.add(jbstart);
jp.add(jbstop);
jf.setJMenuBar(jmb);
jf.add(jt,"North");
jf.add(jp,"South");
jf.setSize(300,200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
System.out.println(e.getActionCommand());
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestSwing extends JFrame implements ActionListener{
public static void main(String agrs[]){
}
public TestSwing(){
JFrame jf=new JFrame("My JFrame");
JMenuBar jmb=new JMenuBar();
JMenu file =new JMenu("file");
JMenu help =new JMenu("help");
JMenuItem jm_new =new JMenuItem("new");
JMenuItem open= new JMenuItem("OPen");
JMenuItem seve =new JMenuItem("seve");
jm_new.addActionListener(this);
open.addActionListener(this);
seve.addActionListener(this);
jm_new.setMnemonic('n');
open.setMnemonic('o');
seve.setMnemonic('s');
file.setMnemonic('f');
help.setMnemonic('h');
file.add(jm_new);
file.add(open);
file.add(seve);
jmb.add(file);
jmb.add(help);
JToolBar jt =new JToolBar();
JButton jbnew=new JButton(new ImageIcon("1.jpg"));
JButton jbopen =new JButton(new ImageIcon("2.jpg"));
JButton jbseve=new JButton(new ImageIcon("3.jpg"));
jbnew.setActionCommand("new");
jbopen.setActionCommand("open");
jbseve.setActionCommand("seve");
jbnew.setToolTipText("新建文件");
jbopen.setToolTipText("打开文件");
jbseve.setToolTipText("保存文件");
jbnew.addActionListener(this);
jbopen.addActionListener(this);
jbseve.addActionListener(this);
jt.add(jbnew);
jt.add(jbopen);
jt.add(jbseve);
JPanel jp= new JPanel();
JButton jbstart=new JButton("Start");
JButton jbstop =new JButton("stop");
jbstart.setToolTipText("开始操作");
jbstop.setToolTipText("停止操作");
jbstart.setMnemonic('s');
jbstop.setMnemonic('t');
jp.add(jbstart);
jp.add(jbstop);
jf.setJMenuBar(jmb);
jf.add(jt,"North");
jf.add(jp,"South");
jf.setSize(300,200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
System.out.println(e.getActionCommand());
}
} 展开
2个回答
展开全部
main方法里加上
new TestSwing();
new TestSwing();
追问
为什么呢?
追答
你那些方法加载窗体的工作都写在了构造方法里,
怎么样执行构造方法?就是new啊
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询