怎样在JFrame中添加一个JInternalFrame
初学我想实现在JFrame中通过b按钮弹出JInternalFrame但是实现不了求高手指点。importjava.awt.*;importjavax.swing.*;i...
初学我想实现 在JFrame中通过b按钮 弹出JInternalFrame 但是实现不了 求高手指点。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class AdminFrame extends JFrame implements ActionListener{
JButton b;
public JDesktopPane desktop=null;
public AdminFrame(){
super("");
this.setBounds(570,260,300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBackground(java.awt.Color.lightGray);
this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
b=new JButton();
desktop = new JDesktopPane();
this.getContentPane().add(desktop);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b){
new NewUserFrame();
}
}
}
import java.awt.*;
import javax.swing.*;
public class NewUserFrame extends JInternalFrame{
JTextField newNumber = null;
JTextField newPassword= null;
JTextField rePassword= null;
public NewUserFrame(){
super("",true,true,true,true);
this.setSize(300,300);
newNumber = new JTextField(18);
newPassword = new JTextField(18);
rePassword = new JTextField(18);
this.getContentPane().add(newNumber);
this.getContentPane().add(newPassword);
this.getContentPane().add(rePassword);
this.setVisible(true);
}
} 展开
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class AdminFrame extends JFrame implements ActionListener{
JButton b;
public JDesktopPane desktop=null;
public AdminFrame(){
super("");
this.setBounds(570,260,300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBackground(java.awt.Color.lightGray);
this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
b=new JButton();
desktop = new JDesktopPane();
this.getContentPane().add(desktop);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b){
new NewUserFrame();
}
}
}
import java.awt.*;
import javax.swing.*;
public class NewUserFrame extends JInternalFrame{
JTextField newNumber = null;
JTextField newPassword= null;
JTextField rePassword= null;
public NewUserFrame(){
super("",true,true,true,true);
this.setSize(300,300);
newNumber = new JTextField(18);
newPassword = new JTextField(18);
rePassword = new JTextField(18);
this.getContentPane().add(newNumber);
this.getContentPane().add(newPassword);
this.getContentPane().add(rePassword);
this.setVisible(true);
}
} 展开
2个回答
展开全部
有两个地方需要修改:
1、如果使用FlowLayout,那么应该设置desktop的preferredSize
2、将你的InternalFrame加入desktop中
改后:
public class AdminFrame extends JFrame implements ActionListener {
JButton b;
public JDesktopPane desktop = null;
public AdminFrame() {
super("");
this.setBounds(570, 260, 300, 240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBackground(java.awt.Color.lightGray);
this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
b = new JButton("Show Internal Frame");
b.addActionListener(this);
desktop = new JDesktopPane();
desktop.setPreferredSize(new Dimension(300, 300));//设置desktop的大小
this.getContentPane().add(desktop);
this.getContentPane().add(b);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
NewUserFrame newUserFrame = new NewUserFrame();
desktop.add(newUserFrame); //将Internal Frame加入desktop中
}
}
public static void main(String[] args) {
new AdminFrame();
}
}
1、如果使用FlowLayout,那么应该设置desktop的preferredSize
2、将你的InternalFrame加入desktop中
改后:
public class AdminFrame extends JFrame implements ActionListener {
JButton b;
public JDesktopPane desktop = null;
public AdminFrame() {
super("");
this.setBounds(570, 260, 300, 240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBackground(java.awt.Color.lightGray);
this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
b = new JButton("Show Internal Frame");
b.addActionListener(this);
desktop = new JDesktopPane();
desktop.setPreferredSize(new Dimension(300, 300));//设置desktop的大小
this.getContentPane().add(desktop);
this.getContentPane().add(b);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b) {
NewUserFrame newUserFrame = new NewUserFrame();
desktop.add(newUserFrame); //将Internal Frame加入desktop中
}
}
public static void main(String[] args) {
new AdminFrame();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
main方法呢
追问
public static void main(String[] args) { //main 方法 在AdminFrame类中
new AdminFrame();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询