
java程序中GUI编程的缺少main方法的代码如何运行
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.io.*;classFour...
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){}
}
} 展开
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){}
}
} 展开
展开全部
若是在需要运行的程序里,右键选择Run As,之后选择JavaBean就行了
不过,你上面的程序貌似少个public,你在class FourButtons 之前加个public照上面那样就可以了
不过,你上面的程序貌似少个public,你在class FourButtons 之前加个public照上面那样就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这只是一个别人封装的四个按钮的控件,装在了一个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);
}
}
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);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加个main方法不就好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写一个main方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询