importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;publicclassprintspecextendsJFrameimplementsActionListener{staticfinalStringOUTPUT="C://Test.tx...
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class printspec extends JFrame implements ActionListener
{
static final String OUTPUT="C://Test.txt";
JPanel pnl;
JLabel lbl;
JTextField txt1,txt2;
JButton btnCopy,btnClear,btnOutput,btnColor;
JMenuBar mb=new JMenuBar();//菜单栏
JMenu file=new JMenu("文件");//文件菜单
public printspec()
{
super("printspec");
pnl=new JPanel();//中间容器
this.setContentPane(pnl);///意思是给窗体加个pane
pnl.setLayout(null);//设置jpane;的布局管理器为空啊,你就可以自己手动的设置组件的坐标位置和大小了
pnl.setBackground(Color.WHITE);
lbl=new JLabel("百度");
//lbl.setBounds(300,100,20,25);
txt1=new JTextField("ok,",10);//提示输入的文字 以及字符长度 最多为10
txt2=new JTextField(10);
btnCopy=new JButton("复制");
btnCopy.addActionListener(this);
btnClear=new JButton("清除");
btnClear.addActionListener(this);
btnOutput=new JButton("写入");
btnOutput.addActionListener(this);
btnColor=new JButton("变色");
//menubar1= new JMenuBar();
//menu1 =new JMenu("1");
btnColor.addActionListener(this);
//menu1.setBounds(10, 10, 80, 20);
lbl.setBounds(100, 10, 80, 20);
txt1.setBounds(10, 40, 100, 20);
txt2.setBounds(120, 40, 100, 20);
btnCopy.setBounds(10, 70, 60, 20);
btnClear.setBounds(75, 70, 60, 20);
btnOutput.setBounds(140, 70, 60, 20);
btnColor.setBounds(205, 70, 60, 20);
pnl.add(lbl);
pnl.add(txt1);
pnl.add(txt2);
pnl.add(btnCopy);
pnl.add(btnClear);
pnl.add(btnOutput);
pnl.add(btnColor);
setSize(1100,700);
setVisible(true);
//init();
}
public void Copy()
{
txt2.setText(txt1.getText());
}
public void Clear()
{
txt1.setText("");
txt2.setText("");
pnl.setBackground(Color.WHITE);
}
public void Color()
{
pnl.setBackground(Color.BLACK);
}
public void Ouput()
{
try
{
File fl=new File("C:\\Test.txt");
FileWriter fw = new FileWriter(fl);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(txt1.getText());
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnCopy)
this.Copy();
if(e.getSource()==btnClear)
this.Clear();
if(e.getSource()==btnColor)
this.Color();
if(e.getSource()==btnOutput)
this.Ouput();
}
public static void main(String[] args)
{
new printspec();
}
}
//这是我的代码 但是不能显示出最简单的菜单栏 求修改 让我能显示出菜单栏 是“文件”与“属性”两个菜单
展开