读过Java核心技术的大牛们请进

licwang13 2014-09-16 12:35:32
书中有很多的程序清单,其中有的没有main函数。我是Java初学者,想看看程序的运行效果,但是对于没有main函数的程序不知道怎么运行。请问各位大牛当时是怎么解决的?

程序清单8-3 action/ActionFrame.java
"package action;

import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class ActionFrame extends JFrame{

private JPanel buttonPanel;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;

public ActionFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

buttonPanel = new JPanel();

//define actions
Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"),Color.YELLOW);
Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"),Color.BLUE);
Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"),Color.RED);

//add buttons for these actions
buttonPanel.add(new JButton(yellowAction));
buttonPanel.add(new JButton(blueAction));
buttonPanel.add(new JButton(redAction));

//add panel to frame
add(buttonPanel);

//associate the Y, B, and R keys with names
InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");
imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");

//associate the names with actions
ActionMap amap = buttonPanel.getActionMap();
amap.put("panel.yellow", yellowAction);
amap.put("panel.blue", blueAction);
amap.put("panel.red", redAction);
}

public class ColorAction extends AbstractAction
{
/**
* Constructs a color action.
* @param name the name to show on the button
* @param icon the icon to display on the button
* @param c the background color
*/
public ColorAction(String name, Icon icon, Color c)
{
putValue(Action.NAME, name);
putValue(Action.SMALL_ICON, icon);
putValue(Action.SHORT_DESCRIPTION, "Set panel color to " + name.toLowerCase());
putValue("color", c);
}

public void actionPerformed(ActionEvent event)
{
Color c = (Color) getValue("color");
buttonPanel.setBackground(c);
}
}
}
"
...全文
802 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
Anthony_azy 2016-05-19
  • 打赏
  • 举报
回复
把这个加进去,其实这个就是前几个例子里的main方法,一样的
public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				ButtonFrame frame = new ButtonFrame();
				frame.setTitle("哈哈哈!");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
Anthony_azy 2016-05-19
  • 打赏
  • 举报
回复
感谢3楼的,希望其它楼层的人少说点废话
新靖界 2014-09-18
  • 打赏
  • 举报
回复
呵呵,到了一定境界,要不要Main函数都一样了
  • 打赏
  • 举报
回复
你可以按照书中的代码示例去联系,然后自己再添加一个main方法去调用类的方法即可,其实在实际应用中,整个应用只有一个入口,不可能每个类都有一个main,类里封装main方法只是为了单类测试用而已。 添加main方法的代码: public static void main(String[] args) { //调用你当前封装的类方法即可测试。 }
Mark__Zeng 2014-09-17
  • 打赏
  • 举报
回复
不用main直接用静态块static{ }运行,记得在最后加上System.exit(0);就可以了
放纵的青春 2014-09-17
  • 打赏
  • 举报
回复
就比如 别人给你写了个工具类 他不会给你写个main方法来调用的 你自己根据需要来写的
放纵的青春 2014-09-17
  • 打赏
  • 举报
回复
main方法只是自己写来测试一下的 没必要纠结···
铁匠梁 2014-09-17
  • 打赏
  • 举报
回复
可以看看其他代码中用到这个类没,如果用到了,看看是否有main
梦想家丶Kern 2014-09-17
  • 打赏
  • 举报
回复
程序要想运行,必须要有main函数调用。你可以重新新建一个class类文件,然后在main函数下调用这个 类。
Harder-狄戈 2014-09-17
  • 打赏
  • 举报
回复
3楼。8楼
menglanxiang 2014-09-17
  • 打赏
  • 举报
回复
一般都看不到main,测试用junit也不需要main
白开水MD5 2014-09-16
  • 打赏
  • 举报
回复
并不是每个类都有main方法的,可能是在其他main方法里面调用的
scott_129 2014-09-16
  • 打赏
  • 举报
回复
引用 6 楼 weinianjie1 的回复:
main函数?实际应用中你几乎永远看不到它,它只出现在课本里
正解。 我也看过java核心技术,这本书还不错。主要是理解代码的逻辑和做了什么事情。至于想看结果的话,方法有很多啊,自己加一个main可以,使用junit的@Test来进行测试也可以啊。
lyxtt 2014-09-16
  • 打赏
  • 举报
回复
能用吗???
悲剧的程序员 2014-09-16
  • 打赏
  • 举报
回复
书上基本是写里面怎么实现,你自己写main方法,调用你写的方法,把测试的参数传进去就可以了。或者你使用框架,让框架帮你加载。又或者你使用接口,被动的接收数据。 注:每一个java程序都会有一个入口的。
-阿克蒙德- 2014-09-16
  • 打赏
  • 举报
回复
这问题…… 建议你顺便看看8楼提到的东西,以后工作会用到
qq_20889099 2014-09-16
  • 打赏
  • 举报
回复
你可以试试用junit
licwang13 2014-09-16
  • 打赏
  • 举报
回复
引用 3 楼 gaofuqi 的回复:
[quote=引用 2 楼 licwang13 的回复:] [quote=引用 1 楼 a470577391 的回复:] 并不是每个类都有main方法的,可能是在其他main方法里面调用的
我知道,但是书中提供的很多程序都没有main[/quote] 自己加main的方法:

public static void main(String[] args)
	{
		ActionFrame frame = new ActionFrame();
		frame.setVisible(true);
	}
[/quote] 非常感谢
weinianjie1 2014-09-16
  • 打赏
  • 举报
回复
main函数?实际应用中你几乎永远看不到它,它只出现在课本里
痞子月 2014-09-16
  • 打赏
  • 举报
回复
自己写个main 然后调用下方法是不是就可以了
加载更多回复(3)

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧