eclipse运行代码无反应,控制台显示终止
代码如下:packageswing;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.ev...
代码如下:
package swing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
class MyJDialog extends JDialog{
public MyJDialog(MyFrame frame){
super(frame,"窗体",true);
Container container=getContentPane();
container.add(new JLabel("对话框"));
setBounds(120,120,100,100);
}
}
@SuppressWarnings("serial")
public class MyFrame extends JFrame{
public static void main(String args[]){
new MyFrame();
}
public MyFrame(){
Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("JFrame窗体");
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
JButton bl=new JButton("弹出对话框");
bl.setBounds(10, 10, 100, 21);
bl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyJDialog(MyFrame.this).setVisible(true);
}
});
container.add(bl);
}
} 展开
package swing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
class MyJDialog extends JDialog{
public MyJDialog(MyFrame frame){
super(frame,"窗体",true);
Container container=getContentPane();
container.add(new JLabel("对话框"));
setBounds(120,120,100,100);
}
}
@SuppressWarnings("serial")
public class MyFrame extends JFrame{
public static void main(String args[]){
new MyFrame();
}
public MyFrame(){
Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("JFrame窗体");
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
JButton bl=new JButton("弹出对话框");
bl.setBounds(10, 10, 100, 21);
bl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyJDialog(MyFrame.this).setVisible(true);
}
});
container.add(bl);
}
} 展开
1个回答
展开全部
修改后的代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
class MyJDialog extends JDialog {
public MyJDialog(MyFrame frame) {
super(frame,"窗体",true);
Container container=getContentPane();
container.add(new JLabel("对话框"));
setBounds(120,120,100,100);
}
}
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
public static void main(String args[]) {
new MyFrame();
}
public MyFrame() {
Container container=getContentPane();
container.setLayout(null);
JLabel jl=new JLabel("JFrame窗体");
jl.setHorizontalAlignment(SwingConstants.CENTER);
container.add(jl);
JButton bl=new JButton("弹出对话框");
bl.setBounds(10, 10, 100, 21);
bl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new MyJDialog(MyFrame.this).setVisible(true);
}
});
container.add(bl);
this.setBounds(100, 100, 300, 500); //设置窗体大小
this.setVisible(true); //设置窗体可见
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询