Java,有两个JFrame A和B,A是父窗体,B是子窗体,在A中添加一个按钮,按下按钮,B显示在A中心,怎么写
1个回答
展开全部
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FrameTest extends JFrame implements ActionListener {
public static final int SIZE_1 = 500;
public static final int SIZE_2 = 400;
private JFrame myFrame;
private JButton button;
FrameTest(){
this.setTitle("我是母窗口");
this.setSize(SIZE_1, SIZE_1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame = new JFrame("我是子窗口");
myFrame.setSize(SIZE_2, SIZE_2);
button = new JButton("显示子窗口");
button.addActionListener(this);
this.add(button);
this.setVisible(true);
}
public void getFramLocation(){
int newX = this.getX()+((SIZE_1 - SIZE_2)/2);
int newY = this.getY() +((SIZE_1 - SIZE_2)/2);
myFrame.setVisible(false);
myFrame.setLocation(newX, newY);
myFrame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new FrameTest();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
this.getFramLocation();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FrameTest extends JFrame implements ActionListener {
public static final int SIZE_1 = 500;
public static final int SIZE_2 = 400;
private JFrame myFrame;
private JButton button;
FrameTest(){
this.setTitle("我是母窗口");
this.setSize(SIZE_1, SIZE_1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame = new JFrame("我是子窗口");
myFrame.setSize(SIZE_2, SIZE_2);
button = new JButton("显示子窗口");
button.addActionListener(this);
this.add(button);
this.setVisible(true);
}
public void getFramLocation(){
int newX = this.getX()+((SIZE_1 - SIZE_2)/2);
int newY = this.getY() +((SIZE_1 - SIZE_2)/2);
myFrame.setVisible(false);
myFrame.setLocation(newX, newY);
myFrame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new FrameTest();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
this.getFramLocation();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询