展开全部
两个e68a84e8a2ad62616964757a686964616f31333236386135JFrame,好像不行,JFrame只有一个!
你这个意思我基本上是懂了,你是想像VB那样弄一个主窗体
和两个子窗体,两个窗体可以随意的切换和关闭,但是互不相关
呵呵,你的
"再按了a的关闭按钮后,如何实现a关了而b不关,现在统一都关了"
问题就是,现在只有一个父窗体,一个子窗体,你关闭一个,两个必然会
都关闭,因为,子窗体是继承的父窗体,他们是个整体,所以你这样不行!
现在有两种类似的解决方法
一个是在JFrame中放置连个JPlane,充当子窗体
另一个就是用多标签来实现,你看看这个程序就知
道了,可以直接运行的!
import java.awt.*;
import java.awt.font.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
public class tab extends JFrame implements ActionListener,ItemListener
{
JPanel p0=new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel p1=new JPanel();
JPanel p2=new JPanel(new BorderLayout());
JPanel p3=new JPanel();
JPanel p4=new JPanel(new FlowLayout(FlowLayout.CENTER));
String[] style={"PLAIN","BOLD","ITALIC","BOLD&ITALIC"};
String[] Size={"8","10","12","14","16","18","20","25","30","40"};
JTextField txt1=new JTextField(10);
JComboBox cobox;
JComboBox cobox2=new JComboBox(style);
JComboBox cobox3=new JComboBox(Size);;
JButton b1=new JButton("Open");
JButton b2=new JButton("Save");
JButton bcolor=new JButton("颜色");
JButton bwrap=new JButton("自动换行");
JTextArea jtxt=new JTextArea();
JScrollPane jtxta=new JScrollPane(jtxt);
JLabel lab1=new JLabel(new ImageIcon("image2.gif"));
JTabbedPane tp=new JTabbedPane();
public static int count=0;
String currFileName;
public tab()
{
super("多标签");
initGUI();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.setSize(800,600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}
public void initGUI()
{
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String enfonts[] =ge.getAvailableFontFamilyNames();
tp.setPreferredSize(new Dimension(790,560));
cobox=new JComboBox(enfonts);
p1.add(txt1);
p0.add(cobox);
p0.add(cobox2);
p0.add(cobox3);
p0.add(bcolor);
p0.add(bwrap);
cobox.setMaximumRowCount(5);
cobox3.setMaximumRowCount(5);
p2.add(p4,BorderLayout.SOUTH);
p2.add(p0,BorderLayout.NORTH);
p2.add(jtxta,BorderLayout.CENTER);
p4.add(b1);
p4.add(b2);
p3.add(cobox3);
tp.addTab("Text",p1);
tp.addTab("Button",p2);
tp.addTab("image",p3);
this.add(tp);
cobox.addItemListener(this);
cobox2.addItemListener(this);
cobox3.addItemListener(this);
b1.addActionListener(this);
bcolor.addActionListener(this);
bwrap.addActionListener(this);
this.addWindowListener(new WindowAdapter()
{
public void closing(WindowEvent e)
{
count=0;
}
});
}
public void actionPerformed(ActionEvent ae)
{
Object obj=ae.getSource();
if(obj==b1)
{
File f;
JFileChooser jf=new JFileChooser(".java");
int i=jf.showOpenDialog(null);
if(i==JFileChooser.APPROVE_OPTION)
{
f=jf.getSelectedFile();
currFileName=f.getPath();
try
{
jtxt.setText(null);
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String s=null;
while((s=br.readLine())!=null)
{
jtxt.append(s + "\n");
}
br.close();
fr.close();
}
catch (Exception e)
{
}
}
}
if(obj==b2)
{
saveFile();
}
if(obj==bcolor)
{
Color c;
JColorChooser jcc=new JColorChooser();
c=jcc.showDialog(this,"选择颜色背景",jtxt.getForeground());
if(c!=null)
jtxt.setForeground(c);
//this.repaint();
}
if(obj==bwrap)
{
if((count++)%2==1)
jtxt.setLineWrap(true);
else
jtxt.setLineWrap(false);
}
}
public boolean saveFile()
{
if(currFileName==null) return saveAsFile();
try
{
File file=new File(currFileName);
FileWriter out=new FileWriter(file);
String text=jtxt.getText();
out.write(text);
out.close();
//this.dirty=false;
//this.setTitle(currFilename);
return true;
}
catch (Exception e)
{
}
return false;
}
public boolean saveAsFile()
{
JFileChooser fc=new JFileChooser();
fc.showSaveDialog(this);
currFileName=fc.getSelectedFile().getPath();
return saveFile();
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getStateChange()!=ItemEvent.SELECTED)
{
return;
}
String fontName=(String)cobox.getSelectedItem();
int fontStyle=cobox2.getSelectedIndex();
String fontSize=(String)cobox3.getSelectedItem();
jtxt.setFont(new Font(fontName,fontStyle,Integer.parseInt(fontSize)));
}
public static void main(String args[])
{
tab ta=new tab();
}
}
希望能对你有启发!!
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起