import javax.swing.*;import java.awt.*;import java.awt.event.*;
class Mywindow extends JFrame
{ JButton button1,button2,button;JTextArea text;JSplitPane split_one,split_two;
Mywindow()
{setSize(200,400);setVisible(true); Container con=getContentPane();
JInternalFrame interframe_1,interframe_2;
button=new JButton("Boy");
interframe_1=new JInternalFrame("内窗体1",true,true,true,true);
interframe_1.setSize(100,100);
interframe_1.getContentPane().add(button);
JDesktopPane desk1=new JDesktopPane();
desk1.add(interframe_1);
button1=new JButton("ok"); button2=new JButton("No");
text=new JTextArea("I love you,java",10,20);
split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,desk1,button2);
split_two=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,split_one,text);
con.add(split_two,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
}
public class Example25_8
{ public static void main(String args[])
{ Mywindow win=new Mywindow();win.pack();
}
}
试一试
class Mywindow extends JFrame
{ JButton button1,button2,button;JTextArea text;JSplitPane split_one,split_two;
Mywindow()
{setSize(200,400);setVisible(true); Container con=getContentPane();
JInternalFrame interframe_1,interframe_2;
button=new JButton("Boy");
interframe_1=new JInternalFrame("内窗体1",true,true,true,true);
interframe_1.setSize(100,100);
interframe_1.getContentPane().add(button);
JDesktopPane desk1=new JDesktopPane();
desk1.add(interframe_1);
button1=new JButton("ok"); button2=new JButton("No");
text=new JTextArea("I love you,java",10,20);
split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,desk1,button2);
split_two=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,split_one,text);
con.add(split_two,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
}
public class Example25_8
{ public static void main(String args[])
{ Mywindow win=new Mywindow();win.pack();
}
}
试一试
不过,你的SplitPane还是加到了contentPane中,
我的应用是这样的,我要做一个MDI的应用,在桌面desktop上打开一个InternalFrame,然后我的应用的UI存放在Internalframe的SplitPane中,
我认为你的这句:con.add(split_two,BorderLayout.CENTER);是加在con中的了!
可以再帮我看看吗?
class Mywindow extends JFrame
{ JButton button1,button2,button;JTextArea text;JSplitPane split_one,split_two;
Mywindow()
{setSize(200,400);setVisible(true); Container con=getContentPane();
JPanel pp = new JPanel();
TextField tt = new TextField("Hello");
TextArea ta = new TextArea(4,10);
pp.setLayout(new FlowLayout());
pp.add(tt);
pp.add(ta);
JInternalFrame interframe_1,interframe_2;
button=new JButton("Boy");
interframe_1=new JInternalFrame("内窗体1",true,true,true,true);
interframe_1.setSize(100,100);
interframe_1.getContentPane().add(pp);
JDesktopPane desk1=new JDesktopPane();
desk1.add(interframe_1);
button1=new JButton("ok"); button2=new JButton("No");
text=new JTextArea("I love you,java",10,20);
split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,desk1,button2);
split_two=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,split_one,text);
con.add(split_two,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
}
public class Example25_8
{ public static void main(String args[])
{ Mywindow win=new Mywindow();win.pack();
}
}
稍作改动,没细考虑
* test.java
*
* Created on 2002年9月16日, 下午12:56
*//**
*
* @author Administrator
*/
public class test extends javax.swing.JInternalFrame {
/** Creates new form test */
public test() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
jTree1 = new javax.swing.JTree();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea(); jSplitPane1.setLeftComponent(jTree1); jTextArea1.setPreferredSize(new java.awt.Dimension(280, 50));
jScrollPane1.setViewportView(jTextArea1); jSplitPane1.setRightComponent(jScrollPane1); getContentPane().add(jSplitPane1, java.awt.BorderLayout.WEST); pack();
}
// Variables declaration - do not modify
private javax.swing.JTree jTree1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JSplitPane jSplitPane1;
// End of variables declaration
}
JInternalFrame(String title,boolean resizable,boolean closable,boolean max ,boolean min),
第一个参数是窗体的名字,接下来的参数分别决定窗体能否调整大小,能否关闭,能否最大化,能否最小化。对于内部窗体需注意下列两点:
1. 内部窗体和前面讲的中间容器有所不同,我们不能直接把组件加到内部窗体中,而只能加到它的内容面板中。内部窗体和JFrame窗体一样,可以通过getContentPane()得到它的内容窗体。
2. 为了能显示内部窗体,必须把内部窗体先添加到一个容器中,这个容器是JdesktopPane,该容器是专门为内部窗体服务的。
3. 不必再调用setVisible为内部窗体设置可见性,它和所在的容器一起显示。但是内部窗体仍然需要调用 setSize()或setBounds设置大小。内部窗体的默认布局也是BorderLayout布局。
to qxjavajava(射手座) :你仍然是直接把JSplitPane加到contentPane中了,你的代码中生成了desktopPane,但是没有使用!
desk1.add(interframe_1);
.....
split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,desk1,button2);
谁说我没用JDesktopPane
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;public class Test extends JFrame{ public Test(){
Container contentPane = getContentPane();
JInternalFrame iframe = new NewFrame();
JDesktopPane desktop = new JDesktopPane();
desktop.add(iframe,new Integer(1));
contentPane.add(desktop);
try{
iframe.setVisible(true);
iframe.setSelected(true);
}catch(java.beans.PropertyVetoException ee){
System.out.println("sdjfl");
ee.printStackTrace();
} } public static JFrame createFrame() {
JFrame frame = new Test();
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
return frame;
}
public JSplitPane initSplitPane(){
JTextArea text1 = new JTextArea();
JTextArea text2 = new JTextArea();
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,text1,text2);
split.setDividerLocation(20);
return split;
}
public static void main(String[] args) {
JFrame fframe = createFrame();
fframe.setSize(200,200);
fframe.show();
}
class NewFrame extends JInternalFrame{
public NewFrame(){
super("",true,true,true,true);
setTitle("sd");
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
JSplitPane split = initSplitPane();
top.add(split,BorderLayout.CENTER);
setContentPane(top);
pack();
setSize(100,100);
setLocation(10,10);
}
}}
我要做的就是在一个MDI的窗口中打开一个内部窗口(JInternalFrame),在打开的JInternalFrame中使用JSplitPane!
to qxjavajava(射手座) :刚才是我没有看清楚,不好意思!但是你是把desktop(包含JInternalFrame)添加到了JSplitPane中,我要问的是怎么能把JSplitPane添加到JInternalFrame中