JAVA将一个JFrame放入另一个JFrame
展开全部
1.先下载一个windowbuilder,安装到Eclipse中,就可以很容易做出来图形化的界面设计
源代码如下:
第一个Java文件Menu.java
package output;
import java.awt.BorderLayout;
public class Menu extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Menu frame = new Menu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Menu() {
setTitle("\u7B2C\u4E00\u4E2AJFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.ORANGE);
contentPane.add(desktopPane, BorderLayout.CENTER);
JButton btnframe = new JButton("\u7B2C\u4E8C\u4E2AJFrame");
btnframe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
One frame = new One();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
btnframe.setBounds(60, 60, 155, 23);
desktopPane.add(btnframe);
JButton btnjframe = new JButton("\u7B2C\u4E09\u4E2AJFrame");
btnjframe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Two frame = new Two();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
btnjframe.setBounds(57, 114, 158, 23);
desktopPane.add(btnjframe);
}
}
第二个文件One.java
package output;
import java.awt.BorderLayout;
public class One extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
One frame = new One();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public One() {
setTitle("\u7B2C\u4E8C\u4E2AJFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(Color.GREEN);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
第三个Java文件
package output;
import java.awt.BorderLayout;
public class Two extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Two frame = new Two();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Two() {
setTitle("\u7B2C\u4E09\u4E2AJFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(Color.BLUE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
源代码如下:
第一个Java文件Menu.java
package output;
import java.awt.BorderLayout;
public class Menu extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Menu frame = new Menu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Menu() {
setTitle("\u7B2C\u4E00\u4E2AJFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.ORANGE);
contentPane.add(desktopPane, BorderLayout.CENTER);
JButton btnframe = new JButton("\u7B2C\u4E8C\u4E2AJFrame");
btnframe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
One frame = new One();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
btnframe.setBounds(60, 60, 155, 23);
desktopPane.add(btnframe);
JButton btnjframe = new JButton("\u7B2C\u4E09\u4E2AJFrame");
btnjframe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Two frame = new Two();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
btnjframe.setBounds(57, 114, 158, 23);
desktopPane.add(btnjframe);
}
}
第二个文件One.java
package output;
import java.awt.BorderLayout;
public class One extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
One frame = new One();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public One() {
setTitle("\u7B2C\u4E8C\u4E2AJFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(Color.GREEN);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
第三个Java文件
package output;
import java.awt.BorderLayout;
public class Two extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Two frame = new Two();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Two() {
setTitle("\u7B2C\u4E09\u4E2AJFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(Color.BLUE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
追问
我们这个是考试内容,不能够抄捷径的,不过还是非常感谢你,下次做开发的时候会用windowbuilder
展开全部
public class c
{
public static void main (String args[])
{
JButton b = new JButton(new a().t);
JFrame w = new JFrame();
w.setLayout(null);
b.setBounds(20,20,100,20);
w.add(b);
w.setSize(500,500);
b.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
new a().setVisible(true);
}
});
w.setVisible(true);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class a extends JFrame
{
ImageIcon t = new ImageIcon("E:\\Q.png");
JLabel l = new JLabel(t);
public a()
{
l.setBounds(0,0,t.getIconWidth(),t.getIconHeight());
add(l);
setLayout(null);
setLocation(0,0);
setSize(500,500);
setVisible(false);
}
}
用第一个界面上的按钮打开第二个界面~
我也是新手~
{
public static void main (String args[])
{
JButton b = new JButton(new a().t);
JFrame w = new JFrame();
w.setLayout(null);
b.setBounds(20,20,100,20);
w.add(b);
w.setSize(500,500);
b.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
new a().setVisible(true);
}
});
w.setVisible(true);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class a extends JFrame
{
ImageIcon t = new ImageIcon("E:\\Q.png");
JLabel l = new JLabel(t);
public a()
{
l.setBounds(0,0,t.getIconWidth(),t.getIconHeight());
add(l);
setLayout(null);
setLocation(0,0);
setSize(500,500);
setVisible(false);
}
}
用第一个界面上的按钮打开第二个界面~
我也是新手~
追问
额,我的意思是在同一个窗口中第一个界面的选项卡中包含第二个界面,不是新产生一个窗口
追答
原来如此 献丑了~ 这个问题很深奥啊~ 咱不是很明白~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询