关于Java的JFrame问题
我先初始化了一个JFrameA,然后在JFrameA中新申请了一个JFrameB。为什么当关闭B的时候,A也一起被关闭了?怎样才能让A不被关闭?...
我先初始化了一个JFrame A, 然后在JFrame A中新申请了一个JFrame B。
为什么当关闭B的时候,A也一起被关闭了?
怎样才能让A不被关闭? 展开
为什么当关闭B的时候,A也一起被关闭了?
怎样才能让A不被关闭? 展开
3个回答
展开全部
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class WindowApplication extends JFrame {
public WindowApplication() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextField textField = new JTextField(15);
textField.setPreferredSize(new Dimension(100, 20));
final JLabel label = new JLabel();
JButton button = new JButton("确定");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
new MyWindow().setVisible(true);
}
}
});
container.add(textField);
container.add(button);
container.add(label);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new WindowApplication().setVisible(true);
}
});
}
}
class MyWindow extends JFrame{
public MyWindow() {
initComponents();
}
private void initComponents() {
// 主要是这句话
setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
setSize(200, 200);
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextField textField = new JTextField(15);
textField.setPreferredSize(new Dimension(100, 20));
final JLabel label = new JLabel();
JButton button = new JButton("确定");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
}
});
container.add(textField);
container.add(button);
container.add(label);
}
}
这是一个例子
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class WindowApplication extends JFrame {
public WindowApplication() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextField textField = new JTextField(15);
textField.setPreferredSize(new Dimension(100, 20));
final JLabel label = new JLabel();
JButton button = new JButton("确定");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
new MyWindow().setVisible(true);
}
}
});
container.add(textField);
container.add(button);
container.add(label);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new WindowApplication().setVisible(true);
}
});
}
}
class MyWindow extends JFrame{
public MyWindow() {
initComponents();
}
private void initComponents() {
// 主要是这句话
setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
setSize(200, 200);
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.LEFT));
final JTextField textField = new JTextField(15);
textField.setPreferredSize(new Dimension(100, 20));
final JLabel label = new JLabel();
JButton button = new JButton("确定");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
}
});
container.add(textField);
container.add(button);
container.add(label);
}
}
这是一个例子
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询