java程序中GUI编程的缺少main方法的代码如何运行

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class FourButtons extends JPanel implements ActionListener{
JTextArea t;
JButton open,clear,save,color;
FourButtons(){
super(new GridLayout(4,1,5,5));
this.t=t;
buttonInit();
add(open);add(save);add(clear);add(color);
}
void buttonInit(){
open=new JButton("Open");
clear=new JButton("Clear");
save=new JButton("Save");
color=new JButton("Color");
open.addActionListener(this);
clear.addActionListener(this);
save.addActionListener(this);
color.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
JButton ob=(JButton)e.getSource();
if(ob==clear) t.setText("");
else if(ob==color) changeColor();
else if(ob==open) open();
else save();
}
void changeColor(){
Color c=JColorChooser.showDialog(this,"Please select",Color.black);
t.setForeground(c);
}
void open(){
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(this);
File file=fc.getSelectedFile();
try{
FileInputStream in=new FileInputStream(file);
byte[] b=new byte[in.available()];
in.read(b);
t.setText(new String(b));
in.close();
}
catch(Exception e){}
}
void save(){
JFileChooser fc=new JFileChooser();
fc.showSaveDialog(this);
File file=fc.getSelectedFile();
try{
FileWriter out=new FileWriter (file);
out.write(t.getText());
out.close();
}
catch(Exception e){}
}
}

第1个回答  2011-12-11
若是在需要运行的程序里,右键选择Run As,之后选择JavaBean就行了
不过,你上面的程序貌似少个public,你在class FourButtons 之前加个public照上面那样就可以了
第2个回答  2011-12-11
这只是一个别人封装的四个按钮的控件,装在了一个JPanel里面~
so~我们写一个东西好了

public class BtnTest(){
public static void main(String[] argv){
JFrame frame = new JFrame();
frame.setSize(600,400);
frame.setVisable(true);
FourButtons fb = new FourButtons();
frame.add(fb);
}
}本回答被提问者采纳
第3个回答  2011-12-12
加个main方法不就好了
第4个回答  2011-12-12
写一个main方法
相似回答
大家正在搜